0

I've been looking in this for a day or two now. When I upload multiple files to this script, "$finalfilename" gives me back multiple filenames from the second file.

Here's my code:

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++;
}

As you can see, I tried resetting all the strings at the end before adding $i.

UPDATE

I tried using $file instead of $_FILES (echo $file['name'][$i];) This gives me back this warning: Illegal string offset 'name' in

also the output of the second file ($finalfilename) gives me 'filename'.extention'filename'.extention ea5816965b01dae0b19072606596c01efc015334.jpeg21aa3008f90c89059d981bdc51b458ca1954ab46.jpg

Which need to be separated.

I need to only get the filename of each file separately.

Thank you!

10
  • 1
    Instead of $_FILES['NEW-POST-FORM_IMAGES']['name'][$i] in your loop you should just use $file I think. Commented Dec 7, 2017 at 9:57
  • @xander: it should be the same. Commented Dec 7, 2017 at 9:57
  • @xander as panther said, it is the same, I tried both, thanks for the help anyway ;) Commented Dec 7, 2017 at 9:59
  • 1
    Still recommend using $file else you could also go for a while loop. Commented Dec 7, 2017 at 10:00
  • What exactly means give's me back multiple filenames from the second file.? Please add a dump from $_FILES['NEW-POST-FORM_IMAGES']['name'] too. Commented Dec 7, 2017 at 10:01

1 Answer 1

2

Problem is in $path variable.

Put $path = $_SERVER['DOCUMENT_ROOT'] . '/assets/uploads/'; into the loop. You can remove variable reseting from the end too.

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++;
}

There are more thing to update, instead of foreach for/while would be better here, or using foreach in else way (use $file in the loop body), etc. Moving $path into loop is easiest way how to fix your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Got it! Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.