简体   繁体   English

PHP的较大的文件上传问题

[英]larger file upload problem with php

I need to upload a csv file to a server. 我需要将csv文件上传到服务器。 works fine for smaller files but when the file is 3-6 meg its not working. 适用于较小的文件,但当文件大小为3-6兆时,则无法正常工作。

$allowedExtensions = array("csv");
         foreach ($_FILES as $file) { 
            if ($file['tmp_name'] > '') { 
             if (!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) { 

              die($file['name'].' is an invalid file type!<br/>'. '<a href="javascript:history.go(-1);">'. '&lt;&lt Go Back</a>'); 

             }
             if (move_uploaded_file($file['tmp_name'], $uploadfile)) {
                    echo "File is valid, and was successfully uploaded.\n";
                } else {
                    echo "Possible file upload attack!\n";
              }

             echo "File has been uploaded";



            } 

//upload form //上传表格

 <form name="upload" enctype="multipart/form-data" action="<? echo $_SERVER['php_self'];?>?action=upload_process" method="POST">
                    <!-- MAX_FILE_SIZE must precede the file input field -->
                    <input type="hidden" name="MAX_FILE_SIZE" value="31457280" />
                    <!-- Name of input element determines name in $_FILES array -->
                    Send this file: <input name="userfile" type="file" />
                    <input type="submit" value="Send File" />
            </form>

I have also added this to htaccess 我也将此添加到htaccess

php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 200
php_value max_input_time 200

Where am i going wrong? 我要去哪里错了?

What is the value of $_FILES['userfile']['error']? $ _FILES ['userfile'] ['error']的值是什么? Have a look here: 在这里看看:

http://php.net/manual/en/features.file-upload.errors.php http://php.net/manual/zh/features.file-upload.errors.php

Also, whats with this: 此外,这是怎么回事:

if ($file['tmp_name'] > '') { 

I don't think that's very healthy. 我认为那不是很健康。

Just some suggestions to your code 对您的代码的一些建议

  • ($file['tmp_name'] > '') should be something like ( ! empty($file['tmp_name'])) ($file['tmp_name'] > '')应该类似于( ! empty($file['tmp_name']))
  • echoing "possible file upload attack" won't help anyone. 回应“可能的文件上传攻击”不会帮助任何人。 If you believe it is a possible attack, log it to a file. 如果您认为这是可能的攻击,请将其记录到文件中。
  • The form action attribute, the $_SERVER['php_self'] should be capitalised because it is a constant, ie $_SERVER['PHP_SELF'] . 表单操作属性$_SERVER['php_self']应该大写,因为它是一个常量,即$_SERVER['PHP_SELF']

检查您的apache错误日志,该错误应该存在吗?

Check your php.ini file and see if upload_max_filesize in there is set higher than 3 MB, I don't know if the .htaccess has precedence over the php.ini. 检查您的php.ini文件,看看其中是否设置了upload_max_filesize大于3 MB,我不知道.htaccess是否优先于php.ini。

The default upload limit on debian php5 is 2MB if I recall correctly, but I am not sure what system you are running on. 如果我没记错的话,debian php5的默认上传限制为2MB,但是我不确定您在哪个系统上运行。

You can also check the php values if you create a file eg info.php and put it in the same directory as your "problem php script". 如果创建文件( 例如info.php) 并将其与“问题php脚本”放在同一目录中,则还可以检查php值

The file content should look like this <?php phpinfo(); ?> 文件内容应如下所示: <?php phpinfo(); ?> <?php phpinfo(); ?>

This will give you all php relevant values referring to the directory you are in , hope it helps. 这将为您提供所有与php有关的值,这些值涉及您所在的目录 ,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM