开发者

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开发者_JAVA技巧 to get these files from root?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜