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

时间:2014-01-21 23:16:24

标签: php file upload indexing undefined

<?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包含在表单标签中,所以我无法弄清楚它是什么..任何人都可以帮助我吗?

6 个答案:

答案 0 :(得分:9)

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

您可以按照以下方式更改代码

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

答案 1 :(得分:5)

在上传文件时解析了php中未定义的索引 由于最大文件大小限制
xmlns:map="http://schemas.android.com/apk/res-auto" (...) <com.google.android.gms.maps.MapView (...) map:liteMode="true"/>

中的更改
php.ini

根据您的要求进行更改

答案 2 :(得分:4)

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

答案 3 :(得分:2)

如果您将整个代码用作一个文件(我怀疑您是),那么您需要使用条件语句执行以下操作,我在发布之前测试(并且正在工作)

另外,请确保您的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>

<强>脚注:

我添加了条件声明:

if(isset($_POST['submit']))

我命名了提交按钮:(与isset()条件语句一起使用)

<input type="submit" name="submit" value="Submit">

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

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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