13
<?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??

4
  • Is your entire code inside one file, or are they split into two seperate files? Commented Jan 21, 2014 at 23:20
  • 1
    @Fred -ii- in single , 200% Commented Jan 21, 2014 at 23:22
  • I agree. I was about to post an answer to that affect, but it's already been taken care of below. @voodoo417 Commented Jan 21, 2014 at 23:24
  • Actually, I changed my mind. I posted a working copy below. @voodoo417 Commented Jan 21, 2014 at 23:26

6 Answers 6

10

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

<?php

if (isset($_FILES["file"]["name"])) {

    $name = $_FILES["file"]["name"];
    $tmp_name = $_FILES['file']['tmp_name'];
    $error = $_FILES['file']['error'];

    if (!empty($name)) {
        $location = 'uploads/';

        if  (move_uploaded_file($tmp_name, $location.$name)){
            echo 'Uploaded';
        }

    } else {
        echo 'please choose a file';
    }
}
?>

<form action="test.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="file"><br><br>
    <input type="submit" value="Submit">
</form>
Sign up to request clarification or add additional context in comments.

4 Comments

thanks! the undefined index error is gone but now i get this error:
Warning: move_uploaded_file(uploads/testhoes.jpg): failed to open stream: No such file or directory in /var/www/upload.php on line 14. Warning: move_uploaded_file(): Unable to move '/tmp/phpnvksz8' to 'uploads/testhoes.jpg' in /var/www/upload.php on line 14
sorry the errors above are 2 errors but i dont know how to put space between them
The upload directory must exist and have write permissions (i.e. 755)
5

Resolved Undefined Index in php while uploading file
due to maximum file size limit
changes in php.ini

`max_execution_time` = 300  
`max_input_time` = 240  
`post_max_size` = 128M
`upload_max_filesize` = 128M

change according to your requirements

1 Comment

Location inside xampp: xampp/php/php.ini
4
<form action="test.php" method="POST" enctype="multipart/form-data"> /* mistake here: change test.php to your source: upload.php */
    <input type="file" name="file"><br><br>
    <input type="submit" value="Submit">
</form>

Comments

3

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.

<?php

if(isset($_POST['submit'])){
$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="" method="POST" enctype="multipart/form-data">
    <input type="file" name="file"><br><br>
    <input type="submit" name="submit" value="Submit">
</form>

Footnotes:

I added a conditional statement:

if(isset($_POST['submit']))

and I named the submit button: (to work in conjunction with the isset() conditional statement)

<input type="submit" name="submit" value="Submit">

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 form upload_form.htm for example) as I did shown above, yet retaining the action="upload.php" and naming the PHP upload handler file accordingly.

21 Comments

just like the answer from phillip the undefined index error is gone. Thanks!
but now i get this error: Warning: move_uploaded_file(uploads/testhoes.jpg): failed to open stream: No such file or directory in /var/www/upload.php on line 14. Warning: move_uploaded_file(): Unable to move '/tmp/phpnvksz8' to 'uploads/testhoes.jpg' in /var/www/upload.php on line 14
Does the folder exist and is it writeable? @user3221348 meaning, does it have the proper write permissions?
You haven't answered my question. @user3221348
the tmp/ folder or the upload/ folder?
|
1
1. You hadn't mention name value in your submit button.
2. Use isset function.

<html>
<body>

<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="file"><br><br>
    <input type="submit" value="Submit" name="submit">
</form>

</body>
</html>



<?php

if(isset($_POST['submit'])){

$name = $_FILES["file"]["name"];

echo $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';
          }
    }
}       
?>

Comments

0
// Count total files
$countfiles = count($_FILES['event_Img']['name']);
for($i=0;$i<$countfiles;$i++){
    $filename = $_FILES['event_Img']['name'][$i];

    // Get extension
    $ext = end((explode(".", $filename)));
    move_uploaded_file($_FILES['event_Img']['tmp_name'][$i], "uploads/".$filename);
    $sqlBrand = 'INSERT INTO ot_event_images 
                SET 
                event_id=:event_id, 
                imagepath=:imagepath, 
                imagemimetype=:imagemimetype';
    $query2 = $conn->prepare($sqlBrand);
    $query2->bindParam(':event_id', $eventid);
    $query2->bindParam(':imagepath', $filename);
    $query2->bindParam(':imagemimetype', $ext);
    $status2 = $query2->execute();
}
if($status2)
{

    echo "File upload successfully";
}
else
{
    echo "error";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.