PHP mkdir失败了

时间:2012-10-03 17:03:36

标签: php html file file-upload upload

我正在尝试上传文件,所以这是我的html:

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

这是我的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";
    }

?>

输出的文件:

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

var_dump在测试文件中输出:

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) } }

它给我的警告/错误是:

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

没有mkdir代码,它给了我:

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

出于某种原因,无论是否有mkdir,即使文件类型正确,也不会在Proposals文件中放入任何文件。这只是服务器的权限问题,还是我需要添加/更改以使这一切都能正常工作?

2 个答案:

答案 0 :(得分:1)

一个。第一个错误

  

严格标准:只有变量应该通过引用传递到第6行的/disks/vhosts///upload_file.php上传:Test.doc

更改

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

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

B中。第二个错误

  

警告:move_uploaded_file(Proposals / Test.doc):无法打开流:第40行的/disks/vhosts///upload_file.php中的权限被拒绝警告:move_uploaded_file():无法在第40行的/disks/vhosts///upload_file.php中将'/ tmp / phpQmgxdO'移动到'Proposals / Test.doc'存储于:Proposals / Test.doc

使用完整路径/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");
    }
}

答案 1 :(得分:1)

您目前有此代码......

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"];
}

如果您要查找的目录存在,那么只有move_uploaded_file。将该行移到if语句下面,然后根据需要创建目录,然后上传文件。