我在嘗試使用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
,但無濟於事。
該腳本以其他方式工作,但在提交之前顯示所有錯誤消息。
我以前試過,但它沒有工作,做什麼工作都在腳本的最後關閉isset和實現你的上述變化。感謝您的指導 –
沒有看到'isset'沒有關閉;) – TeeDeJee