php – 上传文件时未定义的索引

前端之家收集整理的这篇文章主要介绍了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 
 if(isset($_POST['submit'])){
    $name       = $_FILES['file']['name'];  
    $temp_name  = $_FILES['file']['tmp_name'];  
    if(isset($name)){
        if(!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 !!';
    }
}
?>
原文链接:https://www.f2er.com/php/138569.html

猜你在找的PHP相关文章