上传视频在PHP无法正常工作

时间:2014-12-09 09:10:45

标签: php mysql file-upload

我想在php上传视频这是我的代码

<?php

if(isset($_POST['submit']))
{

    $allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
    $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

    if ((($_FILES["file"]["type"] == "video/mp4")
    || ($_FILES["file"]["type"] == "audio/mp3")
    || ($_FILES["file"]["type"] == "audio/wma")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg"))

    && ($_FILES["file"]["size"] < 20000)
    && 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("store/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "store/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "store/" . $_FILES["file"]["name"];
          }
        }
      }

      else
      {
        echo "Invalid file";
      }
}     
?>

HTML CODE是:

<!DOCTYPE html>

<head>
<title></title>
</head>

<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file"><span>Filename:</span></label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

当我上传.mp4文件时显示消息: 文件无效

给我解决方案PLZ

3 个答案:

答案 0 :(得分:6)

Change it:
($_FILES["file"]["size"] < 20000)

To:
($_FILES["file"]["size"] < 2000000)

答案 1 :(得分:4)

因为您的代码:

$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

仅在点后返回扩展名。例如mp4mp3

你希望它是:

video/mp4audio/mp3

我认为你需要文件的mime type

mime_content_type($_FILES["file"]["name"]);

<强>解决方案:

更改

$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

$extension = mime_content_type($_FILES["file"]["name"]);

编辑:

更改

if ((($_FILES["file"]["type"] == "video/mp4")
    || ($_FILES["file"]["type"] == "audio/mp3")
    || ($_FILES["file"]["type"] == "audio/wma")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg"))

if (in_array($extension, $allowedExts))

答案 2 :(得分:2)

这是我的最终代码正在运行我已经改变了我的PHP代码

<?php


$target_dir = "store/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

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

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "mp3" && $imageFileType != "m4v" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $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.";
    }
}

?>

有了这个,我可以上传mp3和Mp4文件。 我已经在代码中更改了文件格式和文件大小的验证...