Home  >  Article  >  Backend Development  >  PHP $_FILES 上传文件,在Windows下通常,但在Ubuntu下无法上传文件

PHP $_FILES 上传文件,在Windows下通常,但在Ubuntu下无法上传文件

WBOY
WBOYOriginal
2016-06-13 12:00:28846browse

PHP $_FILES 上传文件,在Windows下正常,但在Ubuntu下无法上传文件
以下源码在Windows Apache下可正常上传文件,但在Ubuntu Linux下不会报错,但上传文件无法到指定目录。
请教各位如何解决。
谢谢!



$ErrorMessage=null;
$Message=null;
if(!empty($_FILES['fileField']['name'])){
if($_FILES['fileField']['error']>0){
$ErrorMessage='Error: '.$_FILES['fileField']['error'];
}else{

if (is_uploaded_file($_FILES['fileField']['tmp_name'])){
//转移文件
move_uploaded_file($_FILES['fileField']['tmp_name'],
 '/var/www' .'/'. $_FILES['fileField']['name']);

$Message='上传成功!';
}else{
$ErrorMessage='Error: '.'上传失败!';
}
}

}
?>
------解决方案--------------------
估计是权限问题吧 上传目录改成777测试一下
------解决方案--------------------
windows 是单用户操作系统,很少会遇到权限问题。即使是在服务器上,由于有微软强大的团队,出现权限问题也是极少的
linux 是多用户操作系统(即便只有你一个人在用),所以权限问题无处不在。开源项目尚未技术支撑较弱,至今也没有出现简洁快捷的工具

move_uploaded_file 会判断是否为上传文件, is_uploaded_file 多次一举
而 move_uploaded_file 遇到目录不存在、不可写、空间不足的时候会返回错误状态
而你不加判别就 $Message='上传成功!'; 是不妥当的

------解决方案--------------------
在程序最頂加上
ini_set('display_errors','on');
error_reporting(E_ALL);

運行后就知道是什麼錯了,一般都是文件夾權限問題

可在linux終端使用命令
設置文件夾組用戶是www-data
chgrp www-data folder

然後設置組權限
chmod g+rw folder 

------解决方案--------------------
打开错误提示看看
------解决方案--------------------
'/var/www' .'/'. $_FILES['fileField']['name']
贴出目录文件权限

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn