【发布时间】:2013-08-29 20:43:11
【问题描述】:
好的,所以我关注了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!
所以我真的不确定自己做错了什么,在我看来,将上传的文件移动到上传文件夹时出现问题。但我真的不确定我需要做什么来解决这个问题。 我做了检查,file_uploads 在我的服务器中打开。
我希望我已经详细描述了我的问题以获得帮助。我希望它以可读的格式排列。我真的需要一些帮助。
【问题讨论】:
-
一方面,您定义了
$target_path = "/studentAccess/uploads/";两次。删除第二个。 -
尝试从你的根目录运行上传表单(和相同的处理程序),并像这样设置它
$target_path = "studentAccess/uploads/";没有开始的正斜杠,看看是否有效。并查看文件夹是否存在,是否可写。 -
这就是为什么我不确定是否需要为这些文件制作两个文件。如果您按照我的方式查看本教程,我相信它就是这样。
-
我非常熟悉的教程没有提到开头/起始斜线。您可能有路径问题。尝试上传到一个创建的子文件夹而不是两个,并且没有起始正斜杠。即:
$target_path = "studentAccess/"; -
尝试删除目标路径开头的
/
标签: php file-upload upload