php文件上传失败

时间:2014-01-11 15:17:01

标签: php file-upload upload

我按照互联网上的每个教程来上传文件。但是,它失败了。它给了我这个错误:

Warning: move_uploaded_file(/var/www/projects/upload/TASK.txt): failed to open stream: No such file or directory in /var/www/projects/test/upload.php on line 6 Warning: move_uploaded_file(): Unable to move '/tmp/phpjr2JJA' to '/var/www/projects/upload/TASK.txt' in /var/www/projects/test/upload.php on line 6 Something went wrong

的index.html     

 <head>
        <title></title>
    </head>
    <body>
        <form method="POST" action="upload.php" enctype="multipart/form-data">
            <input type="file" name="upload" ><br />
            <input type="hidden" name="MAX_FILE_SIZE" value="1024" />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>
    </html>

upload.php的

<?php

$target_path = $_SERVER['DOCUMENT_ROOT'] . "/upload/";
$target_path = $target_path . basename( $_FILES['upload']['name'] );

if ( move_uploaded_file($_FILES['upload']['tmp_name'], $target_path) ) {
    echo "has been uploaded";
} else {
    echo "Something went wrong";
}

你能帮助我并指出我哪里出错吗?我使用的是ubuntu 12.04,并尝试将/upload文件夹的权限更改为755,并检查file_upload中的php.iniON

非常感谢任何帮助。谢谢!

3 个答案:

答案 0 :(得分:1)

$ _ SERVER ['DOCUMENT_ROOT']给出/ var / www / projects / upload ..作为输出    /在开始时是错误原因

因此

尝试使用相对路径

<?php

$target_path = "upload/";
$target_path = $target_path . basename( $_FILES['upload']['name'] );

if ( move_uploaded_file($_FILES['upload']['tmp_name'], $target_path) ) {
echo "has been uploaded";
} else {
echo "Something went wrong";
}
?>

为我工作

答案 1 :(得分:0)

文件&amp; Dir权限......

将您的FTP程序666添加到文件和目录中。

答案 2 :(得分:0)

我认为您的文件上传位置不正确

index.php

<html>
<head>
    <title>Upload your file</title>
</head>
<body>

<form action="upload.php" method="post"
enctype="multipart/form-data">
   <label for="file">Filename:</label>
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="Upload" value="Submit">
</form>
</body>

upload.php的

<?php

 $target="give the path you want to store the file/";


if ($_FILES["file"]["error"] > 0)
 {
   echo "Error: " . $_FILES["file"]["error"] . "<br>";
 }
 else
 {
    move_uploaded_file($_FILES["file"]["tmp_name"],
   $target. $_FILES["file"]["name"]);

  }
?>