Okay, I am a beginner at this and I am trying to piece everything together. I am trying to create a file directory/download page. The way it will work is there will be a list of products and each product has 4 or 5 files associated with it that need to be listed with that product. I want the thumbnail to show up. I tried doing str_replace on the file name but I obviously am doing something wrong because it isnt replacing anything merely adding them to the top as you can see from my screenshot. The second image is what I am actually going for. Any help would be appreciated. I dont even really know what to ask at this point. So, should each product have its own folder. and then create an array with one for the different file types then have it go to the next directory and so on an so forth. I have been trying all these different things now to no avail so I must be doing something wrong. PLEASE help.

Matt

<?PHP
  function getFileList($dir, $recurse=true)
  {
    // array to hold return value
    $retval = array();

// add trailing slash if missing
if(substr($dir, -1) != "/") $dir .= "play/";

// open pointer to directory and read list of files
$d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
while(false !== ($entry = $d->read())) {
  // skip hidden files
  if($entry[0] == ".") continue;
  if(is_dir("$dir$entry")) {
    $retval[] = array(
      "name" => "$dir$entry/",
      "hires" => 0,
      "lastmod" => filemtime("$dir$entry")
    );
    if($recurse && is_readable("$dir$entry/")) {
      $retval = array_merge($retval, getFileList("$dir$entry/", true));
    }
  } elseif(is_readable("$dir$entry")) {
    $retval[] = array(
      "name" => "$dir$entry",
      "size" => filesize("$dir$entry"),
      "lastmod" => filemtime("$dir$entry")
    );
  }
}
$d->close();

return $retval;
  }
?>
            <?PHP
  $dirlist = getFileList("play/");

/* sample output

  Array
  (
    [0] => Array
        (
            [name] => images/background0.jpg
            [type] => image/jpeg
            [size] => 86920
            [lastmod] => 1077461701
        )

[1] => ...
  )

*/
?>

<?PHP
  // output file list as HTML table
  echo "<table border='1'>\n";
  echo "<tr><th>Name</th><th>Size</th><th>Last Mod.</th></tr>\n";
  foreach($dirlist as $file) {
    echo "<tr>\n";
    echo '<img src="play/play.png" style="width: 20px; height: 20px">';

echo "<td width='200'><a href=\"{$file['name']}\">",basename($file['name']),"</a></td>\n";
     //string that needs to be customized
$rawstring = "{$file['name']}";

//placeholders array
$placeholders = array("{$file['name']}");
//male replace values array
$thumbvals = array('<img src="play/play.png" style="width: 20px; height: 20px">');
//thumb string
$thumbstr = str_replace($placeholders, $thumbvals, $rawstring);


echo $thumbstr ;
    echo "<td width='100'>{$file['size']}</td>\n";
    echo "<td width='200'>",date('r', $file['lastmod']),"</td>\n";
    echo "</tr>\n";
  }
  echo "</table>\n\n";

?>

    First off, you assign dir->read() to $entry, but then uses the undefined variable $dirEntry. After that, I'd recommend ditching string interpolation for your array values, since these two statements are equivalent, but the string version requires extra work

    "{$array['key']}";	// string interpolation - extra work
    $array['key'];		// the exact same thing - no extra work needed
    

    Your usage of str_replace seems weird as well. When you get to the file 11601thumb.jpg, you will have
    (search) $placeholders = array('11601thumb.jpg');
    (replace) $thumbvals = array('<img src="play/play.png" style="width: 20px; height: 20px">');
    (subject) $rawstring = '11601thumb.jpg';

    So, to begin, you will always only have one array element, which makes using arrays pointless. You could just as well use the value directly instead. Moreover, in the string '11601thumb.jpg' you want to replace the string '11601thumb.jpg' with the string '<img src="play/play.png" style="width: 20px; height: 20px">'.
    In other words, you could just as well use the string '<img src="play/play.png" style="width: 20px; height: 20px">' straight away.

    But, if my guess is correct, what you want is '<img src="play/thumb11601.jpg" style="width: 20px; height: 20px">'. And if that is the case, you could build this string directly

    printf('<img src="play/%s" style="width: 20px; height: 20px">', $file['name']);
    

    Since you have index.php in your directory listing, I'm guessing this is the docroot of your web server. Personally I like keeping different types of resources separated, so I'd move all images to ./images, and if you have multiple versions go with something like images/full_size and images/thumbs.
    If you split up your files according to such a hierarchy, you could also keep the exact same filename, extension excluded, for all your files. I.e.
    products/descriptions/11601.txt
    products/images/full/11601.jpg
    products/images/thumbs/11601.jpg

    Assuming all of your products will always have a description, or a thumbnail, you could simply go over the entire descriptions directory, or thumbnail directory, and this way extract all the information you need. Something along the lines of

    function getNames($dir)
    {
    	if (false === ($d = dir($dir)))
    	{
    		return false;
    	}
    
    $names = array();
    while (false !== ($f = $d->read()))
    {
    	if ($f[0] != '.')
    	{
    		printf('<div>%s</div>', $f);
    		$names[] = substr($f, 0, -4);
    	}
    }
    
    return $names;
    }
    
    $baseDir = 'products/';
    $descDir = $base.'descriptions/';
    $fullDir = $base.'/images/full/';
    $thumbDir = $base.'/images/thumbs/';
    
    $products = getNames('products/descriptions');
    if ($products === false)
    {
    	echo 'Error retrieving product data';
    	exit;
    }
    
    foreach ($products as $p)
    {
    	printf('<h3>%s</h3><div>&lt;img src="%s%s" alt="" /&gt;</div><p>%s</p><br />',
    			$p,					/* product name */
    			$thumbDir,			/* thumbnail directory */
    			$p.'.jpg',			/* image thumbnail */
    			$descDir.$p.'.txt'	/* product descrition */
    	);
    }
    

    And then, in the last printf statement, you'd obviously change the &lt;img ... &gt; into <img ...> to actually display the image. Along the same lines, you'd change $descDir.$p.'txt' to file_get_contents($descDir.$p.'txt');

    It may also be a good idea to add code to check that all files actually exist so that you for example might display "No description" if there is no file in products/descriptions.

      Okay, That is working to an extent. I have it to where it is displaying the thumbnail. What I actually want is a product listing and not a file directory (I was using wrong terminology). I would prefer for all the files to be in one directory(folder) and just change via name or file time. For instance. 11601hi.jpg (hires photo) 11601lo.jpg (lowres photo) 11601.txt (for description), 11601misc.mov (movie) 11601misc.pdf (pdf). Something like that. Then I want them to populate a table that shows the thumbnail on the left and the description beside that. Then on the right. All I want are checkboxes to select (hires, lo res, mov, pdf). These files are just being selected for download. I need a download all button and a download selected button at the top. Which I already have in html. But how do I get php to submit and download all and selected items???? Under the thumbnail I need an enlarge button to then view a larger image in a shadowbox type popup. I am trying to keep everything in one folder so that this folder can be batch uploaded to. I am basically using the code that you just provided to me. I am getting a Name field then below that a Thumbnail and below that the txt (all one line...which I dont like...but is okay) The fact that I was displaying in a table is why I was using arrays to begin with. So, now I dont really understand how to move the content where I need it with the code I was given up above. This is my current PHP code.

      <?PHP
      function getNames($dir) 
      { 
          if (false === ($d = dir($dir))) 
          { 
              return false; 
          } 
      
      $names = array(); 
      while (false !== ($f = $d->read())) 
      { 
          if ($f[0] != '.') 
          { 
      
              $names[] = substr($f, 0, -4); 
          } 
      } 
      
      return $names; 
      } 
      
      $baseDir = 'play/11601'; 
      $descDir = $base.'play/11601/descriptions/'; 
      $fullDir = $base.''; 
      $thumbDir = $base.'play/11601/'; 
      
      $products = getNames('play/11601'); 
      if ($products === false) 
      { 
          echo 'Error retrieving product data'; 
          exit; 
      } 
      
      foreach ($products as $p) 
      { 
          printf('<h2>%s</h2><div><img src="%s%s" width="100px" max-height="100px" alt="" ></div><p>%s</p><br />', 
                  $p,                    /* product name */ 
                  $thumbDir. 'thumb',           /* thumbnail directory */ 
                  $p.'.jpg',            /* image thumbnail */ 
                  file_get_contents($descDir.$p.'.txt')    /* product description */ 
          ); 
      } 
      ?>

      I have attached a screenshot of what I currently get. The buttons above are in html code. My other question is how do I get it to look for content and then put no content available or something like that if a certain file type isnt there. Every item will have atleast a picture and a description the rest is considered misc. content. So, can I have it just put a check box beside all items that have "misc" in the file name. Then they can download it. There is no need to view these in browser window just the pop up image that I talked about earlier. If you see my first post you can see exactly what I am trying to get to.

      THANK YOU!!!!

      Matt

        Write a Reply...