iT邦幫忙

0

PHP檔案上傳結果無法顯示

php

最近在測試PHP的上傳系統 雖然檔案是能上傳了
但是結果無法順利顯示 會變成跳下載視窗
https://ithelp.ithome.com.tw/upload/images/20180507/20109820nlGT84Fk7c.jpg

程式碼

upload.html

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Song Uploader</title>
<body>
 <form action="upload.php" method="post" enctype="multipart/form-data">
 <input type="file" name="file" id="file" />
 <input type="submit" name="submit" value="上傳檔案" />
 </form>
 </body>
 <head>
</html> 

upload.php

<?php
if ($_FILES["file"]["error"] > 0){
 echo "Error: " . $_FILES["file"]["error"];
}else{
 echo "檔案名稱: " . $_FILES["file"]["name"]."<br/>";
 echo "檔案類型: " . $_FILES["file"]["type"]."<br/>";
 echo "檔案大小: " . ($_FILES["file"]["size"] / 1024)." Kb<br />";
 echo "暫存名稱: " . $_FILES["file"]["tmp_name"];
 
 if (file_exists("upload/" . $_FILES["file"]["name"])){
  echo "檔案已經存在,請勿重覆上傳相同檔案";
 }else{
  move_uploaded_file($_FILES["file"]["tmp_name"],"upload/".$_FILES["file"]["name"]);
 }
}
?>
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0

php邏輯有問題,應該要確定檔案上傳成功再顯示資訊,加上echo應該只能做一次~
/images/emoticon/emoticon06.gif

<?php
if ($_FILES["file"]["error"] > 0){
 echo "Error: " . $_FILES["file"]["error"];
}else{
  if (file_exists("upload/" . $_FILES["file"]["name"])){
     echo "檔案已經存在,請勿重覆上傳相同檔案";
 }else{
     move_uploaded_file($_FILES["file"]["tmp_name"],"upload/".$_FILES["file"]["name"]);
		$str = "檔案名稱: " . $_FILES["file"]["name"]."<br/>";
		$str .= "檔案類型: " . $_FILES["file"]["type"]."<br/>";
		$str .= "檔案大小: " . ($_FILES["file"]["size"] / 1024)." Kb<br />";
		$str .= "暫存名稱: " . $_FILES["file"]["tmp_name"];  
		echo $str;
 }
}
?>
leotse iT邦新手 5 級 ‧ 2018-05-07 23:05:27 檢舉

會下載php檔?? 估計是伺服器問題.

leotse iT邦新手 5 級 ‧ 2018-05-07 23:07:54 檢舉

抱歉,我本想回答,但放錯位置.

0
leotse
iT邦新手 5 級 ‧ 2018-05-07 23:08:07

會下載php檔?? 估計是伺服器問題.

那要怎解決?

我要發表回答

立即登入回答