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?
In order to get the root directory path, you can use _DIR_ or dirname(). echo dirname(__FILE__); Both the above syntaxes will return the same result.
The web root directory of an app is the directory that files are served from. Your ServerPilot apps use a directory called public. This directory is where you put your app's index. php file and directories containing other files, such as images.
PHP Root Path: Use Dirname() With __DIR__ Then do nothing else except for passing the magical constant “__DIR__” to the dirname() function. The given constant will return the path of your current directory and the dirname() function will return the PHP root directory of the same.
In a computer file system, and primarily used in the Unix and Unix-like operating systems, the root directory is the first or top-most directory in a hierarchy. It can be likened to the trunk of a tree, as the starting point where all branches originate from.
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');
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With