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
Share on other sites

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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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???

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.