gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:34:46 24 4
gpt4 key购买 nike

我尝试制作一个用于上传图片和视频的表单,并使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com