PHP move_uploaded_file() 失败,我不知道为什么

发布于 2024-12-20 19:32:18 字数 417 浏览 0 评论 0原文

这是我的代码:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

夏了南城 2024-12-27 19:32:18

您确定目标目录具有world的写权限吗?即权限表示中的第三个数字?
php 上传的文件归 www-data 组所有,

您可以通过以下方式更改所有权

[sudo] chown -R www-data folder // change owner
[sudo] chown -R www-data:www-data folder // change group and owner

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

[sudo] chown -R www-data folder // change owner
[sudo] chown -R www-data:www-data folder // change group and owner
空‖城人不在 2024-12-27 19:32:18

不知道为什么

,但你必须这么做。

这就是错误消息的用途。
出现问题时您是否看到任何错误消息?如果没有,那么您必须检查错误日志。

在代码顶部添加这一行

error_reporting(E_ALL);

,如果它是您的本地(非实时)服务器

ini_set('display_errors',1);

,那么您将能够在屏幕上看到错误

对于文件上传,您必须检查 $_FILES['file']首先是['错误'])。如果不是 0,请参阅手册页了解实际消息。

i don't know why

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

error_reporting(E_ALL);

and this one, if it's your local (not live) server

ini_set('display_errors',1);

so you'll be able to see errors onscreen

For the file uploads you have to check $_FILES['file']['error']) first. it it's not 0, refer to the manual page for the actual message.

今天小雨转甜 2024-12-27 19:32:18

我在使用 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.

深海夜未眠 2024-12-27 19:32:18

您检查过文件大小的限制吗?崩溃的原因之一可能是您尝试上传的文件大于配置中的限制。查看 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.

迷荒 2024-12-27 19:32:18

这也让我感到困惑。知道:

move_uploaded_file() 既支持安全模式,又支持 open_basedir。但是,仅对目标路径进行限制,以允许移动文件名可能与此类限制冲突的上传文件。 move_uploaded_file() 仅允许移动通过 PHP 上传的文件,从而确保此操作的安全性。

例如,如果您尝试将文件移至网站基本目录之外,这些设置可能会导致上传失败。

This caught me out too. Be aware of:

move_uploaded_file() is both safe mode and open_basedir aware. However, restrictions are placed only on the destination path as to allow the moving of uploaded files in which filename may conflict with such restrictions. move_uploaded_file() ensures the safety of this operation by allowing only those files uploaded through PHP to be moved.

These settings can cause the upload to fail if you try to move the file outside of your website base directory for example.

醉酒的小男人 2024-12-27 19:32:18

除了权限之外,请务必检查服务器上是否有可用磁盘空间。如果不是,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.

你穿错了嫁妆 2024-12-27 19:32:18

您是否尝试激活 error_reporting?

您应该检查 php-config 是否允许文件上传。

Did you try to activate error_reporting?

You should check your php-config if file uploads are allowed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文