上传脚本实际上并没有保存文件



我需要使用PHP将一些信息(如名称,图像(上传到数据库。

我尝试这样做:

Add_tour.php:

<?php
$app=new app();  
$targetDir = "/upload/";
if(isset($_POST['tour_name'])) {
$tour_name=isset($_POST['tour_name'])?trim($_POST['tour_name']):'';
$_SESSION['tour_name']=$tour_name;
}
if(isset($_POST['tour_img'])) {
$tour_img=isset($_POST['tour_img'])?trim($_POST['tour_img']):'';
$_SESSION['tour_img']=$tour_img;
move_uploaded_file($_FILES["tour_img"]["tmp_name"],$targetDir);
}
$ID=$app->add_tour();
?>
<form action="/admin/add_tour.php" method="post">     
<input type="text" class="form-control" id="tour_name" name="tour_name">
<input type="file" data-toggle="custom-file-input" id="tour_img" name="tour_img">                
</form>

然后是我的应用程序.class.php文件:

public function add_tour(){ 
$tour_name=isset($_SESSION['tour_name'])?$_SESSION['tour_name']:'';
$tour_img=isset($_SESSION['tour_img'])?$_SESSION['tour_img']:'';
$data="
tour_name       ='".$this->db->escape($tour_name)."' ,
tour_img        ='".$this->db->escape($tour_img)."'
";
$id=0;      
if($tour_name!='' && $tour_img!='')
{           
//find row
$res=$this->db->RawQuery("SELECT id FROM table WHERE
tour_name       ='".$this->db->escape($tour_name)."' AND
tour_img        ='".$this->db->escape($tour_img)."'
LIMIT 1;");
foreach($res as $row)
$id=$row['id'];     
if($id==0)  
{
$id=$this->db->RawQuery("INSERT INTO table SET $data ;");
$html.=ob_get_contents();
ob_end_clean();
}
return $id;
}

问题是只有文件名会被添加到数据库中。文件本身未上传到"上传"目录。

代码中有错误:

move_uploaded_file($_FILES["tour_img"]["tmp_name"], $targetDir);

您也忘记传递文件名。代码应为:

move_uploaded_file($_FILES["tour_img"]["tmp_name"], $targetDir . $_FILES["tour_img"]["name"]);

此外,请确保您的标签中有enctype="multipart/form-data"

在表单中使用以下属性

enctype="multipart/form-data"

最新更新