PHP fileupload为数组

时间:2017-12-07 09:53:56

标签: php file-upload

我现在已经在这看了一两天 当我将多个文件上传到此脚本时," $ finalfilename"从第二个文件中提取多个文件名。 这是我的代码:

include ($_SERVER['DOCUMENT_ROOT'] . "/config/config.php");

$valid_extensions = array(
    'jpeg',
    'jpg',
    'png',
    'gif',
    'bmp'
); // valid extensions
$path = $_SERVER['DOCUMENT_ROOT'] . '/assets/uploads/'; // upload directory
$uploadOK = 1;
$album = strip_tags($_POST['newPostForm-Album']);
$i = 0;

foreach($_FILES['NEW-POST-FORM_IMAGES']['name'] as $file)
{
    $imgname = $_FILES['NEW-POST-FORM_IMAGES']['name'][$i];
    $tmpname = $_FILES['NEW-POST-FORM_IMAGES']['tmp_name'][$i];
    $timestamp = time();
    $extension = strtolower(pathinfo($imgname, PATHINFO_EXTENSION));
    $newfilename = sha1(time() . $i);
    $finalfilename = $newfilename . "." . $extension;
    if ($_FILES['NEW-POST-FORM_IMAGES']["size"][$i] > 500000)
    {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }

    if ($uploadOK)
    {
        if (in_array($extension, $valid_extensions))
        {
            $path = $path . strtolower($finalfilename);
            if (move_uploaded_file($tmpname, $path))
            {

                // mysqli_query($con, "INSERT INTO posts VALUES('', '$album', '$finalfilename', '$timestamp')");

                echo $_FILES['NEW-POST-FORM_IMAGES']['name'][$i];
            }
            else
            {
                echo "error!";
            }
        }
    }

    $imgname = "";
    $tmpname = "";
    $timestamp = "";
    $extension = "";
    $newfilename = "";
    $finalfilename = "";
    $i++;
}

正如您所看到的,我在添加$ i之前尝试重置所有字符串。

更新

我尝试使用$ file而不是$ _FILES(echo $ file [' name'] [$ i];) 这让我回到了这个警告:     非法字符串偏移'名称'在

第二个文件($ finalfilename)的输出也为我提供了文件名' .extention' filename' .extention     ea5816965b01dae0b19072606596c01efc015334.jpeg21aa3008f90c89059d981bdc51b458ca1954ab46.jpg

需要分开。

我只需要单独获取每个文件的文件名。

谢谢!

1 个答案:

答案 0 :(得分:2)

问题出在$path变量中。

$path = $_SERVER['DOCUMENT_ROOT'] . '/assets/uploads/';放入循环中。您也可以从最后删除变量重置。

foreach ($_FILES['NEW-POST-FORM_IMAGES']['name'] as $file) {
    $path = $_SERVER['DOCUMENT_ROOT'] . '/assets/uploads/';
    $imgname = $_FILES['NEW-POST-FORM_IMAGES']['name'][$i];
    $tmpname = $_FILES['NEW-POST-FORM_IMAGES']['tmp_name'][$i];
    $timestamp = time();
    $extension = strtolower(pathinfo($imgname, PATHINFO_EXTENSION));
    $newfilename = sha1(time() . $i);
    $finalfilename = $newfilename . "." . $extension;
    if ($_FILES['NEW-POST-FORM_IMAGES']["size"][$i] > 500000)
    {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }

    if ($uploadOK)
    {
        if (in_array($extension, $valid_extensions))
        {
            $path = $path . strtolower($finalfilename);
            if (move_uploaded_file($tmpname, $path))
            {

                // mysqli_query($con, "INSERT INTO posts VALUES('', '$album', '$finalfilename', '$timestamp')");

                echo $_FILES['NEW-POST-FORM_IMAGES']['name'][$i];
            }
            else
            {
                echo "error!";
            }
        }
    }

    $i++;
}

还有更多内容需要更新,而不是foreach for / while在这里会更好,或者以其他方式使用foreach(在循环体中使用$file),等等。将$path移动到循环中如何解决问题是最简单的方法。