允许用户通过网站上传文件PHP

时间:2013-08-29 20:43:12

标签: php file-upload upload

好的,所以我跟着一个tutorial教我如何在我的网站上建立一个简单的上传系统。好吧,我收到了错误......

我有一个表单,允许用户选择他们要上传的文件:

<form enctype="multipart/form-data" action="studentAccess/files/uploader.php" method="POST">

    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />

    Choose a file to upload: <input name="uploadedfile" type="file" /><br />

    <input type="submit" value="Upload File" />

</form>

这个PHP脚本应该完成所有工作。现在我对这个教程略感困惑,我不确定这个脚本是应该分成两个不同的文件,还是和现在一样在同一个文件中。

<?php

// Where the file is going to be placed 
$target_path = "/studentAccess/uploads/";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

$target_path = "/studentAccess/uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

?>

上面的脚本放在studentAccess里面的Files文件夹中

我想要存储上传文件的文件夹位于studentAccess中并被称为uploads

studentAccess
  |
  | files 
  | |
  | | uploader.php *this is where the script is located to upload file*
  | |
  | uploads
  | |
  | | *this is the folder where I want to store the uploaded files*
  | |

以上是所有内容所在的文件夹布局。我之所以包括这个是因为我不确定它是否正确布局。 uploader.php文件是否需要与上传文件存储在同一文件夹中?

当我运行脚本(即使用表单上传文件并点击上传文件按钮)时,我会收到直接在浏览器窗口中显示的三个错误。

Warning: move_uploaded_file(/studentAccess/uploads/NHSHandbook1314.doc) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/www/usd309bands.org/studentAccess/files/uploader.php on line 14

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpRpP6uz' to '/studentAccess/uploads/NHSHandbook1314.doc' in /home/www/usd309bands.org/studentAccess/files/uploader.php on line 14

最后是脚本本身产生的错误

There was an error uploading the file, please try again!

所以我真的不确定我做错了什么,在我看来,将上传的文件移动到uploads文件夹时遇到了问题。但我真的不确定我需要做些什么来解决这个问题。 我确实已检查并且我的服务器中已启用file_uploads

我希望我已经详细描述了我的问题以获得帮助。我希望它以可读的格式布局。我真的需要一些帮助。

1 个答案:

答案 0 :(得分:0)

它可能是文件夹许可限制,尝试制作目标文件夹777(读写 - 通过您的ftp程序执行)并查看它是否有效。我强烈建议你使用php的ftp函数来上传文件而不是move_uploaded_file方法。

更新

尝试从$ target_path =“/ studentAccess / uploads /”更改目标路径; to $ target_path =“../uploads /”;