<?php
$name = $_FILES["file"]["name"];
//$size = $_FILES['file']['size']
//$type = $_FILES['file']['type']
$tmp_name = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];
if (isset ($name)) {
if (!empty($name)) {
$location = 'uploads/';
if (move_uploaded_file($tmp_name, $location.$name)){
echo 'Uploaded';
}
} else {
echo 'please choose a file';
}
}
?>
<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><br><br>
<input type="submit" value="Submit">
</form>
i get an 'Notice: Undefined index' error message. The enctype is included in the form tag so i can't figure out what it is.. can anyone help me out??
Resolved Undefined Index in php while uploading file
due to maximum file size limit
changes in
php.ini
change according to your requirements
The first assignment throws an warning, if nothing is uploaded and the isset test is a little bit useless..
You can change your code as follows
If you're using your entire code as one file (which I suspect you are), then you need to do the following using a conditional statement, which I tested (and working) before posting.
Plus, make sure that your
uploads
folder has proper write permissions set and it exists.Footnotes:
I added a conditional statement:
and I named the submit button: (to work in conjunction with the
isset()
conditional statement)N.B.: If you are in fact using your posted code as two seperate files, then you can simply copy the PHP in this answer, along with naming your present submit button set in a seperate HTML form as
name="submit"
(calling your formupload_form.htm
for example) as I did shown above, yet retaining theaction="upload.php"
and naming the PHP upload handler file accordingly.