我有这段代码来处理上传的多张图片。我也想上传和处理视频。如何检查上传的文件是图像还是视频?请注意,我想处理多个视频文件。
<?php
include('selectdb2.php');
if($_SERVER['REQUEST_METHOD'] == "POST")
if(isset($_FILES['file']))
{
$count = 0;
$errors= array();
foreach($_FILES['file']['tmp_name'] as $key => $tmp_name )
{
$file_name = $key.$_FILES['file']['name'][$key];
$file_size =$_FILES['file']['size'][$key];
$file_tmp =$_FILES['file']['tmp_name'][$key];
$file_type=$_FILES['file']['type'][$key];
$enc_id= $_POST['form_id'].$_POST['name3'];
$md5folder = md5($enc_id);
$upload_path ="uploads/".$md5folder;
if(!is_dir($upload_path))
{
mkdir($upload_path, 0777, true);
}
if(empty($errors)==true)
{
move_uploaded_file($file_tmp,$upload_path.'/'.$file_name);
}
}
.....
.....
?>
您必须签出文件名的扩展名,因为您不能信任上传文件的内容类型。它可以被客户端欺骗。因此,您为图像和视频的扩展列表创建了数组列表 - 也称为白名单。
例如:
$allowed = array('gif','png' ,'jpg');
$filename = $_FILES['video_file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed) ) {
echo 'error';
}