2

我正在尝试上传文件,所以这是我的 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,提案文件中都没有放入任何文件,即使它是正确的文件类型。这只是服务器的权限问题,还是我必须添加/更改才能使这一切正常工作?

4

2 回答 2

1

A. 第一个错误

严格的标准:只有变量应该在/disks/vhosts///upload_file.php第6行通过引用传递上传: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():无法将“/tmp/phpQmgxdO”移动到“提案” /Test.doc' in /disks/vhosts/ / /upload_file.php on line 40 存储在: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");
    }
}
于 2012-10-03T17:08:24.473 回答
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语句下方,然后它应该在需要时创建目录,然后上传文件。

于 2012-10-03T17:16:57.120 回答