pixelschupser
pixelschupser

Reputation: 13

PHP - Link from Folder

I'm currently working on the front-end of a clients website. He's a photographer and doesnt want to bother with CMS systems (sheesh).

I'll just explain it the way he explained it to me:

He wants to create folders on his FTP filled with images. These folders represent the pages or categories. The images inside these folders should then be displayed on the page created from the folder. If the folder is named "fashion", he wants the domain to read: www.client.com/fashion.

I was looking all over the internet for a solution, and i really hope you can help me out of my misery.

Basicly, i need a tuned down directory listing system that scans for folders and creates links inside a php file with the folders name, displaying the images inside. Also, if you drop a txt file in the folder, it should display the text inside it.

This system is similar to the Stacey CMS, but sadly he doesnt want any CMS.

If you need any more information, dont hesitate to drop a message.

Thank you.

Upvotes: 0

Views: 1808

Answers (3)

Jeremy
Jeremy

Reputation: 1972

This is very simple actually. Use the DirectoryIterator. I'll post some code to give you a push in the right direction but I am not giving you the solution, you have to do that yourself ;-)

I use this snippet to fetch a list of the folders in my banners directory:

    $tmpDir = dirname(__FILE__).DIRECTORY_SEPARATOR.'banners'.DIRECTORY_SEPARATOR.'emrc'.DIRECTORY_SEPARATOR;
    $dirProc=new DirectoryIterator($tmpDir);
    $banerSizes = array();
    foreach($dirProc as $dirContent){
        if ($dirContent->isFile() || substr($dirContent->getFilename(), 0, 1) === '.') continue;
        if ($dirContent->isDir()) {
            $banerSizes[] = $dirContent->getFilename();
        }
    }
    natcasesort($banerSizes);

I use this to display them: (note: I do not link the image but I do supply a code sample to copy and paste the link. This can easily be modified to your needs)

    <table class="regular" width="100%" cellpadding="0" cellspacing="0">
    <?php 
        /**
         * Display bannes
         */
        foreach($banerSizes as $bannerSize){
            $tmpDir = dirname(__FILE__).DIRECTORY_SEPARATOR.'banners'.DIRECTORY_SEPARATOR.'emrc'.DIRECTORY_SEPARATOR.$bannerSize.DIRECTORY_SEPARATOR;
            $dirProc=new DirectoryIterator($tmpDir);
            ?>
        <tr>
            <td>
                <a name="<?php echo $bannerSize; ?>"><?php echo $bannerSize; ?></a>
                <hr />
            </td>
        </tr>

            <?php 
            foreach($dirProc as $dirContent){
                if (!$dirContent->isFile() || substr($dirContent->getFilename(), 0, 1) === '.') continue;
        ?>
        <tr>
            <td align="center">
                <img src="/banners/emrc/<?php echo $bannerSize; ?>/<?php echo $dirContent->getFilename(); ?>" />
                <br />
                <textarea cols="42" rows="4" readonly="readyonly"><a href="<?php echo $affiliateLink; ?>"><img src="<?php echo $siteLink; ?>/banners/emrc/<?php echo $bannerSize; ?>/<?php echo $dirContent->getFilename(); ?>" /></a></textarea>
                <br />
                <hr style="width:384px" />                      
            </td>
        </tr>
        <?php 
            }
        }
        ?>              
    </table>

Don't use tables. I only did because it was required for the template I worked in. I hope this helps you, please let me know.

Upvotes: 1

DaveRandom
DaveRandom

Reputation: 88697

Firstly, you need to rewrite the URL so it is passed to your PHP script so you can do something with it. Assuming Apache, you would place something like this is a .htaccess file:

Ref: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ imagelist.php?dir=$1 [L]

Then, in imagelist.php, do something like the following:

<?php

  $baseDir = 'images/';

  $workingDir = $baseDir.$_GET['dir'];

  if (!is_dir($workingDir) || (!$dp = opendir($workingDir))) {
    exit('Unknown category: '.$_GET['dir']);
  }

  echo '<html><head><title>'.$_GET['dir'].'</title></head></body>';

  while (($file = readdir($dp)) !== FALSE) {
    if (substr(strtolower($file),-4) == '.txt') {
      // Handle text files
      echo '<pre>'.file_get_contents($workingDir.'/'.$file).'<pre>';
    } else {
      // Treat everything else as an image
      echo '<img src="'.$workingDir.'/'.$file.'" alt="'.$file.'" title="'.$file.'">';
    }
  }

  echo '</body></html>';

Obviously this doesn't do any nice formatting of the layout, but hopefully it will give you a push in the right direction.

Upvotes: 1

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57660

What you need is this,

  1. Read category from url (use mod_rewrite for this)

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # Rewrite all other URLs to index.php/URL
    RewriteRule ^(.*)$ index.php/$1 [PT,L]
    
  2. Scan the directory you read on step 1 by php.

  3. Show the contents in a nice html template
  4. For a single photo (if you want to show it in full screen) use another template.

It'd require 2 php scripts. And 2 html templates. Additionally you may need a solution for thumbnail creation.

Upvotes: 1

Related Questions