设置 PHP tmp 目录 - PHP 上传不起作用

2021-12-24 00:00:00 file-upload php valums-file-uploader

我正在使用 Valums 文件上传器通过带有进度条的网页上传文件.几乎一切正常,但我无法更改默认的 tmp 目录,该目录在上传过程中存储文件.

I'm working on file upload via a webpage with a progress bar using Valums file uploader. Almost everything works fine, but I'm not able to change the default tmp directory, where the file is stored during the upload.

文件应该存放在/upload目录下,而不是系统默认的/tmp目录,因为/tmp是挂载在内存盘中的其大小限制为 4MB,用户将上传大约 10MB 的文件.

Files should be stored in /upload directory and not in default system /tmp directory, because /tmp is mounted in a RAM disk which has its size limited to 4 MB and user will be uploading files around 10 MB.

我搜索了很多网页,但没有一个解决方案奏效.我在 php.ini 中设置了临时目录:

I've searched lots of webpages, but none of solutions worked. I've set temp directory in php.ini:

upload_tmp_dir =/upload

我已经为/upload 目录设置了权限,并且apache 是该文件的所有者,所以该目录肯定是PHP 可写的.

I've set permissions to the /upload dir, and apache is owner of the file, so the directory is definitely writable by PHP.

我将文件上传器中的目标路径设置为/upload,因为我希望上传后的文件也存储在此目录中.最终结果是小文件上传成功,但大于 4MB 的文件上传失败 - 我想到的这种行为的唯一原因是文件在上传过程中存储在 /tmp.可以肯定的是,我已经用 sys_get_temp_dir() 检查了它,结果是 /tmp - 所以 PHP 忽略了我的 php.ini 指令或者有一些其他的方法来设置上传过程中文件的存储位置.

I've set the target path in file uploader to /upload, because I want the files to be stored after the upload also in this directory. The final result is small files are being uploaded successfuly, but files larger than 4 MB fail to upload-the only reason of this behaviour that comes to my mind is that files are stored in /tmp during upload. To be sure, I've checked it with sys_get_temp_dir() and the result was /tmp-so PHP ignores my php.ini directive or there is some other way to set where files are stored during upload.

哦,还有最后一个信息:open_basedir 没有设置,所以 PHP 对磁盘的访问只受文件权限的限制.

Oh, and the last information: open_basedir isn't set, so the PHP access to disk is only limited by file permissions.

推荐答案

这里描述的问题我很久以前就解决了,但我真的不记得上传不起作用的主要原因是什么.有很多事情需要修复,以便上传可以工作.我已经创建了清单,可以帮助其他有类似问题的人,我将对其进行编辑以使其尽可能有用.之前我在聊天中说过,我是做嵌入式系统的,所以在非嵌入式系统上可能会跳过一些要点.

The problem described here was solved by me quite a long time ago but I don't really remember what was the main reason that uploads weren't working. There were multiple things that needed fixing so the upload could work. I have created checklist that might help others having similar problems and I will edit it to make it as helpful as possible. As I said before on chat, I was working on embedded system, so some points may be skipped on non-embedded systems.

  • 检查 php.ini 中的 upload_tmp_dir.这是 PHP 在上传时存储临时文件的目录.

  • Check upload_tmp_dir in php.ini. This is directory where PHP stores temporary files while uploading.

检查 php.ini 中的 open_basedir.如果定义,它将限制 PHP 对指定路径及其子目录的读/写权限.确保 upload_tmp_dir 在此路径内.

Check open_basedir in php.ini. If defined it limits PHP read/write rights to specified path and its subdirectories. Ensure that upload_tmp_dir is inside this path.

检查 php.ini 中的 post_max_size.如果您想上传 20 MB 的文件,请尝试更大一些的文件,例如 post_max_size = 21M.这定义了您在上传过程中可能使用的 POST 消息的最大大小.

Check post_max_size in php.ini. If you want to upload 20 Mbyte files, try something a little bigger, like post_max_size = 21M. This defines largest size of POST message which you are probably using during upload.

检查 php.ini 中的 upload_max_filesize.这指定了可以上传的最大文件.

Check upload_max_filesize in php.ini. This specifies biggest file that can be uploaded.

检查 php.ini 中的 memory_limit.这是脚本可能消耗的最大内存量.很明显,它不能小于上传大小(说实话我不太确定——PHP 可能在复制临时文件时正在缓冲).

Check memory_limit in php.ini. That's the maximum amount of memory a script may consume. It's quite obvious that it can't be lower than upload size (to be honest I'm not quite sure about it-PHP is probably buffering while copying temporary files).

确保您正在检查正确的 php.ini 文件,该文件是 PHP 在您的网络服务器上使用的文件.最好的解决方案是使用此处描述的指令执行脚本 http://php.net/manual/en/function.php-ini-loaded-file.php (php_ini_loaded_file 函数)

Ensure that you're checking the right php.ini file that is one used by PHP on your webserver. The best solution is to execute script with directive described here http://php.net/manual/en/function.php-ini-loaded-file.php (php_ini_loaded_file function)

检查用户 php 以什么身份运行(参见此处如何操作:如何检查 php 正在以什么用户身份运行?).我曾在不同的发行版和服务器上工作过.有时它是apache,但有时它可以是root.无论如何,请检查此用户是否有权在您上传的临时目录和目录中进行读写.检查路径中的所有目录,以防您上传到子目录(例如 /dir1/dir2/ - 检查 dir1dir2.

Check what user php runs as (See here how to do it: How to check what user php is running as? ). I have worked on different distros and servers. Sometimes it is apache, but sometimes it can be root. Anyway, check that this user has rights for reading and writing in the temporary directory and directory that you're uploading into. Check all directories in the path in case you're uploading into subdirectory (for example /dir1/dir2/-check both dir1 and dir2.

在嵌入式平台上,您有时需要限制写入根文件系统,因为它存储在闪存卡上,这有助于延长该卡的使用寿命.如果您使用脚本来启用/禁用文件写入,请确保在上传前启用写入.

On embedded platforms you sometimes need to restrict writing to root filesystem because it is stored on flash card and this helps to extend life of this card. If you are using scripts to enable/disable file writes, ensure that you enable writing before uploading.

我遇到了基于会话的 PHP >5.4 上传监控的严重问题(如此处所述 http://phpmaster.com/tracking-upload-progress-with-php-and-javascript/ )在某些平台上.首先尝试一些简单的事情(例如:http://www.dzone.com/snippets/very-simple-php-file-upload).如果可行,您可以尝试更复杂的机制.

I had serious problems with PHP >5.4 upload monitoring based on sessions (as described here http://phpmaster.com/tracking-upload-progress-with-php-and-javascript/ ) on some platforms. Try something simple at first (like here: http://www.dzone.com/snippets/very-simple-php-file-upload ). If it works, you can try more sophisticated mechanisms.

如果您对 php.ini 进行了任何更改,请记住重新启动服务器,以便重新加载配置.

If you make any changes in php.ini remember to restart server so the configuration will be reloaded.

相关文章