Jump to content

how to rename a file in php?


helloise

Recommended Posts

i have the following code:

$filename = $this->getMsisdn().".png";

if (!file_exists(sfConfig::get('sf_upload_dir') . '/rainbowcode/images/profilepics/'.$filename))

{

    $filename = "Rainbow-code-1_blck.jpg";

    //rename it to msisdn

               

    $source = imagecreatefromjpeg($filename);

}

 

i JUST want to rename "Rainbow-code-1_blck.jpg" to $this->getMsisdn().".jpg" ??

is this possible?? i want to have the SAME picture but it has a different name

 

please help?

thank you

Link to comment
https://forums.phpfreaks.com/topic/234137-how-to-rename-a-file-in-php/
Share on other sites

try:

$filename = $this->getMsisdn().".png";
if (!file_exists(sfConfig::get('sf_upload_dir') . '/rainbowcode/images/profilepics/'.$filename))
{
     $filein = "Rainbow-code-1_blck.jpg";               
//     $source = imagecreatefromjpeg($filein);
      imagejpeg($filein, $this->getMsisdn().'.jpg')
}

i need to use filename further in the code i now have:

$filename = $this->getMsisdn().".png";

if (!file_exists(sfConfig::get('sf_upload_dir') . '/rainbowcode/images/profilepics/'.$filename))

            { 

                $file_in = "Rainbow-code-1_blck.jpg";

                $using_default_pic = 1;           

            }

           

            if ($using_default_pic == 1)

            {

                $using_default_pic = 0;

                echo "filename before rename ".$file_in;

                $filename = rename($file_in,$this->msisdn."jpg");

                echo "calling resize image after rename: ".$filename.'<br />';

            } 

 

echo file_in gives:Rainbow-code-1_blck.jpg

 

and echo "calling resize image after rename: ".$filename.'<br />'; gives nothing????

please help?

thank you

 

thanks  i fixed it but still not working :(

 

echo "filename before rename ".$file_in;  //gives: Rainbow-code-1_blck.jpg

$filename = rename($file_in,$this->getMsisdn());

echo "calling resize image after rename: ".$filename.".jpg".'<br />';  //here $filename is empty???

 

yikes please help?

thank you

 

thank you but can you help me with this please and show me how to achieve this so that i rename Rainbow-code-1_blck.jpg to $this->getMsisdn().".jpg"

and assign $filename the value of $this->getMsisdn().".jpg"  because i need to use $filename further on in my code

 

thank you

$file_in = "Rainbow-code-1_blck.jpg"; 
$filename = $this->getMsisdn().".jpg";

if(!rename($file_in, $filename)) echo "Failed to rename image.";

You do understand that as soon as you run this once, the code will fail if you try to run it again, right? It won't be able to find 'Rainbow-code-1_blck.jpg' because it's been renamed. That's why I was suggesting you make a copy of it (under that new name). To do that just replace the rename call with copy.

yikes maybe i must explain:

 

a user registers a profile online and if he DOES NOT upload a profile pic i must use a default pic which is: "Rainbow-code-1_blck.jpg";

if he DOES upload a pic the profile pic will be saved as : $this->getMsisdn().".jpg" and i carry on with my code as per usual

 

BUT

 

if $this->getMsisdn().".jpg does not exist(user did not upload a profile pic) i must use Rainbow-code-1_blck.jpg BUT further along my code i need the file name to be

$this->getMsisdn() AND the picture is the actual default picture just renamed....

 

hope this makes sense???

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.