Jump to content

Need help to modify script upload images


Aganju

Recommended Posts

Hello,

I need help to modify this script to upload images. Unfortunately I am not very skilled in php.

This script is working and I need to change in order to upload many images at once.

 

The form

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

 

The first function addProduct() is responsible for receiving data from the form


function addProduct()
{
$images = uploadProductImage('fleImage', SRV_ROOT . 'images/product/');

$mainImage = $images['image'];
$thumbnail = $images['thumbnail'];

$sql = "INSERT INTO tbl_product (cat_id, pd_name, pd_description, pd_price, pd_qty, pd_image, pd_thumbnail, pd_date)
VALUES ('$catId', '$name', '$description', $price, $qty, '$mainImage', '$thumbnail', NOW())";

$result = dbQuery($sql);

header("Location: index.php?catId=$catId");
}

 

The second function uploadProductImage() makes a copy and resize the image and then move to the specified folder

 


function uploadProductImage($inputName, $uploadDir)
{
$image = $_FILES[$inputName];
$imagePath = '';
$thumbnailPath = '';

// if a file is given
if (trim($image['tmp_name']) != '') {
$ext = substr(strrchr($image['name'], "."), 1); //$extensions[$image['type']];

// generate a random new file name to avoid name conflict
$imagePath = md5(rand() * time()) . ".$ext";

list($width, $height, $type, $attr) = getimagesize($image['tmp_name']);

// make sure the image width does not exceed the
// maximum allowed width
if (LIMIT_PRODUCT_WIDTH && $width > MAX_PRODUCT_IMAGE_WIDTH) {
$result = createThumbnail($image['tmp_name'], $uploadDir . $imagePath, MAX_PRODUCT_IMAGE_WIDTH);
$imagePath = $result;
} else {
$result = move_uploaded_file($image['tmp_name'], $uploadDir . $imagePath);
}

if ($result) {
// create thumbnail
$thumbnailPath = md5(rand() * time()) . ".$ext";
$result = createThumbnail($uploadDir . $imagePath, $uploadDir . $thumbnailPath, THUMBNAIL_WIDTH);

// create thumbnail failed, delete the image
if (!$result) {
unlink($uploadDir . $imagePath);
$imagePath = $thumbnailPath = '';
} else {
$thumbnailPath = $result;
}
} else {
// the product cannot be upload / resized
$imagePath = $thumbnailPath = '';
}

}

return array('image' => $imagePath, 'thumbnail' => $thumbnailPath);
}


 

Now, I know I should use a foreach to process several images that I get from the form, but how can I do that?

 

thanks for helping :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.