Solved

Rename a file and keep file extension

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php construct 5 26
PHP and google maps 13 44
ebay api to see some data about my customers 2 28
Restore of mysql database from .SQL file - using Coldfusion 5 34
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
The viewer will learn how to dynamically set the form action using jQuery.

776 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