0

我的表格中有这个:

<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');
    }
4

1 回答 1

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;
        }
    }
于 2013-10-11T00:33:26.940 回答