Solved

Content of folder and output

Posted on 2003-12-05
4
242 Views
Last Modified: 2006-11-17
Hi All
I have a folder that contains for example

picture1.jpg   picture1.pdf
picture2.jpg   picture2.pdf
picture3.jpg   picture3.pdf
etc                etc

is it possible to scan the folder and produce a two column table  - the picture1.jpg displayed in the first cell and a link to the picture1.pdf in the second cell and continuing to insert a row for each additional picture found in the folder. Any help and code to get me started would be greatly appriciated.

Many Thanks in advanced

Michael

0
Comment
Question by:daleoran
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
aolXFT earned 100 total points
ID: 9883611
<?php

$foldername = 'pics';

$dir_h = opendir($foldername);
$files = array();
while( ($filename = readdir($dir_h)) !== false ){
  $files[] = substr($filename, 0, -4); //removing extension.
}

$files = array_flip(array_flip($files)); // removing duplicates

echo "<table>"
foreach($files as $file){
     echo "<tr><td>$file.jpg</td><td>$file.pdf</td></tr>\n";
}
echo "</table>";

?>
0
 
LVL 6

Expert Comment

by:aolXFT
ID: 9883614
Not the most efficient way but, It'd do the job.
0
 
LVL 10

Author Comment

by:daleoran
ID: 9919346
I've put the code in as follows and I am getting the following error message

Parse error: parse error, expecting `','' or `';'' in pics/photo.php on line 22
which is the line
foreach ($files as $file){
I'm just beginning to learn php so I'm a bit stumped with this one

<HTML>
<HEAD>
<TITLE></TITLE>
<META name="description" content="">
<META name="keywords" content="">
<META name="generator" content="CuteHTML">
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<?php

$foldername = 'pics';

$dir_h = opendir($foldername);
$files = array();
while( ($filename = readdir($dir_h)) !== false ){
  $files[] = substr($filename, 0, -4); //removing extension.
}

$files = array_flip(array_flip($files)); // removing duplicates

echo "<table>"
foreach ($files as $file){
     echo "<tr><td>$file.jpg</td><td>$file.pdf</td></tr>\n";
}
echo "</table>";

?>


</BODY>
</HTML>
0
 
LVL 10

Author Comment

by:daleoran
ID: 9933863
sorted - there was a ; missing from the first echo.
Just one more thing before I close this question. I've added

while( ($filename = readdir($dir_h)) !== false ) if ($filename !='.' && $filename != '..' && $filename != 'photo.php' && $filename != 'try.php')

to the While to look for the  .  &  ..  and two .php files that are in the folder. How can I use a wildcard to look for any .php file just like u would in dos or windows *.php

Again Many thanks

Michael

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to use embargo Date/time in php 29 39
Do you need to unset the session variable after CSRF token check passes? 2 42
preventing spam with mailto 22 33
PHP AJAX JSON 2 61
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question