php 文件上传失败

Posted nekoooo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 文件上传失败相关的知识,希望对你有一定的参考价值。

使用OSX系统,在使用MAMP Pro作为虚拟服务器,并使用php作为后端语言进行文件上传,从临时文件夹拷贝文件的方法为

move_uploaded_file

代码如下:

 1 if($_FILES[‘file‘][‘error‘]>0){
 2     echo ‘file error‘;
 3 }else{
 4     echo ‘upload:‘.$_FILES[‘file‘][‘name‘].‘<br>‘;
 5     echo ‘type:‘.$_FILES[‘file‘][‘type‘].‘<br>‘;
 6     echo ‘size:‘.$_FILES[‘file‘][‘size‘].‘<br>‘;
 7     echo ‘stored in:‘.$_FILES[‘file‘][‘tmp_name‘].‘<br>‘;
 8     if (file_exists("upload/" . $_FILES[‘file‘]["name"]))
 9           {
10           echo $_FILES[‘file‘]["name"] . " already exists. ";
11           }
12         else
13           {
14           move_uploaded_file($_FILES[‘file‘]["tmp_name"],
15           "upload/" . $_FILES[‘file‘]["name"]);
16           echo "Stored in: " . "/Applications/MAMP/htdocs/learnPhp/upload/" . $_FILES[‘file‘]["name"];
17           }
18 }

运行报错:

[23-Mar-2016 22:01:39] PHP Warning:  move_uploaded_file(upload/visualize_pnas.py) [<a href=‘function.move-uploaded-file‘>function.move-uploaded-file</a>]: failed to open stream: Permission denied in /Applications/MAMP/htdocs/learnPhp/upload.php on line 18
[23-Mar-2016 22:01:39] PHP Warning:  move_uploaded_file() [<a href=‘function.move-uploaded-file‘>function.move-uploaded-file</a>]: Unable to move ‘/Applications/MAMP/tmp/php/phpBeg1Al‘ to ‘upload/visualize_pnas.py‘ in /Applications/MAMP/htdocs/learnPhp/upload.php on line 18

主要报错为

failed to open stream: Permission denied

意思是,Php没有文件夹写权限,因为只有owner才有写权限。我们需要把这个文件夹的owner改为httpd的owner:

$ ps aux | grep httpd

结果为:

_www            68160   0.0  0.0  2487444    100   ??  S    10:10下午   0:00.00 /Applications/MAMP/Library/bin/httpd -f /Library/Application Support/appsolute/MAMP PRO/conf/httpd.conf -k start
_www            68159   0.0  0.0  2487444    108   ??  S    10:10下午   0:00.01 /Applications/MAMP/Library/bin/httpd -f /Library/Application Support/appsolute/MAMP PRO/conf/httpd.conf -k start
_www            68154   0.0  0.0  2487444    108   ??  S    10:10下午   0:00.03 /Applications/MAMP/Library/bin/httpd -f /Library/Application Support/appsolute/MAMP PRO/conf/httpd.conf -k start
_www            66607   0.0  0.0  2487444    108   ??  S     8:34下午   0:00.09 /Applications/MAMP/Library/bin/httpd -f /Library/Application Support/appsolute/MAMP PRO/conf/httpd.conf -k start
_www            66301   0.0  0.0  2487444    108   ??  S     8:15下午   0:00.06 /Applications/MAMP/Library/bin/httpd -f /Library/Application Support/appsolute/MAMP PRO/conf/httpd.conf -k start
_www            66294   0.0  0.0  2487444    108   ??  S     8:15下午   0:00.04 /Applications/MAMP/Library/bin/httpd -f /Library/Application Support/appsolute/MAMP PRO/conf/httpd.conf -k start
_www            66293   0.0  0.0  2487444    108   ??  S     8:15下午   0:00.06 /Applications/MAMP/Library/bin/httpd -f /Library/Application Support/appsolute/MAMP PRO/conf/httpd.conf -k start
_www            66292   0.0  0.0  2487444    108   ??  S     8:15下午   0:09.47 /Applications/MAMP/Library/bin/httpd -f /Library/Application Support/appsolute/MAMP PRO/conf/httpd.conf -k start
_www            66291   0.0  0.0  2487444    108   ??  S     8:15下午   0:07.27 /Applications/MAMP/Library/bin/httpd -f /Library/Application Support/appsolute/MAMP PRO/conf/httpd.conf -k start
_www            66290   0.0  0.0  2487444    108   ??  S     8:15下午   0:00.08 /Applications/MAMP/Library/bin/httpd -f /Library/Application Support/appsolute/MAMP PRO/conf/httpd.conf -k start
_www            66097   0.0  0.0  2472172     76   ??  S     8:15下午   0:00.00 /Applications/MAMP/Library/bin/

第一列为_www,所以,httpd的owner是_www。

接下来修改文件夹的owner:

$ sudo chown _www /Applications/MAMP/htdocs/learnPhp/upload/ 

这样就可以啦!我们现在验证一下:

看看这个文件夹的owner:

进入文件父目录:

$ cd /Applications/MAMP/htdocs/learnPhp/

查看子目录的owner:

$ ls -l 

结果为:

drwxr-xr-x  3 Yellow  admin  102  3 23 20:27 files
-rw-r--r--  1 Yellow  admin  277  3 23 19:42 index.html
drwxr-xr-x  4 _www    admin  136  3 23 23:07 upload
-rw-r--r--  1 Yellow  admin  792  3 24 10:50 upload.php
-rw-r--r--  1 Yellow  admin  275  3 23 21:10 welcome.php

我们可以看到upload文件夹的owner已经是_www了,不过,之后要是用户常规操作修改文件夹的时候系统就提示需要输入管理员密码了。

以上是关于php 文件上传失败的主要内容,如果未能解决你的问题,请参考以下文章

php为啥上传图片会失败

php 文件上传失败

php多文件上传实现代码

大文件上传失败的php

PHP文件上传失败移动文件

解决PHP move_uploaded_file函数移动图片失败