我正在开设一个用户上传视频的网站,并将name
,size
,type
,path
和tmp_name
放入一个MySQL数据库。 upload.php
文件位于下方,
<?php
$is_form_submitted = (isset($_POST['submit']))?true:false;
if($is_form_submitted)
{
//defines variables
$name=$_FILES['file']['name'];
$type=$_FILES['file']['type'];
$size=$_FILES['file']['size'];
$tmp_name=$_FILES['file']['tmp_name'];
$target_path="videos/";
$allowedTypes = array("video/wmv","video/avi",
"video/mpeg","video/mpg","video/mp4");
$is_valid_type = (in_array($_FILES['file']['type'], $allowedTypes))?true:false;
if ( $is_valid_type&& ($_FILES["file"]["size"] < 20000000000))
{
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"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
mysql_query("INSERT INTO vids(name, type, size, tmp_name, target_path)
VALUES('$name', '$type', '$size', '$tmp_name', '$target_path')");
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"videos/" . $_FILES["file"]["name"]);
echo "Stored in: " . "videos/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
}
?>
所有内容都正常上传,但当我去尝试获取视频的名称和路径时,它说视频播放器找不到支持的视频,那么我该如何获取来自数据库的随机视频并将其作为视频播放器的来源? (视频文件可在播放器中播放,因此不是视频的错误)
这是文档代码中的php代码
<?php
$vid_url = "videos/";
$result = mysql_query("SELECT * FROM `vids` WHERE 1");
while($row = mysql_fetch_assoc($result))
{
echo
<div name="video">
<video width="100%" height="100%" controls>
<source src=".$vid_url.$row."type="video/mp4">
Error: Video Not working
</object>
</video>
</div>';
}
?>
我存储视频的地方是一个名为视频的目录
答案 0 :(得分:0)
我想出来以防有人想要参考这个
<?php
//Connects to database
$host="################"; // Host name
$username="##########"; // Mysql username
$password="###########"; // Mysql password
$db_name="#######"; // Database name
$tbl_name="####"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
//Tells variable video where to get the name from
$vid_url = "videos/";
$result = mysql_query("SELECT * FROM vids ORDER BY RAND() LIMIT 1");
$row = mysql_fetch_assoc($result);
$video = $vid_url.$row["name"];
<?