php - 以表单上传图片和视频,并将数据存储到mysql中

标签 php mysql forms upload

我尝试制作一个用于上传图片和视频的表单,并使用 mysql 中的特定 id 对其进行重命名。

数据存储在mysql中,图片正在上传,但视频未上传。有人能告诉我哪里错了吗?

表单代码:

<form id='adaugaanunt' name='add-item' method='post' action='add_in_video.php'     enctype='multipart/form-data' onsubmit="return jcap();">
<div class='container'>
<label for='titlu' >*Titlu:</label><br />
<input name='titlu' required='required' placeholder='ex. Vand ceas ROLEX' type='text'  class='input required' /></div><br />
<div class='container'>
<label for='descriere' >*Descriere:</label><br />
<input name='descriere' required='required' placeholder='ex. Vand ceas ROLEX cu luminite' type='text' class='input required' /></div><br />
<div class='container'>
<label for='pic1' >Poza: </label><br />
<input name='pic1' type='file' /><br />
</div><br />
<div class='container'>
<label for='vid1' >Video: </label><br />
<input name='vid1' type='file' /><br />
</div><br />
<div class='container' style="float:right"><input name='submit' type='submit' value='Trimite' /></div>
<div class='container' style="float:left"><input type="reset" name="Reseteaza" value="Reseteaza"></div>
</form>

add_in_video.php

<?php include 'include/global.php'; 
$titlu = str_replace("'","`",$_POST['titlu']);
$cere = "SELECT * FROM `video` WHERE titlu='".$titlu."'";
$rez = mysql_query($cere);
$cnt = mysql_num_rows($rez);
if ($cnt == 0) {
$title = htmlentities($titlu);
$descriere = htmlentities($_POST['descriere']);
$cerereSQL = "INSERT INTO `video` (`id` , `titlu`, `descriere`)
VALUES ('', '$titlu', '$descriere')";
//echo $cerereSQL;
$result_insert_user = mysql_query($cerereSQL);
if (file_exists("../videos/".mysql_insert_id()."a.jpg"))
{
unlink("../videos/".mysql_insert_id()."a.jpg");
}
if (file_exists("../videos/".mysql_insert_id()."b.avi"))
{
unlink("../videos/".mysql_insert_id()."b.avi");
}
if ($_FILES["pic1"]["name"] != "") {
if (($_FILES["pic1"]["type"] == "image/gif") || ($_FILES["pic1"]["type"] == "image/jpeg") || ($_FILES["pic1"]["type"] == "image/pjpeg") || ($_FILES["pic1"]["type"] == "image/bmp"))
{
if ($_FILES["pic1"]["error"] > 0)
{
echo "A intervenit o eroare: " . $_FILES["pic1"]["error"] . "<br />";
}
else
{
if (file_exists("../videos/" . $_FILES["pic1"]["tmp_name"]))
  {
  echo "Poza exista deja";
  }
 else
  {
  move_uploaded_file($_FILES["pic1"]["tmp_name"], "../videos/".mysql_insert_id()."a.jpg");
  }
 }
 }
else
{
header('Location: '.$domain.'index.php');
exit;
}
}

if ($_FILES["vid1"]["name"] != "") {
$allowedExts = array("wmv","avi","mpeg","mpg", "mp4");
$extension = end(explode(".", $_FILES["vid1"]["name"]));
if ((($_FILES["vid1"]["type"] == "video/mp4") || ($_FILES["vid1"]["type"] == "video/wmv") || ($_FILES["vid1"]["type"] == "video/mpg") || ($_FILES["vid1"]["type"] == "video/mpeg")) && in_array($extension, $allowedExts))
 {
if ($_FILES["vid1"]["error"] > 0)
{
echo "A intervenit o eroare: " . $_FILES["vid1"]["error"] . "<br />";
}
else
{
if (file_exists("../videos/" . $_FILES["vid1"]["tmp_name"]))
  {
  echo "Acest video exista deja";
  }
else
  {
  move_uploaded_file($_FILES["vid1"]["tmp_name"], "../videos/".mysql_insert_id()."b.avi");
    }
   }
 }
else
{
header('Location: '.$domain.'index.php');
exit;
}
}



} else {
header('Location: '.$domain.'index.php');
exit;
}
?>

我不t recieve any error message. The image file is uploading and the video file is NOT UPLOADING. Sorry about this code, I我是 PHP/MYSQL 新手。 我需要将该文件存储在本地,因为我有一个从该文件夹填充的视频播放器。

最佳答案

确保错误代码实际上为零(这是文件不存在的第一个原因)。如果它不为零,那么您可以找到有关错误代码 PHP - Error Messages Explained 的信息。

您应该检查 move_uploaded_file 的返回值:

$tmp_name = $_FILES["vid1"]["tmp_name"];
$name = "../videos/" . mysql_insert_id() . "b.avi";
if(!move_uploaded_file($tmp_name, $name)){
    print "Could not move file $tmp_name to $name<br>";
} else {
    print "Successfully copied files<br>";
}

move_uploaded_file 调用中可能存在错误,并且后续警告被 PHP 抑制

您可以通过在脚本开头调用 error_reporting(E_ALL); 来打印所有错误/警告/消息。有关错误报告的更多信息请参见此处 - PHP - error_reporting

关于php - 以表单上传图片和视频,并将数据存储到mysql中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17053512/

相关文章:

php - 验证电子邮件格式 PHP

python - 如何处理 MySQL 列中需要 varchar(255) 的 Python 字符串列表?

asp.net-mvc - 如何在发布表单时将默认值设置为空 EditorFor-field i .NET MVC

javascript - 如何修改表单操作添加选定的单选按钮值

javascript - 通过 JS 自动提交表单(超时、时间间隔)

php - 如何使用 PEAR DB 从 utf8 编码的 PHP 访问 latin1 编码的数据库

javascript - 谷歌地图显示多个位置(错误 : locations[i] is undefined)

php - 如何使用 print_r 只显示数组中的文本

php - PDO 查询未插入 - HY093 错误消息但绑定(bind)变量的数量正确

Mysql行返回/最新记录中的错误值