任何人都可以帮我上传多个静态名称的视频。我想逐个上传多个视频,这意味着用户可以上传任何类型的视频。
<form action="" method="post" enctype="multipart/form-data">
<input name="vv" type="file" id="file"/>
<input type="submit" value="UPLOAD" name="submit" id="upload" style=" background:#03d5fb; color:#00264a; font-size:26px; margin-top:4%; width:23%;" />
</form>
<?php
$resultt=(mysql_query("Select * From aa where email ='$_SESSION[login_user]'"));
$rows= (mysql_fetch_array($resultt, MYSQL_ASSOC));
$id=$rows['id'];
if (isset($_POST['submit'])) {
if(!is_dir("images/videos".$id.'/')) {
mkdir("images/videos".$id.'/');
$filess = $_FILES['vv']['name'];
$tmppath = $filess ['tmp_name'];
$target_dir = "images/videos".$id.'/';
$random=rand(0000, 9999);
move_uploaded_file($tmppath, $target_dir.'video'.$random.'.mp4');
echo "uploaded ";
}
else
{
$filess = $_FILES['vv']['name'];
$tmppath = $filess ['tmp_name'];
$target_dir = "images/videos".$id.'/';
$random=rand(0000, 9999);
move_uploaded_file($tmppath, $target_dir.'video'.$random.'.mp4');
echo "uploaded ";
}
}
?>
这是我尝试没有运气的示例代码。请给我一些解决方案。
感谢您的帮助。
答案 0 :(得分:2)
要允许用户选择和上传多个文件,以下是代码的一些简单提示
name="inputName[]"
multiple="multiple"
或multiple
。要知道
有关此内容的更多信息,请查看link $_FILES['inputElemName']['param'][index]
现在让我们来看看你的代码
<强> HTML 强>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="file[]" multiple="multiple"/>
<input type="submit" value="UPLOAD" name="submit" id="upload" style=" background:#03d5fb; color:#00264a; font-size:26px; margin-top:4%; width:23%;" />
</form>
<强> PHP 强>
<?php
if (isset($_POST['submit']))
{
$result = mysql_query("Select * From aa where email = '".$_SESSION[login_user]."'");
if($result)
{
$row = mysql_fetch_array($result);
$id = $row['id'];
if(!is_dir("images/videos".$id.'/'))
{
mkdir("images/videos".$id.'/');
}
$target_dir = "images/videos".$id;
// setup an array to check errors at the time of file upload
$errors = array();
//Loop through each file
for($i=0; $i<count($_FILES['file']['name']); $i++)
{
// set the array of allowed extensions
$allowed = array('mp4', 'mkv');
// extrach the file extension and check if it is valid
$ext = pathinfo($_FILES['file']['name'][$i], PATHINFO_EXTENSION);
if(!in_array($ext,$allowed))
{
$errors[] = "Invalid fiel extension.";
}
if(empty($errors))
{
// make parts of file name to append timestamp to them to avoid uploading of files with same name
$path_parts = pathinfo($_FILES["file"]["name"][$i]);
$image_path = $path_parts['filename'].'_'.time().'.'.$path_parts['extension'];
//Setup our new file path
$newFilePath = $target_dir.'/'.$image_path;
//Upload the file into the temp dir
if(move_uploaded_file($_FILES["file"]["tmp_name"][$i], $newFilePath))
{
//Handle other code here
}
}
else
{
print_r($errors);
}
}
if(empty($errors))
{
echo "Success";
}
}
else
{
echo "Error while fetching records ".mysql_error();
}
}
?>
希望这有助于完成工作。