2017-02-17 112 views
-1

我在這裏是一個初學者,我想上傳幾個圖像到SQL數據庫和一個文件夾,我目前正在我的本地主機上測試,它通常上傳到我的文件夾但沒有上傳到SQL數據庫,有什麼建議?圖片上傳到文件夾,但沒有上傳到SQL數據庫

的HTML

<!DOCTYPE html> 
<?php 
$conn = mysql_connect("localhost","root","")or die("could not connect"); 
mysql_selectdb("decorydata", $conn); 
?> 
<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>Import data from Decory DB</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta name="title" content="Hemant Vishwakarma"> 
    <meta name="description" content="Import Excel File To MySql Database Using php"> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
</head> 
<body>  
<br><br> 
    <div class="container"> 
     <div class="row"> 
     <div class="col-md-12 text-center"><h1> All Decory images </h1> </div> 
<br> 
      <div class="col-md-3 hidden-phone"></div> 
      <div class="col-md-6" id="form-login"> 
       <form class="well" action="import-img.php" method="post" enctype="multipart/form-data"> 
        <fieldset> 
         <legend>Import image file</legend> 
         <div class="control-group"> 
          <div class="control-label"> 
           <label>image File:</label> 
          </div> 
          <div class="controls form-group"> 
           <input type="file" name="files[]" id="file" class="input-large form-control" multiple> 
          </div> 
         </div> 

         <div class="control-group"> 
          <div class="controls"> 
          <button type="submit" id="submit" name="submit" class="btn btn-success btn-flat btn-lg pull-right button-loading" data-loading-text="Loading...">Upload images</button> 
          </div> 
         </div> 
        </fieldset> 
       </form> 
      </div> 
      <div class="col-md-3 hidden-phone"></div> 
     </div> 


     <table class="table table-bordered"> 
      <thead> 
        <tr> 
         <th>ID</th> 
         <th>name</th> 
         <th>type</th> 

        </tr> 
        </thead> 
      <?php 
       $SQLSELECT = "SELECT * FROM images"; 
       $result_set = mysql_query($SQLSELECT, $conn); 
       while($row = mysql_fetch_array($result_set)) 
       { 
       ?> 
        <tr> 
         <td><?php echo $row['ID']; ?></td> 
         <td><?php echo $row['name']; ?></td> 
         <td><?php echo $row['type']; ?></td> 
        </tr> 
       <?php 
       } 
      ?> 
     </table> 
    </div> 
</body> 
</html> 

的PHP:

<?php 

$conn = mysql_connect("localhost", "root", "") or die("could not connect"); 
mysql_selectdb("decorydata", $conn); 

$rd = rand(); 
if (isset($_FILES['files'])) { 
$errors = array(); 
foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) { 
    $file_name = $key . $rd . $_FILES['files']['name'][$key]; 
    $file_size = $_FILES['files']['size'][$key]; 
    $file_tmp = $_FILES['files']['tmp_name'][$key]; 
    $file_type = $_FILES['files']['type'][$key]; 
    if ($file_size > 2097152) { 
     $errors[] = 'File size must be less than 2 MB'; 
    } 
    $query = "INSERT INTO images (name,type) VALUES($file_name','$file_type',); "; 
    $desired_dir = "uploadphotos"; 
    if (empty($errors) == true) { 
     if (is_dir($desired_dir) == false) { 
      mkdir("$desired_dir", 0700); // Create directory if it does not exist 
     } 
     if (is_dir("$desired_dir/" . $file_name) == false) { 


      $src = imagecreatefromjpeg($tmp_name); 


      list($width, $height) = getimagesize($tmp_name); 


      $newwidth = ($width/$height) * 150; 
      $newheight = 150; 
      $tmp = imagecreatetruecolor($newwidth, $newheight); 

      imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); 
      $rd = rand(); 

      $filename = "thumbphotos/" . $file_name; 
      imagejpeg($tmp, $filename, 100); 

      imagedestroy($src); 

      move_uploaded_file($file_tmp, "$desired_dir/" . $file_name); 
     } else {   // rename the file if another one exist 
      $new_dir = "$desired_dir/" . $file_name . time(); 
      rename($file_tmp, $new_dir); 
     } 
     mysql_query($query); 
    } else { 
     print_r($errors); 
    } 
} 
if (empty($error)) { 
    echo " <div class='alert alert-success'>Your Photos Is Successfully Uploded.<a href='imagesupload.php'> Add new Photos</a></div>"; 
} 
} 
?> 

我缺少什麼?

+1

這是一個錯誤'VALUES($ file_name','和'',);'<<<並檢查錯誤會告訴你。 –

+1

**警告**:如果您只是學習PHP,請不要使用['mysql_query'](http://php.net/manual/en/function.mysql-query.php)界面。這是非常可怕和危險的,它在PHP 7中被刪除了。[PDO的替代品並不難學](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps -pdo-for-database-access /)以及[PHP The Right Way](http://www.phptherightway.com/)等指南介紹了最佳實踐。你的用戶數據是**不是** [正確轉義](http://bobby-tables.com/php.html),並有[SQL注入漏洞](http://bobby-tables.com/),並且可以被利用。 – tadman

+0

以及thx很多:),對不起,我很長時間工作很累,我滑倒這個錯誤 –

回答

0

爲什麼要將圖像上傳到數據庫中有很大的原因嗎?它效率不高,加工費用高。您必須解構圖像才能將其插入數據庫。然後,當你需要將圖像拉出來顯示時,你將不得不重建它。另外,即使只有表中的幾行,在數據庫中擁有圖像也會使數據庫大小顯着增長。 最佳做法是將圖像文件上傳到您已有工作的文件夾中。然後,只需獲取圖像文件的名稱及其擴展名並將其存儲在數據庫中即可。當你需要顯示圖像時,只需要在其中存儲圖像的靜態路徑,並從數據庫中動態提取所需圖像的名稱。

+0

thx的建議,但如果你仔細看看php文件,這正是我在做什麼,我上傳的圖像文件夾「uploadedphotos」,只是他們的名字和類型的數據庫,因爲一旦我需要顯示他們,然後我只是打電話給他們的名稱從文件夾,我完成:) –

相關問題