文件上传 php $_FILES 未定义索引错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21271060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:58:53  来源:igfitidea点击:

file upload php $_FILES undefined index error

phpfileuploadindexingundefined

提问by michAmir

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

我收到“通知:未定义索引”错误消息。enctype 包含在表单标签中,所以我无法弄清楚它是什么.. 谁能帮我?

回答by Philipp

The first assignment throws an warning, if nothing is uploaded and the isset test is a little bit useless..

如果没有上传任何内容并且isset测试有点无用,则第一个作业会抛出警告。

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>

回答by Randeep Singh

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


由于最大文件大小限制的
变化,在上传文件时解决了 php 中的未定义索引php.ini

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

change according to your requirements

根据您的要求更改

回答by Mr. Kerimov

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

回答by Funk Forty Niner

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 uploadsfolder has proper write permissions set and it exists.

另外,请确保您的uploads文件夹设置了正确的写入权限并且它存在。

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

我将提交按钮命名为:(与isset()条件语句结合使用)

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

注意:如果您实际上将发布的代码用作两个单独的文件,那么您可以简单地复制此答案中的 PHP,同时将单独的 HTML 表单中设置的当前提交按钮命名为name="submit"upload_form.htm例如调用您的表单)作为我确实如上所示,但保留action="upload.php"并相应地命名了 PHP 上传处理程序文件。

回答by Salman Aziz

// 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";
}

回答by Kumar Pal

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