2014-02-05 52 views
0

我無法將文件移動到所需的文件夾中。我想將圖像保存到上傳的文件夾中。在MySQL中我有它在blob類型。 這是我的代碼無法將文件移動到php文件夾中

$target_Path = "uploaded/"; 
    $target_Path = $target_Path.basename($_FILES['image']['name']); 
    move_uploaded_file($_FILES['image']['tmp_name'], $target_Path); 

    mysqli_query($con,"UPDATE info SET photo='$target_Path' WHERE user_id='$id'"); 

在MySQL中它顯示的東西已經被保存,但它不能打開該文件沒有移動到文件夾上傳。我正在做我的本地主機。請幫忙。

+0

您收到哪條錯誤消息並嘗試傳遞文件夾的完整路徑 –

+0

您的代碼對SQL注入打開。在將變量粘貼到查詢中之前,請使用準備好的語句並綁定變量,或者至少使用變量['mysqli_real_escape_string'](http://www.php.net/mysqli_real_escape_string)。 – h2ooooooo

回答

0

嘗試使用IF語句來包裝一下你move_uploaded_file,並拋出一個異常,如果該文件未能轉移:

if (move_uploaded_file($_FILES['image']['tmp_name'], $target_Path) === false) { 
    throw new Exception([text]); 
} 

此外,要檢驗目的地是否是可寫的,你可以嘗試打開有一個文件,並寫入它:

if (($fp = fopen($target_path, 'w')) === false) { 
    thrown new Exception("Failed to open file $target_path for writing"); 
} 
fwrite($fp, file_get_contents($_FILES['image']['tmp_name'])); 
fclose($fp); 

機會是問題是文件權限或所有權,你將需要使用chmod或chown將此命令行來修復之一。

相關問題