无法将pic链接存储到数据库中

时间:2018-03-31 10:50:29

标签: php mysql image pdo

我正在尝试将我的图片上传到文件夹和文件链接存储到数据库中,尽管文件夹存储在文件夹中,但遗憾的是它并没有在数据库中存储链接。请看我在哪里做错了。

<?php
include('dbconnection.php');
if(count($_FILES["file"]["name"]) > 0)
{
 sleep(3);
 for($count=0; $count<count($_FILES["file"]["name"]); $count++)
{
  $file_name = $_FILES["file"]["name"][$count];

  $tmp_name = $_FILES["file"]['tmp_name'][$count];

  $file_array = explode(".", $file_name);

  $file_extension = end($file_array); 
  if(file_already_uploaded($file_name, $connect))
{   
$file_name = $file_array[0] . '-'. rand() . '.' . $file_extension;  
}  
$location = 'files/' . $file_name;
  if(move_uploaded_file($tmp_name, $location))
  {
   $stmt= $connect->prepare("INSERT INTO tbl_image (image_name) VALUES (:image_name)");
   $stmt->bindParam(':image_name', $file_name); 
   $stmt->execute();
  }
 }
}

function file_already_uploaded($file_name, $connect)
{
 $statement = $connect->prepare("SELECT image_name FROM tbl_image WHERE image_name = '".$file_name."'");
 $statement->execute();
 $number_of_rows = $statement->rowCount();
 if($number_of_rows > 0)
 {
   return true;
 }
 else
 {
   return false;
 }
}
?>

1 个答案:

答案 0 :(得分:0)

将图像名称存储为具有文件名的位置:

$location = 'files/' . $file_name;
  if(move_uploaded_file($tmp_name, $location))
  {
   $stmt= $connect->prepare("INSERT INTO tbl_image (image_name) VALUES (:image_name)");
   $stmt->bindParam(':image_name', $location.'/'.$file_name); 
   $stmt->execute();
  }