Solved

Rename a file and keep file extension

Posted on 2011-09-23
5
295 Views
Last Modified: 2012-05-12
Hi,

As in title, I need to rename a file with php, so here's the code
$file = "filenamehere.pdf"'
$file_extension = /* I need a help here */
rename("$file", "renamedfile".$file_extension);
All I need is a code which will recognize file extension of $file;
Or maybe there's some easier way to do this?

Thanks for any help.
0
Comment
Question by:Zado
  • 3
  • 2
5 Comments
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 500 total points
ID: 36586784
Use this

$array = explode($file, '.');
$file_extension = $array[1];

Cheers
0
 
LVL 8

Author Comment

by:Zado
ID: 36586841
Can I reverse the results somehow? I mean if you have a file like 'some.file.jpg', then your script wouldn't work, but if we can reverse the results, I mean the array would be then

jpg
file
some

instead

some
file
jpg

then $array[0] would always mean file extension, but I'm not sure how to do this.
Thanks.
0
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 36586873
Simply do

$file_extension = $array[count($array)-1];

This will return the last element of the array, the extension ;-)
0
 
LVL 8

Author Comment

by:Zado
ID: 36586937
Excellent, thanks a lot.
Btw, there's a small misspell in your code:
instead
$array = explode($file, '.');

Open in new window

there should be
$array = explode('.', $file);

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 36586980
oooops, you're right! anyway, I'm glad to help you. On to the next.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now