PHP root folder

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

This code gives the file not found .

The script where the file is called is placed inside the /source/ folder.

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

How to get these files from root?

+7
file php root
source share
2 answers

rename is a file system function and requires file system paths. But it seems that you are using URI paths.

You can use $_SERVER['DOCUMENT_ROOT'] to add 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 the dirname 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 are in / script, .. goes one level up:

 rename('../images/old_name.jpg', '../images/new_name.jpg'); 
+29
source share

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

 rename('images/old_name.jpg', 'images/new_name.jpg'); 
+6
source share

All Articles