无法上传视频。 $ _FILE是空的

时间:2016-07-20 04:34:22

标签: php video file-upload

我不允许上传视频时遇到错误。我添加print_r($_FILES)来基本检查文件数组中的内容。请帮助!!!

这是我的PHP代码:         

if(isset($_POST["submit"]))
{
    $allowedExts = array( "mp4", "wma");
    $target_dir = "temp/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $vidoeFileType = pathinfo($target_file,PATHINFO_EXTENSION);

    if (($_FILES["fileToUpload"]["type"] == "video/mp4")  && ($_FILES["fileToUpload"]["size"] < 5120000) && in_array($vidoeFileType, $allowedExts))
    {
        $uploadOk = 1;
    }else{
        echo "Not upload | Name: ".$_FILES["fileToUpload"]["name"] ." | Type: ".$_FILES["fileToUpload"]["type"] . "| Size: ". $_FILES["fileToUpload"]["size"] . "<br/>";
print_r($_FILES);
        $uploadOk = 0;
    }

    //check file is error or not
    // if ($_FILES["fileToUpload"]["error"] > 0)
 //    {
 //     echo "Return Code: " . $_FILES["fileToUpload"]["error"] . "<br />";
 //    }


    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }

    // Check file size
    if ($_FILES["fileToUpload"]["size"] > 5120) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }


    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

            echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }


}else{



?>



<!DOCTYPE html>
<html>
<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input name="MAX_FILE_SIZE" value="100000000000000"  type="hidden"/>
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload" name="submit">
</form>

</body>
</html>

<?php

}

?>

输出:

Not upload | Name: aiu.mp4 | Type: | Size: 0
Array ( [fileToUpload] => Array ( [name] => aiu.mp4 [type] => [tmp_name] => [error] => 1 [size] => 0 ) ) Sorry, your file was not uploaded.

它只显示视频名称和错误。

3 个答案:

答案 0 :(得分:0)

[error] => 1

此错误表示文件大小超过了PHP的最大文件大小限制。 用php.ini增加它。

upload_max_filesize = 7M

您还应该确保您的post_max_size大于(或等于)upload_max_filesize,因为文件上传次数与总POST大小相对应。

根据文件的大小,您还可以增加memory_limit

答案 1 :(得分:0)

默认情况下,PHP设置为允许上传大小为2MB或更小的文件。尝试在php.ini中增加以下值,例如:memory_limit = 32M upload_max_filesize = 24M post_max_size = 32M。如下所示:

 <?php
// overwrite default uload max file size
ini_set('upload_max_filesize', '50M');

if(isset($_POST["submit"]))
{
    $allowedExts = array( "mp4", "wma");
    $target_dir = "temp/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $vidoeFileType = pathinfo($target_file,PATHINFO_EXTENSION);

    if (($_FILES["fileToUpload"]["type"] == "video/mp4")  && ($_FILES["fileToUpload"]["size"] < 5120000) && in_array($vidoeFileType, $allowedExts))
    {
        $uploadOk = 1;
    }else{
        echo "Not upload | Name: ".$_FILES["fileToUpload"]["name"] ." | Type: ".$_FILES["fileToUpload"]["type"] . "| Size: ". $_FILES["fileToUpload"]["size"] . "<br/>";
print_r($_FILES);
        $uploadOk = 0;
    }

    //check file is error or not
    // if ($_FILES["fileToUpload"]["error"] > 0)
 //    {
 //     echo "Return Code: " . $_FILES["fileToUpload"]["error"] . "<br />";
 //    }


    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }

    // Check file size
    if ($_FILES["fileToUpload"]["size"] > $_POST['MAX_FILE_SIZE']) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }


    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

            echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }


}else{



?>



<!DOCTYPE html>
<html>
<body>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input name="MAX_FILE_SIZE" value="100000000000000"  type="hidden"/>
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload" name="submit">
</form>

</body>
</html>

<?php

}

?>

希望这能解决您的问题。

答案 2 :(得分:-1)

解决此问题的步骤:

**************************我在这里使用Xamp **************** **************

  1. 转到Xampp-&gt; PHP-&gt; Php.config
  2. 将upload_max_filesize和post_max_size的值从2M更改为50M
  3. 在php.ini-Development
  4. 中做类似的更改
  5. 在php.ini-Production中进行类似的更改。
  6. 注* 确保重新启动服务器。

    由于