沃梦达 / 编程问答 / php问题 / 正文

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

file upload php $_FILES undefined index error(文件上传 php $_FILES 未定义索引错误)

本文介绍了文件上传 php $_FILES 未定义索引错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我收到通知:未定义索引"错误消息.enctype 包含在表单标签中,所以我无法弄清楚它是什么.. 任何人都可以帮助我吗??

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

推荐答案

第一次赋值抛出警告,如果什么都没上传,而且isset测试有点没用..

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>

这篇关于文件上传 php $_FILES 未定义索引错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:文件上传 php $_FILES 未定义索引错误

基础教程推荐