大家好我用视频大小和类型验证创建了我的上传文件。仅允许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
答案 0 :(得分: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.";
}
}
答案 1 :(得分:0)
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();">