2017-02-23 60 views
0

我想從用戶那裏得到一張照片並插入到數據庫中。用戶應該在下面的輸入字段中上傳文件。另外,我想在需要時顯示數據庫中的圖像。在MySql中保存和檢索照片

<label>Photo</label> 
<input type="file" name="photo" required> 
<?php 
mysql_connect("localhost", "root", ""); mysql_select_db("prs"); 
if(isset($_POST['submit'])){ 
    $photo = $_FILES['photo']['name']; 
    { 
     $query = mysql_query("insert into date (photo) values ('$photo')"); 
    } 
} 
?> 
+0

上傳服務器上的圖像。將其路徑保存在數據庫中並在顯示時將其從URL中拉出來 – Satya

+0

您可以請解釋一下 – Udhayakkrishna

+0

首先提及[this](http://stackoverflow.com/questions/13470482/php-upload-image-to-directory) – Akshay

回答

1

您可以通過以下過程

  1. 做到這一點上傳圖像到服務器。你可以將它保存在圖像文件夾或你想要的地方。

  2. 只將上傳的圖像路徑存儲在數據庫中。恩。 /image/default.png。您也可以只保存圖像名稱作爲數據庫ex。 default.png

  3. 想要顯示圖像時從數據庫中檢索圖像路徑。

  4. 您可以使用圖像標記 前,在網頁中顯示圖像。 <img src="../images/default.png" />

0

最好的辦法是在數據庫表中保存文件路徑或文件名。並將您的文件保存在服務器中。

下面是一個示例MULTIPLE圖像上傳和檢索腳本爲您的需要。我要在這裏使用3個文件。 front.html,upload.php的最後preview.php

  1. Front.html

該文件包含了基本的HTML表單的內容。

<form enctype="multipart/form-data" action="upload.php" method="post"> 
<input id="uploadFile" type="file" name="files[]" class="img" /> 
<button class="btn btn-primary" name="up_img">POST</button> 
</form> 

2.upload.php

if(isset($_POST['up_img'])){ $errors= array(); 

    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){ 
     $file_name =$key.$_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'; 
     } 
     $pType='I'; 
     $query="insert into img(user,FILE_NAME)values('$sUser','$file_name') "; 
     $desired_dir="images_folder"; 
     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){ 
       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)){ 
    header('Location:preview.php'); 
    } 

    } 

3.preview.php

現在,我已經成功上傳我的圖像/影像服務器,並準備對其進行檢索。記住我的圖像文件現在存儲在文件夾call images_folder中,它們的路徑保存在img表中。

<?php 
$sql=mysql_query('SELECT * FROM img '); 
while($row = mysql_fetch_array($sql)) 
{ 
$sPic=$row['FILE_NAME']; 

echo '<img src="images_folder/'.$sPic.'">'; 

} 
?> 

就是這樣。這就是你要如何上載圖像到你的服務器,並檢索它們。

0

所以這裏有一個小小的選項來上傳一張圖片並將圖片保存到mysql 這是沒有測試只是鍵入。

if(isset($_FILES['picture']) //checks for form post if not show form 
    { 
     $title = $_POST['title']; //get title 
     $desc = $_POST['description']; //get description 
     $target = "downloads/pics/".basename($_FILES['uploaded']['name']); //this gets the target folder to save pictures make sure folder exists 
     $ok=1; //basic error set to 1 
     //This is our size condition 
     if ($uploaded_size > 20000000) 
     { 
      $out = "Your file is too large.<br>"; //checks for file size this can be changed to w/e 
      $ok=0; //if over error is set to 0 
     } 
     if ($ok==0) 
     { 
      $out.= "Sorry your file was not uploaded"; 
      return $out; 
     } 
     //If everything is ok we try to upload it 
     else 
     { 
      if(move_uploaded_file($_FILES['picture']['tmp_name'], $target)) //tires to move the uploaded picture to target 
      { 
       $out = "The Picture: <b><u>".basename($_FILES['picture']['name']). "</b></u> has been uploaded"; //if successfull then post was uploaded 
       $sql = "INSERT INTO database SET title=\"".$title."\",picture=\"".basename($_FILES['uploaded']['name'])."\",description=\"".$desc."\""; //sql string to insert the folder and picture name 
        if (!mysql_query($sql)) 
        { 
         die('Error In Picture Upload: - SQL INSERT INTO - '.date('m-d-y').' - SQL ERROR: '.mysql_error()); //if mysql error post error 
        } 
        else 
        { 
         $out.="<br /><br />Return To Your <a href=\"return location\">Downloads</a>"; //if mysql is ok show the return link 
         return $out; 
        } 
      } 
     } 
    else // if form is not submited then show form 
    { 
     $out ="\t\t<h3>".ucwords('new picture')."</h3>\r\n"; 
     $out.="\t\t<form name=\"post\" action=\"".$_SERVER['PHP_SELF']."\" method=\"post\" enctype=\"multipart/form-data\">\r\n"; 
     $out.="\t\t\t<p>Title:<br /><input type=\"text\" class=\"inputtext\" name=\"title\" value=\"\" /></p>\r\n"; 
     $out.="\t\t\t<p><b>".ucwords('picture upload')."</b>: <input type=\"file\" name=\"picture\" /></p>\r\n"; 
     $out.="\t\t\t<p>Description:<br /><textarea id=\"text9\" name=\"description\"></textarea></p>\r\n"; 
     $out.="\t\t\t<p><input type=\"submit\" name=\"submit\" value=\"".ucwords('upload')."\" /></p>\r\n"; 
     $out.="\t\t</form>\r\n"; 
     echo $out; 
    }