Tom
Tom

Reputation: 193

Display images in a folder using PHP

Ok, been looking all over the internet and even Stackoverflow but I can't seem to get the following PHP code to work.

<div class="grid">
<ul class="images">
<?php
//path to directory to open
$directory = "/Media/pics/";
$dir_handle = @opendir($directory) or die("Unable to open folder");

while(false !== ($file = readdir($dir_handle)))
{
  if($file != '.' && $file != '..' && $file != 'Thumbs.db') 
  {
    echo "<li class='picture' title='Remote2'> <img src='/Media/pics/".$file."'alt='test'/></li>";                      
  }
}
closedir($dir_handle);
?>

Each time I try and view the page, I get:

403 Forbidden - You don't have permission to access intranet/Media/pics/".$file." on this server.

Yet I am able to navigate to "intanet/Media/pics" and see and click on all the images in the folder. I don't think it is a permission issue, but maybe something I'm missing in the PHP code. Makes me wish I had a really good PHP editor with syntax highlighting and auto-complete (currently using Aptana Studio 3).

Thanks for the help!

SOLUTION: Had to do the following: 1) Change "pictures.html" to "pictures.php" (newb mistake)

2) Fix the realitive path from "/media/pics" to just "media/pics"

Upvotes: 0

Views: 2401

Answers (4)

iWantSimpleLife
iWantSimpleLife

Reputation: 1954

Looking at the comments to the answer of @Marc B.

The PHP scripts in the file did not seems to be executed. Did you name the file correctly?

In order for the PHP screpts to be ran, you need to give the file a ".php" extension. I suspect you might have names it as ".html"?

Or is it the configuration on the Apache is not set up correctly to run ".php" files past PHP?

Upvotes: 0

Syntax Error
Syntax Error

Reputation: 4527

Try changing this line:

echo "<li class='picture' title='Remote2'> <img src='/Media/pics/".$file."'alt='test'/></li>";

to

echo "<li class='picture' title='Remote2'> <img src='/Media/pics/$file' alt='test'/></li>";

Your error is kind of strange if it's treating the variable name as a string. Double quotes will use the value of $file so this should at least get us a different error if it doesn't work.

Edit: Also I suspect the path might be missing something as has already been mentioned. Compare your path to the path mentioned in the error and see if you have the whole thing in there correctly. For example should it be /home/site/Media/pics (as an example)

Upvotes: 0

Marc B
Marc B

Reputation: 360862

You're using an absolute path, something like /Media/pics/somepic.jpg. Do you have a /Media directory in the root of your file system? As well, is that /Media folder part of your website's document root? Remember that PHP's file operations work at the file system level, not at your webserver's URL level. PHP can access files that are completely outside of your website's reach, so outputting a file system direct as a URL for a src can quite easily point at a file that is impossible for a remote user to access.

Upvotes: 2

Matt
Matt

Reputation: 23789

Unix systems generally restrict file reading permissions by default. If you know how to CHMOD, make sure that PHP has permission to read that directory. (640 should be enough if Apache is the owner of them. If not, you can try higher, but for reading you shouldn't need higher than 700 or 750.)

Upvotes: 0

Related Questions