2014-02-12 68 views
0

我最初通過mysql顯示圖像。現在我想通過上傳的圖像替換最初的圖像。我可以這樣做嗎?我使用此代碼在網頁上上傳圖像PHP:通過上傳的圖像替換mysql中存儲的圖像

$UPLOADDIR = "upload/"; 

// if the form has been submitted then save and display the image(s) 
if(isset($_POST['Submit'])) 
{ 
    // loop through the uploaded files 

    foreach ($_FILES as $key => $value) 
    { 
     $image_tmp = $value['tmp_name']; 
     $image  = $value['name']; 
     $image_file = "{$UPLOADDIR}{$image}"; 

     // move the file to the permanent location 

     if(move_uploaded_file($image_tmp,$image_file)) 
     { 
      echo <<<HEREDOC 
      <div style="float:left;margin-right:10px"> 
       <img src="{$image_file}" alt="file not found" /></br> 
      </div> 
      HEREDOC; 
     } 
     else 
     { 
      echo "<h1>image file upload failed, image too big after compression</h1>"; 
     } 
    } 
} 
else 
{ 
    ?> 

<form name='newad' method='post' enctype='multipart/form-data' action=''> 
    <table> 
    <tr> 
     <td><input type='file' name='image'></td> 
    </tr> 
    <tr> 
     <td><input name='Submit' type='submit' value='Upload image'></td> 
    </tr> 
</table> 
</form> 

<?php 
} 
?> 
+0

你的意思是說,你想更新現有的圖像?請更清楚! –

+0

@ CodingAnt-是的,我想這樣做。 –

+0

在db中更新圖像信息並刪除/替換舊圖像 –

回答

0

只需將更新查詢時,圖像上傳,如果要刪除現有的再使用的unlink(先前文件名);這將刪除以前的文件。

if(move_uploaded_file($image_tmp,$image_file)) 
    { 
     EXECUTE UPDATE QUERY TO REPLACE EXISTING FILE 
    } 
    else 
    { 
     echo "<h1>image file upload failed, image too big after compression</h1>"; 
    } 
+0

其在數據庫沒有更新圖像,並指定文件夾中甚至沒有存儲圖像。 –

+0

那麼我就需要數據庫結構和完整的頁面代碼,因爲你正在做一些邏輯錯誤,它不能被糾正這樣的。 –

+0

- 這是我使用以顯示存儲在數據庫圖像的代碼:<?PHP //到數據庫的連接 $ RESULT1 =請求mysql_query( 「SELECT * FROM $表」) 或die(「SELECT Error:」.mysql_error()); echo「

」; 而($行= mysql_fetch_array($ RESULT1)) { \t回聲 「」; \t echo「​​」; ?> 「高度=」 100" 寬度= 「100」> 」; \t回聲「 」; } 回聲「
」; > –