上传文件时未定义索引 [英] Undefined Index while uploading file

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

问题描述

这是我的错误:

注意:未定义的索引:文件在 第21行的C:\ xampp \ htdocs \ Project \ Template1 \ users \ index.php注意: 未定义的索引:文件在 请在第23行输入C:\ xampp \ htdocs \ Project \ Template1 \ users \ index.php 上传

Notice: Undefined index: file in C:\xampp\htdocs\Project\Template1\users\index.php on line 21 Notice: Undefined index: file in C:\xampp\htdocs\Project\Template1\users\index.php on line 23 please uploaded

如何摆脱它?

HTML代码:

<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file"><br><br>
<input type="submit" value="submit" name="submit">
</form>

Php代码:

<?php

    $name = $_FILES['file']['name'];
    $temp_name = $_FILES['file']['temp_name'];

    if (isset($name)) {

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

        if (move_uploaded_file($temp_name, $location.$name)) {
            echo 'uploaded';
        }

    } else {
        echo 'please uploaded';
    }
?>

推荐答案

按如下所示更改您的PHP脚本并尝试

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

这篇关于上传文件时未定义索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆