Skip to content
Advertisement

Uploading PDFs to a directory PHP

I’m uploading PDFs to a directory and my script works fine for one directory but I’m having troubles coming up with a way to write the script efficiently when I have more then one directory to upload PDFs on a page. I know of a few ways I can do it, like write another function like below but there must be a better way so I dont have to write out the whole script for every directory I want to upload PDFs to. Code below.

if(is_post_request()) {

$targetdirectory = "../../pathofdirectory/pdf/";
$targetdirectory = $targetdirectory . basename( $_FILES['file']['name']) ;

$file_type=$_FILES['file']['type'];

if ($file_type=="application/pdf") {

 if(move_uploaded_file($_FILES['file']['tmp_name'], $targetdirectory))

 {

 $message = "The file ". basename( $_FILES['file']['name']). " is uploaded";

 }

 else {

 $message = "Problem uploading file";

 }
}

else {

 $message = "You may only upload PDFs.<br>";

}
    
}

And of course the simple form

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file" size="50" />
    <input class="button common" type="submit" value="Submit" />
</form>

Thanks in advance for any suggestions.

Advertisement

Answer

If you’re okay with allowing users choosing which directory to upload to, you can give them the option.

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="file" size="50" />
    <select name="folderOption">
        <option value="1">First Folder</option>
        <option value="2">Second Folder</option>
        <option value="3">Third Folder</option>
    </select>
    <input class="button common" type="submit" value="Submit" />
</form>

Obviously, you would need to validate server-side for bad data.

if (is_post_request()) {
    $targetdirectory = "../../pathofdirectory/pdf/";
    $targetdirectory = $targetdirectory . basename( $_FILES['file']['name']) ;
    $file_type = $_FILES['file']['type'];
    $folderOption = $_POST['folderOption'];
    if ($folderOption == 1 || $folderOption == 2 || $folderOption == 3) {
        switch ($folderOption) {
            case 1:
                $targetdirectory = "../../pathofdirectory/pdf/";
                break;
            case 2:
                $targetdirectory = "../../pathofdirectory2/pdf/";
                break;
            case 3:
                $targetdirectory = "../../pathofdirectory3/pdf/";
                break;
            default:
                $targetdirectory = "../../pathofdirectory/pdf/";
        }
        if ($file_type == "application/pdf") {
            if(move_uploaded_file($_FILES['file']['tmp_name'], $targetdirectory)) {
                $message = "The file ". basename( $_FILES['file']['name']). " is uploaded";
            } else {
                $message = "Problem uploading file";
            }
        } else {
            $message = "You may only upload PDFs.<br>";
        }
    } else {
        $message = "Bad data received for directory choice.<br>";
    }
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement