2017-07-08 109 views
1

我試圖使用PHP和MySQL上傳的圖像,並且下面是所使用的代碼..爲什麼move_upload_file在給定的代碼中總是返回false?

的index.php

<form action="submit.php" enctype="multipart/form-data" method="post"> 

<table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5"> 
<tbody><tr> 
<td> 
<input name="uploadedimage" type="file"> 
</td> 

</tr> 

<tr> 
<td> 
<input name="Upload Now" type="submit" value="Upload Image"> 
</td> 
</tr> 


</tbody></table> 

</form> 

submit.php

<?php 

include("mysqlconnect.php"); 

function GetImageExtension($imagetype) { 
    if (empty($imagetype)) 
     return false; 
    switch ($imagetype) { 
     case 'image/bmp': return '.bmp'; 
     case 'image/gif': return '.gif'; 
     case 'image/jpeg': return '.jpg'; 
     case 'image/png': return '.png'; 
     default: return false; 
    } 
} 

if (!empty($_FILES["uploadedimage"]["name"])) { 

    $file_name = $_FILES["uploadedimage"]["name"]; 
    $temp_name = $_FILES["uploadedimage"]["tmp_name"]; 
    $imgtype = $_FILES["uploadedimage"]["type"]; 
    $ext = GetImageExtension($imgtype); 
    $imagename = date("d-m-Y") . "-" . time() . $ext; 
    $target_path = "images/" . $imagename; 



    if (move_uploaded_file($_FILES['uploadedimage']['tmp_name'], $target_path)) { 
     $detail = date("Y-m-d"); 
     $sql = "INSERT INTO `image_upload`(`id`, `image`, `detail`) VALUES (NULL,'$target_path','$detail')"; 
     if (!$conn->query($sql)) { 
      echo $conn->error; 
     } else { 
      echo "Successfully inserted. "; 
     } 
    } else { 

     exit("Error While uploading image on the server"); 
    } 
} 
?> 

表結構:

# Name  Type Collation  Attributes Null Default Extra 
1 id(Primary) int(11)        No None AUTO_INCREMENT 
2 image  blob         Yes NULL  
3 detail  varchar(500) utf8_general_ci   Yes NULL 

每當我執行這個它總是顯示我「錯誤在服務器上傳圖片時「我不明白爲什麼。有人可以讓我知道我哪裏錯了,我該如何改進我的實施? 在此先感謝。

+1

檢查您的目錄中存在與否,並有寫權限給予 –

+0

權限目錄已經它仍然不執行。 –

回答

0

在移動文件之前添加此行。

if (!file_exists($target_path)) { mkdir($target_path , 0777, true); } 

這將添加文件夾,如果不存在..具有所有權限。

+0

我嘗試添加這但仍給了相同的輸出 –

+0

顯示我的錯誤.... – GYaN

+0

它不給任何錯誤,它只是給move_uploaded_file代碼else塊: 其他{ 退出(「錯誤在服務器上上傳圖片「); } –

0

改變你的表結構的圖像爲varchar(255),並檢查是否工作

相關問題