Solved

Rename a file and keep file extension

Posted on 2011-09-23
5
294 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 30

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 30

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 30

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

746 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