2017-03-15 42 views
-2

我有一些代碼可讓您將圖像上載到文件,然後在上載後將其顯示在頁面上。唯一的問題是圖像不會顯示?PHP顯示已上傳到文件的圖像

<?php 
$target_dir = "uploads/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
//echo $target_file; 
$uploadOk = 1; 
//$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 




// Check if file already exists 
if (file_exists($target_file)) { 
    echo "Sorry, file already exists."; 
    $uploadOk = 0; 
} 






// Check file size 
if ($_FILES["fileToUpload"]["size"] > 700000000) { 
    echo "Sorry, your file is too large."; 
    $uploadOk = 0; 
} 





// Check if $uploadOk is set to 0 by an error 
if ($uploadOk == 0) { 
    echo "Sorry, your file was not uploaded."; 
// if everything is ok, try to upload file 
} else { 




    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
     echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
    } else { 
     echo "Sorry, there was an error uploading your file."; 
    } 
} 



/* Displaying Image*/ 
//$image=$_FILES["fileToUpload"]["name"]; 
//  $img="uploads/".$image; 

echo "<br>"; 

         //echo'<br><img src="'.$img.'">'; 
         //echo "<br><img src=\"upload/$img\">"; 


echo '<br><img src="$target_file">'; 

這裏是我已經被顯示的圖像的一部分:

/* Displaying Image*/ 

    echo "<br>"; 

    echo '<br><img src="$target_file">'; 
+0

告訴我們,當您查看HTML源代碼,'echo'是什麼
';'告訴你?對於那些被註釋掉的其他人呢? –

+0

你上傳到一個目錄,而不是一個文件! ;)一個圖像,一個單詞文檔,一個csv,pdf ......這些都是文件。因此,您不會將圖像上傳到文件,您正在將圖像上傳到目錄..或者對於任何文檔:您將文件上傳到目錄。只爲下次;) – Twinfriends

回答

1

嘗試:

echo '<br><img src="'.$target_file.'">'; 

echo "<br><img src='$target_file'>"; 
+0

謝謝!第一個代碼適用於我 – Smeaj