2011-08-16 111 views
0

我收到以下錯誤:錯誤與PHP代碼(文件上傳)

注意:未定義指數:theimage在C:\ WAMP \ www_upload \的index.php上線5

注意:未定義指數:theimage在C:\ WAMP \ www_upload \的index.php上線7

下面是代碼:

<?php 

$target_path = "images/"; 

$target_path = $target_path . basename($_FILES['theimage']['name']); 

if(move_uploaded_file($_FILES['theimage']['tmp_name'], $target_path)) { 
echo "<p>The image ". basename($_FILES['theimage']['name']). " has been uploaded</p>"; 
} else{ 
echo "<p>There was an error uploading the image, please try again!</p>"; 
} 

?> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<html> 
<head> 
<title></title> 
<meta name="" content=""> 
</head> 
<body> 

<form enctype="multipart/form-data" action="index.php" method="POST"> 
<fieldset> 
<input name="theimage" type="file" /> 
<input type="submit" value="Upload" /> 
</fieldset> 
</form> 



</body> 
</html> 

關於如何解決此問題,請任何想法?

+0

'print_r($ _ FILES)'給你什麼? –

+0

您可以發佈'$ _FILES'數組的var_dump或'print_r'嗎?另外,這三個php設置的值是什麼? 'file_uploads','upload_max_filesize','max_file_uploads' – Jeff

回答

2
<?php 
if(!empty($_FILES['theimage'])) { 
    // YOUR CURRENT PHP CODE HERE 
?> 

問題是即使沒有發佈任何內容(例如當您第一次加載頁面時),上傳腳本仍在執行。

2

檢查以確保你在$_FILES$_POST獲取數據,你嘗試做任何事情與他們之前:

<?php 
    if(isset($_FILES)) {  // if(isset($_POST)) 
           // would work as well 
     $target_path = "images/"; 
     $target_path = $target_path . basename($_FILES['theimage']['name']); 
     if(move_uploaded_file($_FILES['theimage']['tmp_name'], $target_path)) { 
      echo "<p>The image ". basename($_FILES['theimage']['name']). 
       " has been uploaded</p>"; 
     } else { 
      echo "<p>There was an error uploading the image!</p>"; 
     } 
    } 
?> 
0

更改您的提交按鈕:

<input type = 'submit' name = 'theSubmitBtn' value = 'Upload'> 

之後,所有包裹在此,如果檢查的頂部內你的PHP代碼:

if(isset($_POST['theSubmitBtn'])) { 

... Your Code ... 

} 

看看是否能爲你工作。在附註中,如果要在生產環境中使用它,還需要添加更多的錯誤檢查。