I tried to create an upload script in PHP. I getting this error message:
Notice: Undefined index: file in path\upload\index.php on line 3
This is my form (pretty basic):
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="fileUpload">
<input type="submit" value="Upload Image" name="submitt">
</form>
This is my (not working) upload script:
<?php
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
if(isset($name)){
if(!empty($name)){
$location = '../data/';
if(move_uploaded_file($temp_name, $location.$name)){
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
?>
This is my php.ini config:
; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads=On
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize=40M
; Maximum number of files that can be uploaded via a single request
max_file_uploads=20
; Maximum size of POST data that PHP will accept.
; Its value may be 0 to disable the limit. It is ignored if POST data reading
; is disabled through enable_post_data_reading.
; http://php.net/post-max-size
post_max_size=50M
So what did I do wrong. I checked every previously asked question but didn't find an answer.
EDIT 1:
After checking out this and altering my code the problem still consists.
<?php
if (isset($_POST['submitt'])) {
if (isset($_FILES['file']) && isset($_FILES['file']['name']) && isset($_FILES['file']['tmp_name'])) {
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
if(!empty($name)){
$location = '../data/';
if(move_uploaded_file($temp_name, $location.$name)){
echo 'File uploaded successfully';
}
} else {
echo 'You should select a file to upload !!';
}
}
}
?>
EDIT 2:
I found the problem: Replacing action upload
with upload/index.php
resolved the problem. I am using XAMPP on my local machine to test my code. This is an error due to an incorrect Apache config by XAMPP itself.