以表格形式上传图像和视频,并在mysql中存储数据

时间:2013-06-11 20:46:42

标签: 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。 我需要将这些文件存储在本地,因为我有一个从该文件夹中填充的视频播放器。

1 个答案:

答案 0 :(得分:0)

确保错误代码实际上为零(这是文件不存在的第一个原因)。如果它不为零,那么您可以找到有关错误代码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