【问题标题】:Uploading mp4 files using PHP使用 PHP 上传 mp4 文件
【发布时间】:2014-06-23 09:48:05
【问题描述】:

我可以使用 PHP 上传脚本上传 png/jpegs/图像,但无法在我的本地服务器上上传 mp4 文件。脚本没有显示任何错误。

<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
//include authentication here/ Gmail is good solution for now
//check if it's not allowing any other extenstion other than MP4
$allowedExts = array("gif", "jpeg", "jpg", "png","mp4");
$temp = explode(".", $_FILES["file"]["name"]);
print_r($_FILES["file"]["type"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
|| ($_FILES["file"]["type"] == "video/mp4"))
&& ($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts)) {
  if ($_FILES["file"]["error"] > 0) {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
  } else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    if (file_exists("uploads/" . $_FILES["file"]["name"])) {
      echo $_FILES["file"]["name"] . " already exists. ";
    } else {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
    }
  }
} else {
  echo "Invalid file";
}
?>
enter code here
changed my code to this


 <?php
    ini_set('display_startup_errors',1);
    ini_set('display_errors',1);
    error_reporting(-1);
    //include authentication here/ Gmail is good solution for now
    //check if it's not allowing any other extenstion other than MP4
    $allowedExts = array("gif", "jpeg", "jpg", "png","mp4");
    $temp = explode(".", $_FILES["file"]["name"]);
    print_r($_FILES["file"]["type"]);
    $extension = end($temp);
    if (($_FILES["file"]["size"] < 200000)) {
      if ($_FILES["file"]["error"] > 0) {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
      } else {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
        if (file_exists("uploads/" . $_FILES["file"]["name"])) {
          echo $_FILES["file"]["name"] . " already exists. ";
        } else {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "uploads/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
        }
      }
    } else {
      echo "Invalid file";
    }
    ?>

还是一样的错误

视频/mp4无效文件

【问题讨论】:

  • print_r($_FILES["file"]["type"]); 的输出是什么?并非每个浏览器都会为同一个文件发送相同的 mime 类型。
  • print_r($_FILES["file"]["type"]) 的输出是视频/mp4。没有错误

标签: php video upload mp4


【解决方案1】:

如果您的代码没有任何错误,请确保增加 post_max_size AND load_max_filesize AND memory_limit

请参阅Handling file uploads: Common Pitfals,其中详细解释了这一点以及如何计算这些值。

【讨论】:

    【解决方案2】:

    您必须在 php.ini 中修改 POST 最大大小和文件上传大小

    如果你想禁用限制,它应该是这样的:

    ; Maximum size of POST data that PHP will accept.
    ; Its value may be 0 to disable the limit. It is ignored if POST data reading
    ; is disabled through enable_post_data_reading.
    ; http://php.net/post-max-size
    post_max_size=0
    

    当然还有文件限制:

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

    【讨论】:

      猜你喜欢
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 2017-06-30
      • 2018-07-25
      • 1970-01-01
      • 2012-03-22
      • 2016-03-20
      相关资源
      最近更新 更多