Imagecopyresampled() - How Do You Edit A Filename?

Okay so imagecopyresampled()… let’s say I just uploaded the file funnycat.jpg, so for the thumbnail I want to call it funnycatthm.jpg. How would I do that??? When you get the filename it automatically includes the extension. But it (http://www.php.net/manual/en/function.imagecopyresampled.php) says to enter the “Destination image link resource” as a parameter. So that means the entire file name (directories included and all, like site.com/images/coolstuff/funnycat.jpg), right? So how the heck do I add something before the extension to give the new file a name? :sick: I’m so confused.

Any help with this would be immensely appreciated.

You can use imagejpeg to output a jpg file to whatever file name you like.

Well, I understand that I can give it whatever name I want, but the images that are upload have file names that are generated for them, so I would like to just be able to tack something onto the end of it (such as ‘thm’), I want to know if it’s possible to do that.

Is there a way for it to tell me what the extension is? Even that would be helpful (since, for example JPEGs can be either jpg or jpeg), because then I could just take off the file extension, add what I want, and then stick it back on…

What is the code that generates the file names for the uploaded files? I would think you should be able to adjust that code to add ‘thm’ to the file name.

$now = time();
while (file_exists($uploadFileName = $uploaddir . $now . '-' . $_FILES['file']['name']))
{
	$now++;
}

That’s the code, but I was just thinking that I can just add what I want to the beginning instead of the end ^_^; -sigh- My problems are always so simple to solve.

What you want to do is separate the filename bit from the extension, append what you want to the filename, and then glue them back together. Like so:


// assume uploaded file
$filename=$_FILES['myfile']['name'];


// get the position of the last (!) dot in the filename
$pos=strrpos($filename, '.')

// get the file name
$name=substr($filename, 0, $pos);

// get the extension
$ext=substr($filename, $pos + 1);

// set the new name
$newName=$name.'hth';

// glue back together
$newFilename=$newName.'.'.$ext;

Note that this uses $ext without the dot in the extension, which makes it easer if you want to check the extension later; ‘jpg’ is easier to read and type than ‘.jpg’ :slight_smile:

Ooooooh; I knew there must’ve been a way to do that. Thank you!!! :smiley: