PHP / HTML表单上传多个图像

时间:2013-10-10 22:17:50

标签: php

我的形式是:

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

然后在表单的操作页面上显示这个PHP:

$files = array();
$fdata = $_FILES["images"];
if(is_array($fdata["name"]))
{
    //This is the problem
    for ($i = 0; $i < count($fdata['name']); ++$i)
    {
        $files[] = array(
        'name' => $fdata['name'][$i],
        'tmp_name' => $fdata['tmp_name'][$i],
        );
    }
}
else
{
    $files[] = $fdata;
}

foreach ($files as $file)
{
    move_uploaded_file ( "$pic1_name","$image1") or die("image 1 did not copy<br>");
}

但我知道move_uploaded_file行错了。我需要在这里放什么呢?

更新:

我的最新代码如下:

$files = array();
    $fdata = $_FILES["images"];
    if(is_array($fdata["name"]))
    {
        //This is the problem
        for ($i = 0; $i < count($fdata['name']); ++$i)
        {
            $files[] = array(
            'name' => $fdata['name'][$i],
            'tmp_name' => $fdata['tmp_name'][$i],
            );
        }
    }
    else
    {
        $files[] = $fdata;
    }

    foreach($_FILES['images'] as $file)
    {
        echo $file['tmp_name'];
        move_uploaded_file($file['tmp_name'], $_SERVER["DOCUMENT_ROOT"].'/img/project-gallery/test');
    }

1 个答案:

答案 0 :(得分:0)

我会尝试这样的东西...当然要添加一些验证,以确保用户只上传您期望的文件类型

    $currentDate = microtime();

    $storeFolder = $_SERVER["DOCUMENT_ROOT"] . "/yourSavePath/";
    $fileNames = array();
    foreach ($_FILES['images']['name'] as $key => $value) {

        if (!empty($_FILES['images'])) {

            //tempfile is the file which has actually been stored on your server
            $tempFile = $_FILES['images']['tmp_name'][$key];

            //prepend the time so to make filenames unique...the filename here is the one the user sees and the temp filename above is one generated by the server

            $targetFile1 = $currentDate . $_FILES['images']['name'][$key];

            //get rid of spaces in the name since they can cause issues
            $targetFile1 = str_replace(' ', '_', $targetFile1);

            $res = move_uploaded_file($tempFile, $storeFolder . $targetFile1);
            $fileNames[] = $targetFile1;
        }
    }