5

我创建了带有视频大小和类型验证的上传文件。只允许使用 webm、mp4 和 ogv 文件类型,最大文件大小为 2gb。我的PHP代码:

if (isset($_POST['submit']))
{
    $file_name = $_FILES['file']['name'];
    $file_type = $_FILES['file']['type'];
    $file_size = $_FILES['file']['size'];

    $allowed_extensions = array("webm", "mp4", "ogv");
    $file_name_temp = explode(".", $file_name);
    $extension = end($file_name_temp);
    
    $file_size_max = 2147483648;
    if (!empty($file_name))
    {
        if (($file_type == "video/webm") || ($file_type == "video/mp4") || ($file_type == "video/ogv") &&
            ($file_size < $file_size_max) && in_array($extension, $allowed_extensions))
        {
            if ($_FILES['file']['error'] > 0)
            {
                echo "Unexpected error occured, please try again later.";
            } else {
                if (file_exists("secure/".$file_name))
                {
                    echo $file_name." already exists.";
                } else {
                    move_uploaded_file($_FILES["file"]["tmp_name"], "secure/".$file_name);
                    echo "Stored in: " . "secure/".$file_name;
                }
            }
        } else {
            echo "Invalid video format.";
        }
    } else {
        echo "Please select a video to upload.";
    }
}

我的html代码:

<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br />
<input type="submit" name="submit" value="Submit">
</form>

我总是收到“无效的视频格式”。我从 flowplayer 网站下载了 webm、mp4 和 ogv 视频文件来测试我的小上传脚本。

http://stream.flowplayer.org/bauhaus/624x260.webm
http://stream.flowplayer.org/bauhaus/624x260.mp4
http://stream.flowplayer.org/bauhaus/624x260.ogv
4

2 回答 2

6

您的扩展程序未正确验证.. 试试这个

if (isset($_POST['submit']))
{
    $file_name = $_FILES['file']['name'];
    $file_type = $_FILES['file']['type'];
    $file_size = $_FILES['file']['size'];

    $allowed_extensions = array("webm", "mp4", "ogv");
    $file_size_max = 2147483648;
    $pattern = implode ($allowed_extensions, "|");

    if (!empty($file_name))
    {    //here is what I changed - as you can see above, I used implode for the array
        // and I am using it in the preg_match. You pro can do the same with file_type,
        // but I will leave that up to you
        if (preg_match("/({$pattern})$/i", $file_name) && $file_size < $file_size_max)
        {
            if (($file_type == "video/webm") || ($file_type == "video/mp4") || ($file_type == "video/ogv"))
            {
                if ($_FILES['file']['error'] > 0)
                {
                    echo "Unexpected error occured, please try again later.";
                } else {
                    if (file_exists("secure/".$file_name))
                    {
                        echo $file_name." already exists.";
                    } else {
                        move_uploaded_file($_FILES["file"]["tmp_name"], "secure/".$file_name);
                        echo "Stored in: " . "secure/".$file_name;
                    }
                }
            } else {
                echo "Invalid video format.";
            }
        }else{
            echo "where is my mojo?? grrr";
        }
    } else {
        echo "Please select a video to upload.";
    }
}
于 2013-10-21T21:25:20.397 回答
1

function fileSelected() {
            var inputs = document.getElementsByClassName('myclass');
            var input = inputs[0];
            var file = input.files[0];
            var name = file.name;
            var size = file.size;
           	var type = file.type;
 			//alert("type: "+type);
 			if(type!="video/mp4")
 			{
 				alert("NOT SUITABLE EXTENSION SELECTED");
 				
 			}
 			
            filesArray.push({ name: name, size: size });
        }

<input type="file" accept="video/*" class=" form-control btn btn-primary myclass" required name="file1" id="file1" onchange="fileSelected();">

于 2018-09-27T06:33:31.110 回答