如何使用name =“image []”简单地上传多个文件

时间:2013-04-09 14:19:36

标签: php file-upload

我是php的新用户,想询问使用name="image[]"上传多个文件的问题。这是我的代码:


HTML

<form action="" method="post" enctype="multipart/form-data">
  <input type="file" name="image[]" multiple="multiple"/>
  <input type="submit" name="submit"/>
</form>

PHP

<?php

    $table_name ="posts";

    $_POST['submit'];
    $title = $_POST['title'];
    $cat_id = $_POST['cat_id'];
    $date = date('Y-m-d', strtotime($_POST['date']));
    $description = mysql_real_escape_string($_POST['description']);
    $status = $_POST['status'];


    $sql = "INSERT INTO $table_name (title,cat_id,date,description,status) VALUES ('$title','$cat_id','$date','$description','$status')";
    $result = mysql_query($sql);                
    $sqlImage = mysql_fetch_array(mysql_query("SELECT id FROM $table_name ORDER BY id DESC "));
    $parentId = $sqlImage['id'];
    foreach ($_POST['image'] as $images) {
        $resImage = mysql_query("INSERT INTO media (parent_id,image) VALUES ('$parentId','$images') ");
    }
    $_SESSION['add_content'] = true;    

   echo ' <script>location.href="'.$base_url.''.$table_name.'"</script>';


?>

如何输入图像?

1 个答案:

答案 0 :(得分:0)

首先,$_POST['submit']目前没有做任何事情。其次,上传文件需要您使用$_FILES而不是$_POST

这应该可以上传所有文件

foreach($_FILES['image'] as $file)
{
    //do something with the data
}

PHP manual : $_FILES
希望这有帮助!