tags:

views:

52

answers:

2
+1  Q: 

PHP root folder

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?

+5  A: 

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');
Gumbo
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.
Sebastián Grignoli
@Sebastián Grignoli: Yes, that’s another good option.
Gumbo
+1  A: 

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');
adamse