【服务器配置】php move_uploaded_file文件移动失败

来源:互联网 发布:孙中山萝莉控 知乎 编辑:程序博客网 时间:2024/05/21 10:37

在学习php的过程中,很多时候在本地windows的环境下跑通的代码上传到云服务器(我使用的云主机的系统是Ubuntu)上的时候发生一些自己不敢相信的事情,相信很多小伙伴和我有过一样的经历。

最近在做上传图片这样一个实验的时候,这种情况再次发生了,很痛苦,在网上找到的答案都没能很好的解决问题,只好静下心来想一下问题可能出现在什么位置。


出现这种问题可能是因为:
1、权限不够
2、文件太大(默认只能上传2M以内的文件)
3、文件路径不对


首先可以很快的将2排除,因为在写php程序的时候我将这个问题就给过滤掉了。

然后再考虑3这种可能性。将tmp 和目标路径打印出来的时候发现,tmp文件中一直都没有文件,目标路径下更是没有任何东西。

然后我就想着去查看php.ini文件中的描述

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;


; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On


; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir = "/var/www/tmp"


; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 4M


; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20

     我对描红的位置进行了修改将tmp文件夹配置到/var/www/tmp这个位置结果发现这个tmp目录下依旧不会出现我想得到的.tmp文件,然后就将3的可能性给排除了,紧接着我对/var/www/tmp的权限进行了修改,修改为所有人可读可写可操作,对目标文件夹进行了同样的操作之后重启apache服务器


sudo /etc/init.d/apache2 restart


问题得到了解决。下面贴出文件上传的代码


<?php function uploadFile($fileInfo,$uploadPath = 'uploads',$flag=true,$allowExt=array('jpeg','jpg','gif','png'),$maxSize = 2097152){// 判断错误号if ($fileInfo ['error'] > 0) {switch ($fileInfo ['error']) {case 1 :$mes = '上传文件超过了PHP配置文件中upload_max_filesize选项的值';break;case 2 :$mes = '超过了表单MAX_FILE_SIZE限制的大小';break;case 3 :$mes = '文件部分被上传';break;case 4 :$mes = '没有选择上传文件';break;case 6 :$mes = '没有找到临时目录';break;case 7 :case 8 :$mes = '系统错误';break;}echo ( $mes );return false;}$ext = pathinfo ( $fileInfo ['name'], PATHINFO_EXTENSION );if(!is_array($allowExt)){exit('系统错误');}// 检测上传文件的类型if (! in_array ( $ext, $allowExt )) {exit ( '非法文件类型' );}//$maxSize = 2097152; // 2M                  // 检测上传文件大小是否符合规范if ($fileInfo ['size'] > $maxSize) {exit ( '上传文件过大' );}//检测图片是否为真实的图片类型//$flag=true;if($flag){if(!getimagesize($fileInfo['tmp_name'])){exit('不是真实图片类型');}}// 检测文件是否是通过HTTP POST方式上传上来if (! is_uploaded_file ( $fileInfo ['tmp_name'] )) {exit ( '文件不是通过HTTP POST方式上传上来的' );}//$uploadPath = 'uploads';if (! file_exists ( $uploadPath )) {mkdir ( $uploadPath, 0777, true );chmod ( $uploadPath, 0777 );}$uniName = md5 ( uniqid ( microtime ( true ), true ) ) . '.' . $ext;$destination = $uploadPath . '/' . $uniName;if (! move_uploaded_file ( $fileInfo ['tmp_name'], $destination )) {exit ( '文件移动失败' );}// return array(// 'newName'=>$destination,// 'size'=>$fileInfo['size'],// 'type'=>$fileInfo['type']// );//返回文件的路径return $destination;}



<?phprequire_once 'upload.func.php';header("Content-type:text/html;charset=utf8");$fileinfo = $_FILES['filename'];echo uploadFile($fileinfo);

<html>    <head>        <title>上传文件</title>    </head>    <body>        <form action="uplodefile.php" method="post" enctype="multipart/form-data">            <input type="file" name="filename"><br>            <input type="submit" name="上传">        </form>    </body></html>




1 0
原创粉丝点击