2015-08-24 74 views
0

我在嘗試使用W3C文件上載腳本時遇到問題。未定義索引,似乎已定義

<?php 
ini_set('display_errors', '1'); 
include_once '../includes/conn.php'; 

$target_dir = "../images/pp/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
// Check if image file is a actual image or fake image 
if(isset($_POST["upload"])) { 
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
    if($check !== false) { 
     $uploadOk = 1; 
    } else { 
     echo "File is not an image."; 
     $uploadOk = 0; 
    } 
} 
// Check if file already exists 
if (file_exists($target_file)) { 
    echo "Sorry, file already exists."; 
    $uploadOk = 0; 
} 
// Check file size 
if ($_FILES["fileToUpload"]["size"] > 500000) { 
    echo "Sorry, your file is too large."; 
    $uploadOk = 0; 
} 
// Allow certain file formats 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
&& $imageFileType != "gif") { 
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
    $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."; 
    } 
} 
?> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>EpicOwl UK | CMS</title> 
    <meta charset="utf-8"> 
    <link rel="shortcut icon" href="../images/favicon.ico" type="image/x-icon" /> 
    <link rel="stylesheet" type="text/css" href="../css/main.css"> 
</head> 
<body> 
<div id="header"> 
    <a href="./index.php"><img id="logo" src="../images/logo.png" /></a> 
</div> 
<div id="content"> 
    <div id="spacer1"></div> 
    <div id="spacer2"></div><br /><br /> 
    <div id="profileboarder"> 
     <form method="post" action="#" enctype="multipart/form-data"> 
     <label class="green"><strong>Upload Profile Picture:</strong></label><br /> 
     <input type="file" name="fileToUpload" id="fileToUpload" /><br /><br /> 
     <input class="login" type="submit" name="upload" value="Upload" /> 
     </form> 
    </div> 
</div> 
</body> 
</html> 

我曾嘗試:

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 

改爲

$fileToUpload = $target_dir . basename($_FILES["fileToUpload"]["name"]); 

在此之後,我再更換了所有$target_dir$fileToUpload,但無濟於事。

該腳本以其他方式工作,但在提交之前顯示所有錯誤消息。

回答

1

地說:

$target_dir = "../images/pp/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 

內:

if(isset($_POST["upload"])) { 

因爲你沒有上傳任何文件$_FILES["fileToUpload"]尚未確定。這就是你得到錯誤的原因。未定義的索引意味着數組的索引尚未定義。如果您上傳文件並提交表單,$_FILES將被填充。

+0

我以前試過,但它沒有工作,做什麼工作都在腳本的最後關閉isset和實現你的上述變化。感謝您的指導 –

+0

沒有看到'isset'沒有關閉;) – TeeDeJee

1

PHP在第一次調用時讀取整個腳本(如果我是正確的)。所以當你的腳本第一次被調用時,indexToFileUpload是未定義的。在提交html表單後,索引被填充,因此不再無法查明。

你可以試試這個:

?php 
ini_set('display_errors', '1'); 
include_once '../includes/conn.php'; 

if($_SERVER["REQUEST_METHOD"] == "POST") 
{ 
    $target_dir = "../images/pp/"; 


// your code here. 


} else { 
?> 
<!DOCTYPE html> 
<html lang="en"> 
<head>` 
<!-- your HTML here --> 
<?php } ?>