Question

I am using uploadify to upload images to my website.

it is a great product, but I cannot get thumbnails working properly.

I have adapted some code I found, it works fine, it uploads as many images at once that I like, but as it uploads each image overwrites the previous image, so that only one image and one thumb remain in the upload folder

Here is the code I am using, any help would be great

<?php

// Define a destination
$targetFolder = '../uploadifythumbtest'; // Relative to the root
$thumbsFolder = '../uploadifythumbtest/thumbs/'; // Relative to the root



 if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $targetFolder;
$fileParts = pathinfo ( $_FILES ['Filedata'] ['name'] );
$extension = $fileParts ['extension'];
$filename = "artist_" . $yourid . "." . $fileParts ['extension'];
$targetFile = rtrim ( $targetPath, '/' ) . '/' . $filename;
$targetThumb = rtrim ( $thumbsFolder, '/' ) . '/' . $filename;

// Validate the file type
$fileTypes = array (
        'jpg',
        'jpeg',
        'gif',
        'png',
        'JPG',
        'bmp' 
); // File extensions

if (in_array ( $fileParts ['extension'], $fileTypes )) {
    // CREATE THUMBNAIL
    if ($extension == "jpg" || $extension == "jpeg") {
        $src = imagecreatefromjpeg ( $tempFile );
    } else if ($extension == "png") {
        $src = imagecreatefrompng ( $tempFile );
    } else {
        $src = imagecreatefromgif ( $tempFile );
    }

    list ( $width, $height ) = getimagesize ( $tempFile );

    $newwidth = 50;
    $newheight = ($height / $width) * $newwidth;
    $tmp = imagecreatetruecolor ( $newwidth, $newheight );

    imagecopyresampled ( $tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width,           $height );

    $thumbname = $targetThumb;



    imagejpeg ( $tmp, $thumbname, 100 );

    imagedestroy ( $src );
    imagedestroy ( $tmp );

    move_uploaded_file($tempFile,$targetFile);
    echo '1';
} else {
    echo 'Invalid file type.';
}
}
?>
Was it helpful?

Solution

The filename used to save the image is created in these line:

$filename = "artist_" . $yourid . "." . $fileParts ['extension'];

So every file you upload get's the same name.

You could use the original filenames filenames:

$filename = $fileParts['filename']. "." . $fileParts ['extension'];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top