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
php upload error, undefined index userfile
undefined index userfile, that means when the page loads there is no $_FILES['imageupload'] you need to submit the page to have that variable
<?php
if(isset($_POST['save'])){
$path="upload/";
$name = $_FILES['imageupload']['name'];//Name of the File
$temp = $_FILES['imageupload']['tmp_name'];
if(move_uploaded_file($temp, $path . $name)){
echo "success";
}else{
echo "failed";
}
}
?>
<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="imageupload">
<input type="submit" name="save" value="submit">
</form>
$_FILES undefined index : file
Correct way is:
<?php
if(isset($_POST['submit'])){
echo $_FILES['file']['error'];
}
?>
<form action="uploads.php" method = "POST" enctype="multipart/form-data">
<input name="file" type="file"><br>
<input type="Submit" value="Submit" name='submit'/>
</form>
You are echoing "$_FILES['file']['error']
" before form submit. echo input type file value or something after form submission.
$_FILES undefined index error
Try setting the proper enctype in your form tag:
<form action="wpis.php" method="POST" enctype="multipart/form-data">
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
Echo a Multi Dimensional Array
Key of Null Variable Equals Null Not Error
Display Price on Add to Cart Button from the Functions.PHP File in Woocommerce
PHP JSON_Encode and JavaScript Functions
.Htaccess Rewriterule: Two Domains Using Same Server and Directory
JSON Encode an Entire MySQL Result Set
Remove Everything Except Letters from PHP String
Output a PHP Multi-Dimensional Array to a HTML Table
Linkify Regex Function PHP Daring Fireball Method
PHP Twitter Replace Link and Hashtag with Real Link
Check If Method Exists in the Same Class
Php: Output Data Before and After Sleep()
PHP MySQL Set Connection Timeout