PHP move_uploaded_file() 失败,我不知道为什么
这是我的代码:
$uploaddir = '/temp/';
$uploadfile = $uploaddir.basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
send_OK();
else
send_error("ERROR - uploading file");
我尝试使用 ftp_fput、ftp_put、move_uploaded_file 进行上传、重命名、复制以及我可以使用的任何内容。似乎没有什么作用。
我无法理解问题是什么,因为 move_uploaded_file 仅返回 true 或 false 并且没有错误代码。
帮助??
this is my code:
$uploaddir = '/temp/';
$uploadfile = $uploaddir.basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile))
send_OK();
else
send_error("ERROR - uploading file");
i have tried to upload with ftp_fput, ftp_put, move_uploaded_file, rename, copy and anything i can put my hands on. nothing seems to work.
i can't understand what is the problem since move_uploaded_file returns only true or false and no error code.
help??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您确定目标目录具有
world
的写权限吗?即权限表示中的第三个数字?php 上传的文件归
www-data
组所有,您可以通过以下方式更改所有权
Are you sure that the target directory has write permissions for
world
?ie,the third number in permission representation?The files uploaded by php are owned by and comes under the group
www-data
You can change the ownership by
,但你必须这么做。
这就是错误消息的用途。
出现问题时您是否看到任何错误消息?如果没有,那么您必须检查错误日志。
在代码顶部添加这一行
,如果它是您的本地(非实时)服务器
,那么您将能够在屏幕上看到错误
对于文件上传,您必须检查
$_FILES['file']首先是['错误'])
。如果不是0
,请参阅手册页了解实际消息。But you have to.
That's what error messages are for.
Do you see any error message when something goes wrong? If not, then you have to check error logs.
Add this line at the top of your code
and this one, if it's your local (not live) server
so you'll be able to see errors onscreen
For the file uploads you have to check
$_FILES['file']['error'])
first. it it's not0
, refer to the manual page for the actual message.我在使用 move_uploaded_file 时遇到了类似的问题,它无法上传 $_FILES['filename']['error'] 代码为 0 的特定文件。
事实证明文件名相对于目标目录必须是唯一的。 move_uploaded_file 不知道如何处理相同的文件名。
I experienced a similar problem when using move_uploaded_file which would fail to upload particular files with an $_FILES['filename']['error'] code of 0.
It turns out that the name of the file needs to be unique in relation to the destination directory. move_uploaded_file does not know how to handle identical files names.
您检查过文件大小的限制吗?崩溃的原因之一可能是您尝试上传的文件大于配置中的限制。查看 php.ini 中的配置变量“upload_max_filesize”并检查文件的大小。
Have you check the limit of the file size? One of the reason if crashing could be that you are trying to upload a file bigger than the limit in your configuration. Look at the config var "upload_max_filesize" in your php.ini and check the size of the file.
这也让我感到困惑。知道:
例如,如果您尝试将文件移至网站基本目录之外,这些设置可能会导致上传失败。
This caught me out too. Be aware of:
These settings can cause the upload to fail if you try to move the file outside of your website base directory for example.
除了权限之外,请务必检查服务器上是否有可用磁盘空间。如果不是,move_uploaded_file() 将失败并返回错误 0。
In addition to permissions, be sure to check that there is disk space available on your server. If not, move_uploaded_file() will fail with error 0.
您是否尝试激活 error_reporting?
您应该检查 php-config 是否允许文件上传。
Did you try to activate error_reporting?
You should check your php-config if file uploads are allowed.