Accessing files outside the web root

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Accessing files outside the web root

Post by social_experiment »

I want to access files outside the root of my website but don't know how to go about it. The folder is called img so i created the following script:

Code: Select all

<?php
 // this is an example, don't critique me 
 // on this.
$uploaddir = '/img/';
$name = '1.png';
	
echo '<img src="'. $uploaddir/$name .'" width="50" 
height="50" border="50" />';

echo '<br />';	
readfile($uploaddir.$name);
?>
I tried both readfile() and the img HTML tag but neither will display the image. Readfile does give an error indicating that is no such directory.

1. What would be the correct syntax to access a file outside the root folder (public_html)?

My hosting provider says it is possible using a php script and I'm thinking he is refering to this.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Accessing files outside the web root

Post by AbraCadaver »

Depends on where the directory is located. Try using the full path to the directory, like /home/somebody/img.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Accessing files outside the web root

Post by social_experiment »

I changed $uploaddir to the following '../../img/' . Now readfile() spits out a ton of weird data, im assuming it has found the file (and it's not giving an error message anymore). Yet when i try to display the file with an img HTML tag, it still doesn't show the image. Is the IMG tag the correct way to display the image in this case?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Accessing files outside the web root

Post by AbraCadaver »

social_experiment wrote:I changed $uploaddir to the following '../../img/' . Now readfile() spits out a ton of weird data, im assuming it has found the file (and it's not giving an error message anymore). Yet when i try to display the file with an img HTML tag, it still doesn't show the image. Is the IMG tag the correct way to display the image in this case?
If the image is outside of the web root then the img tag will not display it because the image is not accessible. readfile() may work but you have to send the proper headers (content type, length, etc.).
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Accessing files outside the web root

Post by social_experiment »

AbraCadaver wrote:readfile() may work but you have to send the proper headers (content type, length, etc.).
It does indeed work, but it makes it difficult to include this image ,lets say use the image as a profile photo. The header('Content-Type: image/jpeg') seems to block any other text that you might want to display.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Accessing files outside the web root

Post by AbraCadaver »

You need to display the image in a PHP file and then have this file as the src of an img tag. Here is something similar just with an image from a database: viewtopic.php?f=1&t=110171&p=582727#p582727
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Accessing files outside the web root

Post by social_experiment »

Cool :). Thanks for the help on this.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply