如何让用户将各种视频文件上传到public_html?我正在使用此代码,但它只允许我上传mp4视频而不是mov或3gp。我怎样才能让它运作
这是我的代码:
<?php
include('config.php');
$allowedExts = array("jpg", "jpeg", "gif", "png", "mov", "mp4", "3gp", "ogg");
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if ((($_FILES["file"]["type"] == "video/mov")
|| ($_FILES["file"]["type"] == "video/mp4")
|| ($_FILES["file"]["type"] == "video/3gp")
|| ($_FILES["file"]["type"] == "video/ogg")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 999999999)
&& 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"] / 999999999) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
// If file has uploaded successfully, store its name in data base
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
答案 0 :(得分:1)
使用正确的MIME类型名称:
Video Type Extension MIME Type
Flash .flv video/x-flv
MPEG-4 .mp4 video/mp4
iPhone Index .m3u8 application/x-mpegURL
iPhone Segment .ts video/MP2T
3GP Mobile .3gp video/3gpp
QuickTime .mov video/quicktime
A/V Interleave .avi video/x-msvideo
Windows Media .wmv video/x-ms-wmv
(资料来源:http://www.encoding.com/help/article/correct_mime_types_for_serving_video_files)
答案 1 :(得分:1)
您可以查看上传文件的type
项中报告的MIME_TYPE;
而不是检查所有视频的个人类型匹配:
所以,改变这个:
if ((($_FILES["file"]["type"] == "video/mov")
|| ($_FILES["file"]["type"] == "video/mp4")
|| ($_FILES["file"]["type"] == "video/3gp")
|| ($_FILES["file"]["type"] == "video/ogg")
|| ($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 999999999)
&& in_array($extension, $allowedExts))
这样的RegEx
匹配:
if(preg_match('#^video/.*$#',$_FILES["file"]["type"]) && ($_FILES["file"]["size"] < 999999999)
答案 2 :(得分:0)
从php.ini文件增加允许的文件大小,然后检查默认情况下是8MB
检查$ _FILES [“file”] [“error”]中的错误代码。值1表示文件大于允许的文件大小(来自php.ini)。