下面是我的代码,它将文件上传到服务器并将每个上传文件的名称存储到数据库中:
<?php
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
if ($_FILES['fileVideo']['error'] === UPLOAD_ERR_OK) {
$result = 0;
if( file_exists("VideoFiles/".$_FILES['fileVideo']['name'])) {
$parts = explode(".",$_FILES['fileVideo']['name']);
$ext = array_pop($parts);
$base = implode(".",$parts);
$n = 2;
while( file_exists("VideoFiles/".$base."_".$n.".".$ext)) $n++;
$_FILES['fileVideo']['name'] = $base."_".$n.".".$ext;
move_uploaded_file($_FILES["fileVideo"]["tmp_name"],
"VideoFiles/" . $_FILES["fileVideo"]["name"]);
$result = 1;
}
else
{
move_uploaded_file($_FILES["fileVideo"]["tmp_name"],
"VideoFiles/" . $_FILES["fileVideo"]["name"]);
$result = 1;
}
$videosql = "INSERT INTO Video (VideoFile)
VALUES (?)";
if (!$insert = $mysqli->prepare($videosql)) {
// Handle errors with prepare operation here
}
//Assign the variable
$vid = 'VideoFiles/'.$_FILES['fileVideo']['name'];
//Dont pass data directly to bind_param store it in a variable
$insert->bind_param("s",$vid);
$insert->execute();
$id = $mysqli->insert_id;
if ($insert->errno) {
// Handle query error here
}
$insert->close();
}else{
echo "Upload was not successful";
}
?>
<script language="javascript" type="text/javascript">
window.top.stopVideoUpload(<?php echo $result; ?>,'<?php echo $id; ?>', '<?php echo $_FILES['fileVideo']['name']; ?>');
</script>
</body>
</html>
现在我正在使用 jwplayer,它需要视频文件来匹配此页面上的视频格式:
因此,当文件上传到服务器时,我需要能够在服务器上自动对文件进行编码。我不希望用户尝试自己手动编码视频文件,我希望它自动完成。但我的问题是如何让自动服务器端文件编码工作?
我有一个演示如何上传视频文件:DEMO
使用演示:
单击
Add Question
按钮,文件输入将出现在表格中点击直接上传,您将看到一个简单的验证,说明允许使用哪种视频文件格式(这只需检查视频文件扩展名即可)
浏览一个视频文件,选择然后点击
Upload
等待上传完成(我推荐一个简短的视频文件以节省时间)。文件上传后会显示成功信息,视频文件上传到服务器,