Reputation: 1173
I am having issues when trying a W3C file upload script.
<?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>
I have tried:
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
Changed to
$fileToUpload = $target_dir . basename($_FILES["fileToUpload"]["name"]);
Following that, I then replaced all $target_dir
with $fileToUpload
but to no avail.
The script works otherwise, but before submitting it shows all the error messages.
Upvotes: 0
Views: 380
Reputation: 151
PHP reads the entire script on first call(if i'm correct). So when your script is first called, the index fileToUpload is undefined. Upon submission of the html form the index is filled and therefore not undefind anymore.
You could try to this :
?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 } ?>
Upvotes: 1
Reputation: 3741
Put:
$target_dir = "../images/pp/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
inside:
if(isset($_POST["upload"])) {
Because you didn't upload any file $_FILES["fileToUpload"]
is not yet defined. That's why you get the error. Undefined index means that the index of an array is not yet defined. $_FILES
will get populated if you upload a file and submit the form.
Upvotes: 1