我创建了带有视频大小和类型验证的上传文件。只允许使用 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