php 上传文件时未定义索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19027992/
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 18:41:18  来源:igfitidea点击:

Undefined Index while uploading file

php

提问by Ashish Singh

This is my error:

这是我的错误:

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

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

How to get rid of it?

如何摆脱它?

Html Code:

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

代码:

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

采纳答案by Sherin Jose

Change your PHP script as below and try

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

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

回答by Nishit Zinzuvadiya

Make sure you have set form attributeenctype="multipart/form-data".

确保您已设置form attributeenctype="multipart/form-data".

This attribute help you to get files from user.

此属性可帮助您从用户那里获取文件。

<form action="PATH" method="get/post" enctype="multipart/form-data"></form>

回答by Arshad Ameen

Make sure you have set form attribute enctype="multipart/form-data". This attribute help you to get files from user.

确保您已设置表单属性enctype="multipart/form-data"。此属性可帮助您从用户那里获取文件。

<form action="#" method="get/post" enctype="multipart/form-data">
</form>

回答by Zzmilanzz Zzmadubashazz

this happens due to the size of the file :

发生这种情况是由于文件的大小:

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

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

in your php.ini file you should change above codes according to your requirement...

在您的 php.ini 文件中,您应该根据您的要求更改上述代码...

回答by Pupil

Spelling mistake:

拼写错误:

<?php
$name = $_FILES['file']['name'];

$temp_name = $_FILES['file']['tmp_name']; // tmp_name

if(isset($name)){
if(!empty($name)){

$location = '../uploads/';
}
if(move_uploaded_file($temp_name, $location.$name)){
echo 'uploaded';
}
}  else {
echo 'please uploaded';
}
?>

回答by Mipzhap

Do a check around your PHP code block checking if either the submit button has been pressed or if isset($_FILES['file']). This should remove your errors. They pop up because the $_FILES['file'] isn't populated before the submit button is pressed.

检查您的 PHP 代码块,检查是否已按下提交按钮或isset($_FILES['file']). 这应该会消除您的错误。它们弹出是因为在按下提交按钮之前没有填充 $_FILES['file']。

回答by chobela

Usually, the problem is forgetting to add this line as a form tag attribute.

通常,问题是忘记将此行添加为表单标记属性。

enctype="multipart/form-data"

enctype="multipart/form-data"

The enctype attribute specifies how the form-data should be encoded when submitting it to the server.

enctype 属性指定表单数据在提交到服务器时应如何编码。

Note: The enctype attribute can be used only if method="post".

注意: enctype 属性只能在 method="post" 时使用。

回答by Mubin

$upload_dir="../uploads";
$target_file="";
$tmp_file="";
if(isset($_POST['submit']))
{

        $tmp_file=$_FILES['file']['tmp_name'];
        $target_file=basename($_FILES['file']['name']);
            if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file))
        {   
        echo "File uploaded <br />";

        }
        else {
              echo "Something went Wrong !!<br/>";
            }
}

回答by sumit kumar pradhan

if you are getting Notice: Undefined index: zip_file inerror message most of the time, While uploading any file to server using php, Then here is the solution for this. only you need to mention enctypetype in form tag.

如果您收到通知:Undefined index: zip_file大部分时间错误消息中,同时使用 php 将任何文件上传到服务器,那么这里是解决方案。只需要在表单标签中提及enctype类型。

<form method="post" action="" name="login" enctype="multipart/form-data">

回答by fvlgnn

Check if file_uploadsis enabled on your php.ini

检查您的 php.ini 是否启用了file_uploads

file_uploads = On