file upload php $_FILES undefined index error

2019-01-23 16:37发布

<?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条回答
男人必须洒脱
2楼-- · 2019-01-23 16:39
<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>
查看更多
萌系小妹纸
3楼-- · 2019-01-23 16:44

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

查看更多
小情绪 Triste *
4楼-- · 2019-01-23 16:58

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>
查看更多
女痞
5楼-- · 2019-01-23 17:01

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.

查看更多
登录 后发表回答