【问题标题】:PHP unable to take files of specified file sizePHP无法获取指定文件大小的文件
【发布时间】:2014-04-08 18:28:45
【问题描述】:

我想通过浏览器获取最大 20 Mb 的文本文件。

我的php代码是:

$allowedExts = array("txt");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
$file_name=$_FILES["file"]["name"];

if ((
   ($_FILES["file"]["type"] == "text/plain"))
&& ($_FILES["file"]["size"] < 5243000)
&& in_array($extension, $allowedExts))
{ echo "Uploaded : " . $_FILES["file"]["name"] . "<br>";}

else {echo "Invalid file";}

注意:我的 php ini 设置是:

ini_set("upload_max_filesize", "20M");
ini_set("memory_limit", "32M");
ini_set("post_max_size","20M");  

但我的浏览器抛出:无效文件。即使是 3 Mb 文件(小于 2 Mb 也可以正常工作
扩展名仅为 .txt,
我确信扩展不是问题。

可能的解决方案是什么?

更新:
我的 PHP.ini 说:上传文件的最大允许大小。 upload_max_filesize = 20M
但我的 php info();还是说:upload_max_filesize local 2M master 2M

【问题讨论】:

  • 试试print_r($_FILES);
  • var_dump($_FILES["file"]);检查$_FILES的实际值
  • 它给出:array(5) { ["name"]=> string(5) "a.txt" ["type"]=> string(0) "" ["tmp_name"] => string(0) "" ["error"]=> int(1) ["size"]=> int(0) } 无效文件
  • @AshwiniAgarwal print_r($_FILES);给出: Array ( [file] => Array ( [name] => a.txt [type] => [tmp_name] => [error] => 1 [size] => 0 ) ) 无效文件

标签: php


【解决方案1】:

您无法使用ini_set 更改upload_max_filesize 变量(如您在此列表中所见:List of php.ini directives)。改为更改 php.ini 配置。

【讨论】:

【解决方案2】:

使用 print_r($_FILES) 确保以下内容

1) $_FILES["file"]["type"] 与 "text/plain" 完全匹配

2) $_FILES["file"]["size"] 小于 5243000

3) $extension 值为 txt (在这种情况下大写很重要)

4) 在 ini_set 之后使用 ini_get 检查所有设置以确保设置已更改

5) 如果还可以,请在此处输入您的 print_r($_FILES) 输出

【讨论】:

  • 它给出:array(5) { ["name"]=> string(5) "a.txt" ["type"]=> string(0) "" ["tmp_name"] => string(0) "" ["error"]=> int(1) ["size"]=> int(0) } 无效文件
  • 请执行 print_r 而不是 var_dump
  • print_r($_FILES);给出: Array ( [file] => Array ( [name] => a.txt [type] => [tmp_name] => [error] => 1 [size] => 0 ) ) 无效文件
  • 您使用什么服务器? nginx?
  • ini_set('max_input_time', 200); ini_set("upload_max_filesize", "8M");请在表单加载和表单提交的地方使用这些代码行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-24
  • 1970-01-01
  • 2018-05-30
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
  • 1970-01-01
相关资源
最近更新 更多