8
rename('/images/old_name.jpg', '/images/new_name.jpg');

This code gives file not found.

Script, where files are called is placed inside /source/ folder.

Files can be opened from http://site.com/images/old_name.jpg

How to get these files from root?

2
  • This FAQ question really needs a good answer Commented Sep 4, 2010 at 15:23
  • 1
    Even when you do it right, the file can take a few minutes to appear on the dest folder if it is a bit large. Commented Jun 28, 2015 at 18:04

2 Answers 2

35

rename is a filesystem function and requires filesystem paths. But it seems that you’re using URI paths.

You can use $_SERVER['DOCUMENT_ROOT'] to prepend the path to the document root:

rename($_SERVER['DOCUMENT_ROOT'].'/images/old_name.jpg', $_SERVER['DOCUMENT_ROOT'].'/images/new_name.jpg');

Or for more flexibility, use dirname on the path to the current file __FILE__:

rename(dirname(__FILE__).'/images/old_name.jpg', dirname(__FILE__).'/images/new_name.jpg');

Or use relative paths. As you’re in the /script folder, .. walks one directory level up:

rename('../images/old_name.jpg', '../images/new_name.jpg');
Sign up to request clarification or add additional context in comments.

1 Comment

instead of rename('../images/old_name.jpg', [...] I would use rename( dirname(dirname(__FILE__)).'/images/old_name.jpg', [...] This way it would be relative to the current file (the one with the call to rename) , not the running script, wich can vary if the call to rename is in an included file.
6

In PHP the root (/) is the root of the filesystem not the "webroot". If the php-file is in the /source/ directory and images are in /source/images/ then this will work:

rename('images/old_name.jpg', 'images/new_name.jpg');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.