How do I change the filename in my upload?

I am resizing an uploaded image and I want to store it in a folder with a random code (called $random).
But, I also want to change the filename of the uploaded file to the same random code.
But, where in this script can I make this?

I want the thumbnail image to be stored in the random code folder with the name $random.jpg

define('UPLOADED_IMAGE_DESTINATION', '../temp_photo/');
define('THUMBNAIL_IMAGE_DESTINATION', '../photo/'.$random.'/');

function process_image_upload($field)
{
    $temp_image_path = $_FILES[$field]['tmp_name'];
    $temp_image_name = $_FILES[$field]['name'];
    list(, , $temp_image_type) = getimagesize($temp_image_path);
    if ($temp_image_type === NULL) {
        return false;
    }
    switch ($temp_image_type) {
        case IMAGETYPE_GIF:
            break;
        case IMAGETYPE_JPEG:
            break;
        case IMAGETYPE_PNG:
            break;
        default:
            return false;
    }
    $uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name;
    move_uploaded_file($temp_image_path, $uploaded_image_path);
    $thumbnail_image_path = THUMBNAIL_IMAGE_DESTINATION . preg_replace('{\\\\.[^\\\\.]+$}', '.jpg', $temp_image_name);
    $result = generate_image_thumbnail($uploaded_image_path, $thumbnail_image_path);
    return $result ? array($uploaded_image_path, $thumbnail_image_path) : false;
}
$temp_image_name = $_FILES[$field]['name'];

Change that to use $random . ‘.jpg’;

Granted you need to tell the function about $random either using global $random or passing it as an argument.

I changed this, but in return I only get .jpg in return.
No filename at all. So, it doesn’t seem to work.

If I set $random to 1234 like this:

$random = '1234';

define('UPLOADED_IMAGE_DESTINATION', '../temp_photo/');
define('THUMBNAIL_IMAGE_DESTINATION', '../photo/'.$random.'/');

function process_image_upload($field)
{
    $temp_image_path = $_FILES[$field]['tmp_name'];
    $temp_image_name = $random . '.jpg';
    list(, , $temp_image_type) = getimagesize($temp_image_path);
    if ($temp_image_type === NULL) {
        return false;
    }
    switch ($temp_image_type) {
        case IMAGETYPE_GIF:
            break;
        case IMAGETYPE_JPEG:
            break;
        case IMAGETYPE_PNG:
            break;
        default:
            return false;
    }
    $uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name;
    move_uploaded_file($temp_image_path, $uploaded_image_path);
    $thumbnail_image_path = THUMBNAIL_IMAGE_DESTINATION . preg_replace('{\\\\.[^\\\\.]+$}', '.jpg', $temp_image_name);
    $result = generate_image_thumbnail($uploaded_image_path, $thumbnail_image_path);
    return $result ? array($uploaded_image_path, $thumbnail_image_path) : false;
}

Well, it does, but as I previously mentioned your function doesn’t have access to $random. You need to use global $random; (first line in your function) or pass $random to your function as an argument process_image_upload($field, $random)

As cpradio said, you need to tell the function that you are passing a new name into it.


function process_image_upload($field, $random)


Its called variable scope.

Nope, that doesn’t work either…

I must be stupid, but I think I’m doing it right…
It’s still a blank name and only .jpg

Here is my code now:

$random = '1234';

define('UPLOADED_IMAGE_DESTINATION', '../temp_photo/');
define('THUMBNAIL_IMAGE_DESTINATION', '../photo/'.$random.'/');

function process_image_upload($field, $random)
{
    $temp_image_path = $_FILES[$field]['tmp_name'];
    $temp_image_name = $random . '.jpg';
    list(, , $temp_image_type) = getimagesize($temp_image_path);
    if ($temp_image_type === NULL) {
        return false;
    }
    switch ($temp_image_type) {
        case IMAGETYPE_GIF:
            break;
        case IMAGETYPE_JPEG:
            break;
        case IMAGETYPE_PNG:
            break;
        default:
            return false;
    }
    $uploaded_image_path = UPLOADED_IMAGE_DESTINATION . $temp_image_name;
    move_uploaded_file($temp_image_path, $uploaded_image_path);
    $thumbnail_image_path = THUMBNAIL_IMAGE_DESTINATION . preg_replace('{\\\\.[^\\\\.]+$}', '.jpg', $temp_image_name);
    $result = generate_image_thumbnail($uploaded_image_path, $thumbnail_image_path);
    return $result ? array($uploaded_image_path, $thumbnail_image_path) : false;
}

Find where you are calling process_image_upload($field); and make sure to change it to process_image_upload($field, $random);

You can’t just change the function definition and assume everywhere you call that function will make use of the new definition.

Hmmm, I still don’t get it. There is no file name attached to the thumbnail.
The only thing I have after the above code, is this:

Is there anything I need to change there as well?

One page is handling the form for the upload. Then the user ends up on this page and the resize and upload process is handled.
And this is where I want the $random to replace the original filename. Then the image is supposed to end up in a folder with the $random as folder name and the image stored in that folder with the $random as file name with .jpg added.

But, after doing what you wrote above, I still just get an image called .jpg

$result = process_image_upload('Image1');
if ($result === false) {
    echo '<br>An error occurred while processing upload';
} else {
    echo '<br>Uploaded image saved as ' . $result[0];
    echo '<br>Thumbnail image saved as ' . $result[1];
}

You need to change the following line:

$result = process_image_upload('Image1'); 

to be

$result = process_image_upload('Image1', $random); 

So you are passing the value to the function.

Of course that had to be changed as well. Now it’s working.
I said I was stupid, right?

Thanks a lot… sometimes my head is spinning the wrong way and I start thinking about other things.
Or maybe too many projects running at the same time…