设置 PHP tmp 目录 - PHP 上传不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12531408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting PHP tmp dir - PHP upload not working
提问by Wookie88
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 duringthe upload.
我正在使用Valums file uploader通过带有进度条的网页上传文件。几乎一切正常,但我无法更改默认的 tmp 目录,该目录在上传过程中存储文件。
Files should be stored in /uploaddirectory and not in default system /tmpdirectory, because /tmpis mounted in a RAM disk which has its size limited to 4 MB and user will be uploading files around 10 MB.
文件应存储在/upload目录中,而不是默认系统/tmp目录中,因为文件/tmp安装在 RAM 磁盘中,其大小限制为 4 MB,用户将上传大约 10 MB 的文件。
I've searched lots of webpages, but none of solutions worked.
I've set temp directory in php.ini:
我搜索了很多网页,但没有一个解决方案有效。我已经在以下位置设置了临时目录php.ini:
upload_tmp_dir =/upload
I've set permissions to the /uploaddir, and apache is owner of the file, so the directory is definitely writable by PHP.
我已经为该/upload目录设置了权限,并且 apache 是该文件的所有者,因此该目录绝对可以被 PHP 写入。
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 /tmpduring 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.
我已将文件上传器中的目标路径设置为/upload,因为我希望文件在上传后也存储在此目录中。最终结果是小文件上传成功,但大于4MB的文件上传失败——我想到的唯一原因是文件/tmp在上传过程中存储在其中。可以肯定的是,我已经检查过它,sys_get_temp_dir()结果是/tmp-so PHP 忽略了我的 php.ini 指令,或者有其他一些方法可以设置上传过程中文件的存储位置。
Oh, and the last information: open_basedirisn't set, so the PHP access to disk is only limited by file permissions.
哦,还有最后一个信息:open_basedir未设置,因此 PHP 对磁盘的访问仅受文件权限的限制。
回答by Wookie88
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.
这里描述的问题很久以前就被我解决了,但我真的不记得上传不起作用的主要原因是什么。有很多事情需要修复,以便上传可以工作。我已经创建了清单,可以帮助其他有类似问题的人,我将对其进行编辑以使其尽可能有用。正如我之前在聊天中所说的,我在研究嵌入式系统,所以在非嵌入式系统上可能会跳过一些要点。
Check
upload_tmp_dirin php.ini. This is directory where PHP stores temporary files while uploading.Check
open_basedirin php.ini. If defined it limits PHP read/write rights to specified path and its subdirectories. Ensure thatupload_tmp_diris inside this path.Check
post_max_sizein php.ini. If you want to upload 20 Mbyte files, try something a little bigger, likepost_max_size = 21M. This defines largest size of POST message which you are probably using during upload.Check
upload_max_filesizein php.ini. This specifies biggest file that can be uploaded.Check
memory_limitin 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).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_filefunction)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 beroot. 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 bothdir1anddir2.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.
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.
If you make any changes in php.ini remember to restart server so the configuration will be reloaded.
检查
upload_tmp_dirphp.ini。这是 PHP 在上传时存储临时文件的目录。检查
open_basedirphp.ini。如果定义,它将限制 PHP 对指定路径及其子目录的读/写权限。确保upload_tmp_dir在此路径内。检查
post_max_sizephp.ini。如果您想上传 20 MB 的文件,请尝试更大一些的文件,例如post_max_size = 21M. 这定义了您在上传过程中可能使用的最大 POST 消息大小。检查
upload_max_filesizephp.ini。这指定了可以上传的最大文件。检查
memory_limitphp.ini。这是脚本可能消耗的最大内存量。很明显,它不能小于上传大小(说实话,我不太确定 - PHP 在复制临时文件时可能正在缓冲)。确保您正在检查正确的 php.ini 文件,该文件是 PHP 在您的网络服务器上使用的文件。最好的解决方案是使用此处描述的指令执行脚本http://php.net/manual/en/function.php-ini-loaded-file.php(
php_ini_loaded_filefunction)检查用户 php 以什么身份运行(请参阅此处如何操作:如何检查 php 正在以什么用户身份运行?)。我曾在不同的发行版和服务器上工作过。有时是
apache,但有时可以root。无论如何,请检查此用户是否有权在您上传的临时目录和目录中进行读写。检查路径中的所有目录,以防您上传到子目录(例如/dir1/dir2/-check bothdir1和dir2.在嵌入式平台上,您有时需要限制写入根文件系统,因为它存储在闪存卡上,这有助于延长该卡的使用寿命。如果您使用脚本来启用/禁用文件写入,请确保在上传之前启用写入。
我在某些平台上遇到了基于会话的 PHP > 5.4 上传监控(如http://phpmaster.com/tracking-upload-progress-with-php-and-javascript/ 所述)的严重问题。首先尝试一些简单的事情(例如:http: //www.dzone.com/snippets/very-simple-php-file-upload)。如果有效,您可以尝试更复杂的机制。
如果您在 php.ini 中进行任何更改,请记住重新启动服务器,以便重新加载配置。
回答by CapCrunch
I struggled with this issue for a long time... My solution was to modify the php.ini file, in the folder that contained the php script. This was important, as modifying the php.ini at the root did not resolve the problem (I have a php.ini in each folder for granular control). The relevant entries in my php.ini looked like this.... (the output_buffering is not likely needed for this issue)
我在这个问题上挣扎了很长时间......我的解决方案是修改包含 php 脚本的文件夹中的 php.ini 文件。这很重要,因为修改根目录下的 php.ini 并没有解决问题(我在每个文件夹中都有一个 php.ini 用于精细控制)。我的 php.ini 中的相关条目看起来像这样......(这个问题可能不需要 output_buffering)
output_buffering = On
upload_max_filesize = 20M
post_max_size = 21M
回答by Jerome Jr. Formentera
create php-file with:
使用以下命令创建 php 文件:
<?php
print shell_exec( 'whoami' );
?>
or
或者
<?php echo exec('whoami'); ?>
try the output in your web-browser. if the output is not your user example: www-datathen proceed to next step
在您的网络浏览器中尝试输出。 如果输出不是您的用户示例:www-data然后继续下一步
open as root:
以 root 身份打开:
/etc/apache2/envvars
/etc/apache2/envvars
look for these lines:
寻找这些行:
export APACHE_RUN_USER=user-name
export APACHE_RUN_GROUP=group-name
导出 APACHE_RUN_USER=用户名
export APACHE_RUN_GROUP=组名
example:
例子:
export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data
导出 APACHE_RUN_USER=www-data
导出 APACHE_RUN_GROUP=www-data
where:
在哪里:
username = your username that has access to the folder you are using group = group you've given read+write+execute access
用户名 = 有权访问您正在使用的文件夹的用户名组 = 您已授予读+写+执行访问权限的组
change it to:
将其更改为:
export APACHE_RUN_USER="username"
export APACHE_RUN_GROUP="group"
导出 APACHE_RUN_USER="用户名"
导出 APACHE_RUN_GROUP="组"
if your user have no access yet:
如果您的用户还没有访问权限:
sudo chmod 775 -R "directory of folder you want to give r/w/x access"
sudo chmod 775 -R “您要授予 r/w/x 访问权限的文件夹目录”
回答by Jorge Polo Contreras Paredes
My problem was selinux...
我的问题是selinux...
Go sestatus
转到 sestatus
If Current mode: enforcing
如果 当前模式:强制执行
Then chcon -R -t httpd_sys_rw_content_t /var/www/html
然后 chcon -R -t httpd_sys_rw_content_t /var/www/html
回答by Abhishek Sinha
I was also facing the same issue for 2 days but now finally it is working.
我也遇到了同样的问题 2 天,但现在终于可以正常工作了。
Step 1 : create a php script file with this content.
第 1 步:使用此内容创建一个 php 脚本文件。
<?php
echo 'username : ' . `whoami`;
phpinfo();
?>
note down the username. note down open_basedir under core section of phpinfo. also note down upload_tmp_dir under core section of phpinfo.
记下用户名。记下 phpinfo 核心部分下的 open_basedir。还要记下phpinfo核心部分下的upload_tmp_dir。
Here two things are important , see if upload_tmp_dir value is inside one of open_basedir directory. ( php can not upload files outside open_basedir directory ).
这里有两件事很重要,看看upload_tmp_dir 值是否在open_basedir 目录之一内。( php 不能上传 open_basedir 目录之外的文件)。
Step 2 : Open terminal with root access and go to upload_tmp_dir location. ( In my case "/home/admin/tmp". )
第 2 步:以 root 访问权限打开终端并转到 upload_tmp_dir 位置。(在我的例子中是“/home/admin/tmp”。)
=> cd /home/admin/tmp
But it was not found in my case so I created it and added chown for php user which I get in step 1 ( In my case "admin" ).
但是在我的情况下没有找到它,所以我创建了它并为我在步骤 1 中获得的 php 用户添加了 chown(在我的情况下为“admin”)。
=> mkdir /home/admin/tmp
=> chown admin /home/admin/tmp
That is all you have to do to fix the file upload problem.
这就是解决文件上传问题所需要做的全部工作。
回答by Tony_H
In my case, it was the open_basedir which was defined. I commented it out (default) and my issue was resolved. I can now set the upload directory anywhere.
就我而言,它是定义的 open_basedir。我将其注释掉(默认)并解决了我的问题。我现在可以在任何地方设置上传目录。

