Undefined Index while uploading file
Change your PHP script as below and try
<?php
if(isset($_POST['submit'])){
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
if(isset($name) and !empty($name)){
$location = '../uploads/';
if(move_uploaded_file($temp_name, $location.$name)){
echo 'File uploaded successfully';
}
} else {
echo 'You should select a file to upload !!';
}
}
?>
PHP Undefined index error $_FILES?
first: try to strict programming
error_reporting(E_ALL | E_STRICT);
also you must use isset for check is index for array available or not
if (isset($_POST['submitbtn']) && isset($_FILES['avatar'])) {
// ...
}
also check php configuraion
file_uploads "1"
upload_max_filesize "2M"
post_max_size "8M"
max_file_uploads 20
post max size must be larger than upload max file size.
also as guys said check form enctype
Undefined index: file
"Undefined index"
means you're trying to read an array element that doesn't exist.
Your specific problem seems to be that you're trying to read upload data that doesn't exist yet: When you first visit your upload form, there is no $_FILES
array (or rather, there's nothing in it), because the form hasn't been submitted. But since you don't check if the form was submitted, these lines net you an error:
//Set Temp Name for upload.
$uploaded->set_tmp_name($_FILES['file']['tmp_name']);
//Set file size
$uploaded->set_file_size($_FILES['file']['size']);
//set file type
$uploaded->set_file_type($_FILES['file']['type']);
//set file name
$uploaded->set_file_name($_FILES['file']['name']);
They're all trying to read the value of $_FILES['file']
to pass them to the methods of $uploaded
.
What you need is a check beforehand:
if (isset($_FILES['file'])) {
$uploaded = new upload;
//set Max Size
$uploaded->set_max_size(350000);
//Set Directory
$uploaded->set_directory("data");
//Set Temp Name for upload.
$uploaded->set_tmp_name($_FILES['file']['tmp_name']);
//Set file size
$uploaded->set_file_size($_FILES['file']['size']);
//set file type
$uploaded->set_file_type($_FILES['file']['type']);
//set file name
$uploaded->set_file_name($_FILES['file']['name']);
//start copy process
$uploaded->start_copy();
if($uploaded->is_ok())
echo " upload is doen.";
else
$uploaded->error()."<br>";
}
Notice: Undefined index: file
I think
<input type="file" name="fileToUpload" id="fileToUpload">
should be
<input type="file" name="file" id="fileToUpload">
Notice: Undefined index: file when uploading file
For the next one looking for it :)
To upload a file you will need enctype="multipart/form-data"
:
<form enctype="multipart/form-data">
<input type="file" name="file"/>
</form>
undefined index in php while uploading an image file
When you first load the page, $_FILES['myimage']['name']
and $_FILES['myimage']['tmp_name'];
will be undefined because you haven't uploaded anything yet.
The solution is:
First add a
name
attribute your submit button, like this:<button type="submit" name="submit" class="btn btn-info">upload</button>
And wrap your form processing code inside an
if
block, like this:// your code
if(isset($_POST['submit'])){
$location="profilepics/";
$name=$_FILES['myimage']['name'];
$temp_name=$_FILES['myimage']['tmp_name'];
if(isset($name)){
move_uploaded_file($temp_name,$location.$name);
}
}
Related Topics
What Means Call to a Member Function on Boolean and How to Fix
MySQL Group by and Fill Empty Rows
How to Download a File Using Curl in PHP
How to Add Two Strings Together
PHP Session-Based Flash Message
How to Execute PHP Code from the Command Line
Magento - Passing Data Between a Controller and a Block
Installing Pdo Driver on MySQL Linux Server
Protocol Https Not Supported or Disabled in Libcurl
When and Where Should I Use Session_Start
Supplied Key Param Cannot Be Coerced into a Private Key with Google APIs
Default as First Option in Switch Statement
How to Rename a Tag in Simplexml Through a Dom Object
How to Find Timezone Id from Ip Address/Country Name in PHP