Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP root folder

Tags:

file

php

root

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?

like image 350
James Avatar asked Sep 04 '10 15:09

James


People also ask

Where is my PHP root directory?

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.

What is a web root directory on PHP?

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.

How do I set the root directory in PHP?

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.

What is a root folder path?

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.


2 Answers

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');
like image 68
Gumbo Avatar answered Sep 21 '22 08:09

Gumbo


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');
like image 23
adamse Avatar answered Sep 20 '22 08:09

adamse