您的位置:首页 > 编程语言 > PHP开发

PHP上传文件大小限制解决方法

2014-01-09 16:42 701 查看
1. 在php.ini中,做如下修改:

file_uploads = on

upload_tmp_dir = /home/upload

upload_max_filesize = 4000M

post_max_size = 4000M

一般地,设置好上述四个参数后,上传<=8M的文件是不成问题,在网络正常的情况下。

但如果要上传>8M的大体积文件,只设置上述四项还一定能行的通。除非你的网络真有100M/S的上传高速,否则你还得关心关心下面的参数:

max_execution_time = 600

max_input_time = 600

memory_limit = 128M



2. 在要确认上传的 <form> 里加入以下行:

<input type="hidden" name="MAX_FILE_SIZE" value="4000000000">



3. $_FILES["userfile"]["error"]返回值说明:

$_FILES['teacher_pic']['error'] = 1 文件大小超过了PHP.ini中的文件限制

$_FILES['teacher_pic']['error'] = 2 文件大小超过了浏览器限制

$_FILES['teacher_pic']['error'] = 3 文件部分被上传

$_FILES['teacher_pic']['error'] = 4 没有找到要上传的文件

$_FILES['teacher_pic']['error'] = 5 服务器临时文件夹丢失

$_FILES['teacher_pic']['error'] = 6 文件写入到临时文件夹出错

$_FILES['teacher_pic']['error'] = -1 末充许的类型

$_FILES['teacher_pic']['error'] = -2 文件过大,上传文件不能超过{$this->maxSize}个字节

$_FILES['teacher_pic']['error'] = -3 上传失败

$_FILES['teacher_pic']['error'] = -4 建立存放上传文件目录失败,请重新指定上传目录

$_FILES['teacher_pic']['error'] = -5 必须指定上传文件的路径



4. 实例如下:请参考 http://www.w3school.com.cn/php/php_file_upload.asp

"upload_file.html":

<html>

<head>

<title>Upload File</title>

</head>

<body>

<form action="upload_file.php" method="post" enctype="multipart/form-data">

<!-- MAX_FILE_SIZE must precede the file input field -->

<input type="hidden" name ="MAX_FILE_SIZE" value="4000000000"/>

<!-- Name of input element determines name in $_FILES array -->

<label for="file">Filename:</label>

<input type="file" name="userfile" id="userfile" />

<br />

<!-- Submit -->

<input type="submit" name="submit" value="Submit" />

</form>

</body>

</html>

"upload_file.php":

<?php

if ($_FILES["userfile"]["error"] > 0)

{

echo "Error: " . $_FILES["userfile"]["error"] . "<br />";

}

else

{

echo "Upload: " . $_FILES["userfile"]["name"] . "<br />";

echo "Type: " . $_FILES["userfile"]["type"] . "<br />";

echo "Size: " . ($_FILES["userfile"]["size"] / 1024) . " Kb<br />";

echo "Stored in: " . $_FILES["userfile"]["tmp_name"] . "<br />";

if (($_FILES["userfile"]["type"] == "application/x-linguist") ||

($_FILES["userfile"]["type"] == "video/vnd.dlna.mpeg-tts"))

{

move_uploaded_file ($_FILES["userfile"]["tmp_name"], "../upload/video/" . $_FILES["userfile"]["name"]);

echo "Stored in: " . "video/" . $_FILES["userfile"]["name"];

}

else if(($_FILES["userfile"]["type"] == "image/jpeg") ||

($_FILES["userfile"]["type"] == "image/pjpeg"))

{

move_uploaded_file ($_FILES["userfile"]["tmp_name"], "../upload/picture/" . $_FILES["userfile"]["name"]);

echo "Stored in: " . "picture/" . $_FILES["userfile"]["name"];

}

else

{

move_uploaded_file ($_FILES["userfile"]["tmp_name"], "../upload/others/" . $_FILES["userfile"]["name"]);

echo "Stored in: " . "others/" . $_FILES["userfile"]["name"];

}

}

?>

5. 经过反复测试,发现以这种方式上传文件最大可到2G,如果再大的文件就会失败了。而且上传大文件使用http的方式并不是最佳。如果一定要用,可以 Flash8 + PHP。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: