File Upload PHP $_Files Undefined Index Error

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



Leave a reply



Submit