Charlie Yabben
Charlie Yabben

Reputation: 307

PHP mkdir failing

I'm trying to upload a file so here's my html:

<form action="upload_file.php" method="post" enctype="multipart/form-data">
    Select a file: <input type="file" name="upload">
    <input type="submit">
</form>

Here's my php:

    <?php
    ini_set('display_errors', 'On');
    error_reporting(E_ALL | E_STRICT);
    session_start();
    $allowedExts = array("doc", "docx");
    $extension = pathinfo( $_FILES["upload"]["name"],PATHINFO_EXTENSION);

    if (($_FILES["upload"]["size"] < 200000)
    && in_array($extension, $allowedExts)) {
        if ($_FILES["upload"]["error"] > 0)
        {
            echo "Return Code: " . $_FILES["upload"]["error"] . "<br />";
        }
        else
        {
            echo "Upload: " . $_FILES["upload"]["name"] . "<br />";
            echo "Type: " . $_FILES["upload"]["type"] . "<br />";
            echo "Size: " . ($_FILES["upload"]["size"] / 1024) . " Kb<br />";
            echo "Temp file: " . $_FILES["upload"]["tmp_name"] . "<br />";

            $dir_exists = is_dir("Proposals/". $_SESSION["FirstName"] ."/");
            $file_exists = file_exists("Proposals/".$_SESSION["FirstName"] ."/" . $_FILES["upload"]["name"]);
            $folderName=$_SESSION["FirstName"];
            $baseDir = "Proposals" ;
            //var_dump($_FILES);
            //die();
            // Create directory if it does not exist
            if (! $dir_exists) {
                if(!is_dir($baseDir))
                    mkdir($baseDir);
                mkdir($baseDir . DIRECTORY_SEPARATOR . $_SESSION["FirstName"]);
            } 

            if ($file_exists) {
                echo $_FILES["upload"]["name"] . " already exists. ";
            } else {
                move_uploaded_file($_FILES["upload"]["tmp_name"],
                "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"]);
                echo "Stored in: " . "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"];
            }
        }
    } else {
        echo "Invalid file";
    }

?>

The file outputted:

Type: application/msword
Size: 124.9296875 Kb
Temp file: /tmp/phpUWDc3s

The var_dump outputted this on a test file:

array(1) { ["upload"]=> array(5) { 
["name"]=> string(18) "Test.doc" 
["type"]=> string(18) "application/msword" 
["tmp_name"]=> string(14) "/tmp/phpUWDc3s" 
["error"]=> int(0) 
["size"]=> int(127928) } }

And the warnings/errors it gives me are:

Strict Standards: Only variables should be passed by reference in /disks/vhosts/***/***/upload_file.php on line 6 Upload: Test.doc

WITHOUT the mkdir code it gives me:

Warning: move_uploaded_file(Proposals/Test.doc): failed to open stream: Permission denied in /disks/vhosts/***/***/upload_file.php on line 40 Warning: move_uploaded_file(): Unable to move '/tmp/phpQmgxdO' to 'Proposals/Test.doc' in /disks/vhosts/***/***/upload_file.php on line 40 Stored in: Proposals/Test.doc

For some reason with or without the mkdir, no file is being put in the Proposals paper even if it's the right file type. Is this just a permission issue with the server or is there something I have to add/change to make this all work?

Upvotes: 2

Views: 709

Answers (2)

Baba
Baba

Reputation: 95101

A. The first Error

Strict Standards: Only variables should be passed by reference in /disks/vhosts///upload_file.php on line 6 Upload: Test.doc

Change

  $extension = end(explode(".", $_FILES["upload"]["name"]));

To

  $extension = path_info( $_FILES["upload"]["name"],PATHINFO_EXTENSION);

B. The second error

Warning: move_uploaded_file(Proposals/Test.doc): failed to open stream: Permission denied in /disks/vhosts///upload_file.php on line 40 Warning: move_uploaded_file(): Unable to move '/tmp/phpQmgxdO' to 'Proposals/Test.doc' in /disks/vhosts///upload_file.php on line 40 Stored in: Proposals/Test.doc

Use something like this with full path /disks/vhosts/***/***/

$baseDir = __DIR__ ; // or /disks/vhosts/***/***/ has applicable 
if (! $dir_exists) {
    if (is_writable("$baseDir/Proposals/")) {
        mkdir("$baseDir/Proposals/" . $_SESSION["FirstName"]);
    } else {
        trigger_error("Proposals/ is not writeable");
    }
}

Upvotes: 1

Justin Wood
Justin Wood

Reputation: 10041

You currently have this code...

if(!$dir_exists) {
    mkdir("Proposals/". $_SESSION["FirstName"]);
} elseif ($file_exists) {
    echo $_FILES["upload"]["name"] . " already exists. ";
} else {
    move_uploaded_file($_FILES["upload"]["tmp_name"],
    "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"]);
    echo "Stored in: " . "Proposals/". $_SESSION["FirstName"] ."/". $_FILES["upload"]["name"];
}

Which will ONLY move_uploaded_file if the directory you're looking for exists. Move that line below your if statement, and it should then create the directory if needed, then upload the file.

Upvotes: 1

Related Questions