3

我有一个表格,可以将多个选定的文件上传到文件夹。我只需要找到如何使用 PHP 将它们插入数据库?

HTML 表单

<form name="demoFiler" id="demoFiler" enctype="multipart/form-data">
<input type="file" name="multiUpload" id="multiUpload" multiple />
<input type="submit" name="submitHandler" id="submitHandler" value="Upload" class="buttonUpload" />
</form>

PHP 代码

if($_SERVER['REQUEST_METHOD'] == "POST"){
    if(move_uploaded_file($_FILES['file']['tmp_name'], "uploads/".$_FILES['file']['name'])){
        echo($_POST['index']);
    }
    exit;
}

这是我的代码,首先我无法将文件移动到上传文件夹:

<?php

$username = "root";
$password = "";
$host = "localhost";
$database = "db_holiday";

// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

// Select your database
mysql_select_db ($database);

if($_SERVER['REQUEST_METHOD']=="POST")
{
    foreach ($_FILES['multiUpload']['name'] as $fileName) { 
        $uploaddir= '/upload/';
    $uploadfile = $uploaddir . basename($fileName);
        if (move_uploaded_file($fileName, $uploadfile)) {
            echo "File is valid, and was successfully uploaded.\n";
        } else {
            echo "Possible file upload attack!\n";
        }
    }
}

?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="multiUpload[]" id="multiUpload" multiple="multiple" />
<input type="submit" name="button"/>
</form>
4

2 回答 2

4

HTML:

<input type="file" name="multiUpload[]" id="multiUpload" multiple />

php:

foreach ($_FILES['multiUpload'] as $fileName) { 
    //write your code here
    //$fileName
}
于 2012-12-25T09:45:28.440 回答
0

使用 2 个input type="file"元素

<input type="file" name="img_1" />
<input type="file" name="img_2" />

使用访问每个文件

$_FILES['img_1'][];
$_FILES['img_2'][];

或者您也可以使用类似的数组

<input type="file" name="img[]" />
<input type="file" name="img[]" />
<!--Which is often used in checkboxes-->

并使用访问每个图像

$_FILES['img']['tmp_name'][0];
$_FILES['img']['tmp_name'][1];
于 2012-12-25T09:43:21.650 回答