Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolute File Path

This problem is still not solved

A video PHP tutorial I am following is building a file called initialize.php in which it is using the PHP pre-defined constant Directory_Separator and then defining a site_root. The site_root is the absolute file path (not the webserver path) for PHP to locate the files it needs. He gave us the following code

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

    defined('SITE_ROOT') ? null :
    define ('SITE_ROOT', DS.'Users'.DS.'kevin'.DS.'Sites'.DS.'photo_gallery');

I am assuming the file path on his computer is root/users/kevin/sites/photogallery

I am not building the site on my computer, but rather directly online. I don't know what file path to insert instead.

As he emphasized that it's not the webserver path, but rather the file system path, what do I put instead. Just the domain name like this.

define('SITE_ROOT', DS. 'www.example.com');

He doesn't want the webserver path but the files are located online? so I don't get it.

UPDATE

The video tutorial used the following code

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

defined('SITE_ROOT') ? null :
define ('SITE_ROOT', DS.'Users'.DS.'kevin'.DS.'Sites'.DS.'photo_gallery');
defined('LIB_PATH') ? null : define('LIB_PATH',SITE_ROOT.DS.'includes');

I used this code:

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)));
defined('LIB_PATH') ? null : define('LIB_PATH',SITE_ROOT.DS.'includes');

I got this error message

require_once(/hsphere/local/home/c263430/quoralist.com/includes/includes/config.php): failed to open stream: No such file or directory in /hsphere/local/home/c263430/quoralist.com/includes/initialize.php on line 11 Fatal error: require_once(): Failed opening required '/hsphere/local/home/c263430/quoralist.com/includes/includes/config.php' (include_path='.:/hsphere/shared/apache/libexec/php5ext/php/') in /hsphere/local/home/c263430/quoralist.com/includes/initialize.php on line 11

UPDATE using the 3 edits below by experimentX, as well as the following

define('SITE_ROOT', DS.'hsphere'.DS.'local'.DS.'home'.DS.'c263430'.DS.'quoralist.com');

I always got the error message

Warning: require_once(LIB_PATH/config.php): failed to open stream: No such file or directory in /hsphere/local/home/c263430/quoralist.com/includes/initialize.php on line 11 Fatal error: require_once(): Failed opening required 'LIB_PATH/config.php' (include_path='.:/hsphere/shared/apache/libexec/php5ext/php/') in /hsphere/local/home/c263430/quoralist.com/includes/initialize.php on line 11

Also,

like image 714
Leahcim Avatar asked Mar 06 '11 07:03

Leahcim


2 Answers

How about

defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)));

echo SITE_ROOT;

Instead of define('SITE_ROOT', 'www.domain.com') you should define('SITE_ROOT', realpath(dirname(__FILE__)).

www.domain.com is the base url (of your site) while realpath(dirname(__FILE__)) is the absolute file path of FILE where it is defined. Using this path, you can set up a ROOT FOLDER for you site.

For example: you cannnot do unlink('www.example.com\img1.jpg') to delete img1

You should do unlink(SITE_ROOT.DS.'img1.jpg');

Similarly for move_uploaded_file or any other directory or file function. i.e. your files are being moved and deleted by server computer(as php script is executed in server), so absolute file path is required.

Using this absolute path, you can navigate to the folder and access files on server.

[UPDATE]

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

defined('SITE_ROOT') ? null : 
    define ('SITE_ROOT', DS.'Users'.DS.'kevin'.DS.'Sites'.DS.'photo_gallery');

Though I don't own mac, the web-root is Users/kevin/Sites/ on Mac and the Users/kevin/Sites/photogallery is the site root.

The tutor knows this so he is doing this. But we might not know this when our project is uploaded in webserver, so

defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)));

Would be the most appropriate.

And as to DS, it is just a CONSTANT (DIRECTORY_SEPARATOR), which is defined because DIRECTORY_SEPARATOR would be too long to write. i.e.

  SITE_ROOT'.DIRECTORY_SEPARATOR.'Users'.DIRECTORY_SEPARATOR.'kevin'.DIRECTORY_SEPARATOR.'Sites'.DIRECTORY_SEPARATOR.'photo_gallery'

So, define it before you define SITE ROOT

defined('DS')? null: define('DS', DIRECTORY_SEPARATOR);
defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)));

As the tutorial is done on MAC DIRECTORY_SEPARATOR is /. And the sample is done on localhost, the tutor knows the absolute file path of his webserver so he is doing

[ERROR: UPDATE]

You got this error because defining this on includes/initialize.php assumes that your your SITE_ROOT is yourwebroot/yourproject/includes where it must have been yourwebroot/yourproject/includes.

There are two ways, one way is

to put initialize.php in yourwebroot/yourproject

And the other way is

defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)).DS."..".DS);

OR

defined('SITE_ROOT')? null: define('SITE_ROOT', DS.'..'.DS.realpath(dirname(__FILE__)));

(I am not sure but one of them should work)

Also try this

defined('SITE_ROOT')? null: define('SITE_ROOT', '/../'.realpath(dirname(__FILE__)));
like image 91
Santosh Linkha Avatar answered Sep 24 '22 00:09

Santosh Linkha


sorry, i have the answer for IMAGES not displaying

in photograph.php, just remove DS

public function image_path() {
  return $this->upload_dir.$this->filename;
}

working line is:

defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)).DS."..".DS);

Thanks!

like image 28
atomxkai Avatar answered Sep 25 '22 00:09

atomxkai