简体   繁体   中英

How can I use dirname(__FILE__) instead of DOCUMENT_ROOT (php web server)

I have written some code for an image gallery, the code works perfectly if the application is installed in the document root folder. However various hosting companies such as C9 and many others create their do not create their sites in the DOCUMENT_ROOT. The apache server I am trying to get the app to work on has a DOCUMENT_ROOT of /user/local/websites/

I have used the command below that runs in index.php and gives me the correct path.

// full file path to app root directory

DEFINE("DOC_ROOT", dirname(__FILE__) . '/');
echo DOC_ROOT; // /home/name/public_www/subject/index.php

How can I change the statements below to use to use the output from the DEFINE command above?

$original_file = $_SERVER['DOCUMENT_ROOT'] . SITE_FOLDER_DIR . DIR_UPLOAD_IMAGE . $main_image;

$original_file_to = $_SERVER['DOCUMENT_ROOT'] . DIR_CREATE_THUMB . $main_image;

If you check your code, you'll find similar define(.. lines for SITE_FOLDER_DIR and DIR_UPLOAD_IMAGE . They work exactly the same way.

To change your statements, just replace $_SERVER['DOCUMENT_ROOT'] with DOC_ROOT eg

$original_file = DOC_ROOT . SITE_FOLDER_DIR . DIR_UPLOAD_IMAGE . $main_image;

Note: as of PHP 5.3, there's no need to use dirname(__FILE__) , you can simply use __DIR__ eg

define("DOC_ROOT", __DIR__ . '/');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM