Add 3 new image option to script

I have some code and at the moment its only dealing with 1 mage, I need to add the option of uploading 3 other images at the same time. I could sort of duplicate what I got another 3 times, but wondered if there was a simpler way to do it.

html

<input type="file" name="image" id="image" />

php

$validExtensions = array('.jpg', '.jpeg', '.gif', '.png');	

function fileWasUploaded($name)
{
if (!file_exists($_FILES[$name]['tmp_name']))
{
return false;
}

return is_uploaded_file($_FILES[$name]['tmp_name']);
}

$image = "no-image.jpg";
if (fileWasUploaded("image") === true)
{
$fileExtension = strrchr(strtolower($_FILES['image']['name']), ".");
if (!in_array($fileExtension, $validExtensions))
{
 echo "Uploaded file was not an image";
}
else
{
$image = $_POST["url"] . ".jpg";
$destination = '../images/products/' . $image;
$thumbnail = '../images/products/thumbs/' . $image;
$manipulator = new ImageManipulator($_FILES['image']['tmp_name']);
$manipulator->save($destination);
$newWidth = 150;
$factor = $manipulator->getWidth() / $newWidth;
$manipulator->resample($newWidth, $manipulator->getHeight() / $factor, false);
$manipulator->save($thumbnail);
}
}

There an include that goes with it which is called imagemanipulator.php which by the looks is just changing the size of it before upload.

I forgot to add the insert part

$sql = "INSERT INTO Products (image) VALUES ('$image')";

The easiest way to me would be to enclose the second section of your code in a function, define the additional input fields in your form and run a loop through those, calling the function for each one and passing in the form field name each time.

Hi droopsnoot, sorry to ask but could you show me.

When you got time if you do :wink:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.