By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

Renaming a file but preserving the extension

P: n/a
Hi,

A question for those of you who always seem to know the one line way
of doing things. I'm using php 4.4.4 and I want to rename the first
part of the file to a particular string. For example, if my input
string were

$input = "/path/to/my/file/12345.jpg";

I would want my result to be "/path/to/my/file/header.jpg", assuming
"header" was my fixed string.

Any slick ways to do this?

Thanks, - Dave

Feb 23 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
la***********@zipmail.com wrote:
Renaming a file but preserving the extension
How do you define "extension"? In the file "nedit-5.5-src.tar.gz" which of
these parts is the "extension"?

.gz
.tar.gz
.5-src.tar.gz

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 23 '07 #2

P: n/a
On Feb 23, 1:56 pm, Toby A Inkster <usenet200...@tobyinkster.co.uk>
wrote:
laredotorn...@zipmail.com wrote:
Renaming a file but preserving the extension

How do you define "extension"? In the file "nedit-5.5-src.tar.gz" which of
these parts is the "extension"?

.gz
.tar.gz
.5-src.tar.gz

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Good question. You can assume for the purposes of this question that
extension is anything following the "." in which there will be exactly
one "." in the file name.

- Dave

Feb 23 '07 #3

P: n/a
la***********@zipmail.com wrote:
Good question. You can assume for the purposes of this question that
extension is anything following the "." in which there will be exactly
one "." in the file name.
$x = pathinfo('/path/to/file.txt');
$X = $x['dirname'] . '/' . 'foo' . '.' . $x['extension'];
print $X;

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 23 '07 #4

P: n/a
Toby A Inkster wrote:
la***********@zipmail.com wrote:
>Good question. You can assume for the purposes of this question that
extension is anything following the "." in which there will be exactly
one "." in the file name.

$x = pathinfo('/path/to/file.txt');
$X = $x['dirname'] . '/' . 'foo' . '.' . $x['extension'];
print $X;
A little more involved, but this will allow for extensions like tar.gz
or tar.bz2, etc. It gets the position in the file name, of the first
".", and keeps everything after.

$p = '/path/to/archive.tar.gz';
$file = basename($p);
$p2 = dirname($p) . '/newname' . substr($file,strpos($file,'.'));
echo $p2;

--
Curtis, http://dyersweb.com
Feb 24 '07 #5

P: n/a
Curtis wrote:
A little more involved, but this will allow for extensions like tar.gz
or tar.bz2, etc. It gets the position in the file name, of the first
".", and keeps everything after.
[...]
What about "nedit-5.5-src.tar.gz"? Your code will rename it to
"newname.5-src.tar.gz" whereas ideally it would be named "newname.tar.gz".

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 24 '07 #6

P: n/a
Toby A Inkster wrote:
Curtis wrote:
>A little more involved, but this will allow for extensions like tar.gz
or tar.bz2, etc. It gets the position in the file name, of the first
".", and keeps everything after.
[...]

What about "nedit-5.5-src.tar.gz"? Your code will rename it to
"newname.5-src.tar.gz" whereas ideally it would be named "newname.tar.gz".
Oh, I see what you meant now. I guess, if needed, one could store some
file extensions in an array to aid with more accurate renaming. Hehe,
I thought I had it. :P

--
Curtis, http://dyersweb.com
Feb 24 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.