Jump to content

Renaming files


Craig Hopson

Recommended Posts

hi guys have this code which works file but i need to rename the file after its converted (xxxx.png to xxxx.jpg)

$originalimg = test.png$srcImg = imagecreatefrompng($originalimg);    imagejpeg($srcImg,$originalimg,100);    imagedestroy($srcImg);

Link to comment
Share on other sites

yes i looked at that but if the file name changes like

$originalimg = $_GET['img']; $srcImg = imagecreatefrompng($originalimg);		imagejpeg($srcImg,$originalimg,100);		imagedestroy($srcImg);

how would i just change the extension from .png to .jpg but keep the file name the same

Edited by Craig Hopson
Link to comment
Share on other sites

yes i looked at that but if the file name changes like
$originalimg = $_GET['img']; $srcImg = imagecreatefrompng($originalimg);		imagejpeg($srcImg,$originalimg,100);		imagedestroy($srcImg);

how would i just change the extension from .png to .jpg but keep the file name the same

I see, just rename it to jpg or jpeg instead png or just change the name then saving the file and remove the old one.
Link to comment
Share on other sites

ok i done this is it correct or is there a better way??

$srcImg = imagecreatefrompng($originalimg);    imagejpeg($srcImg,$originalimg,100);    imagedestroy($srcImg);    $NewName = str_replace(".png",".jpg",$originalimg);    rename($originalimg,$NewName);

Link to comment
Share on other sites

ok i done this is it correct or is there a better way??
$srcImg = imagecreatefrompng($originalimg);	imagejpeg($srcImg,$originalimg,100);	imagedestroy($srcImg);	$NewName = str_replace(".png",".jpg",$originalimg);	rename($originalimg,$NewName);

What works until someone has .png in the filename and not as an extension. Try with explode and take away everything after the last dot.
Link to comment
Share on other sites

Thanks do you have example
$newfile = explode(".",$originalimg);array_pop($newfile);$newfile = implode(".",$newfile) . ".png";rename($originalimg, $newfile);

  • Like 1
Link to comment
Share on other sites

hi bit different problem but same topic so here goes, this code will change Avatar.png to Avatar1339516953.png

$filename .= time();

how would i get 1339516953_Avatar.png

Link to comment
Share on other sites

$filename=time() . '_avatar.png';

  • Like 1
Link to comment
Share on other sites

hi bit different problem but same topic so here goes, this code will change Avatar.png to Avatar1339516953.png
$filename .= time();

how would i get 1339516953_Avatar.png

are you sure it wouldn't actually change it to Avatar.png1339516953? Unless there's something you're leaving out of the picture... to answer your question though,
$filename = time() . '_' . $filename;

Edited by thescientist
Link to comment
Share on other sites

yes there is another variable $ext holding the extension thanks i have sorted it like this

$filenameA   =   time() . '_'.$filename;$filename    =  $filenameA;

Edited by Craig Hopson
Link to comment
Share on other sites

You can get the extension, as well as the base name and directory name of a file with the pathinfo() function, which should significantly ease what you're trying to do.You could just do:

$filename_parts = pathinfo($filename, PATHINFO_BASENAME | PATHINFO_EXTENSION);$filename_parts['basename'] = time() . '_' . $filename_parts['basename'];$filename = implode('.', $filename_parts);

Link to comment
Share on other sites

Guest So Called
cos that was the only way i could get it to do what i wanted
Why not this?
$filename   =   time() . '_'.$filename;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...