Solved

Content of folder and output

Posted on 2003-12-05
4
244 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delete  php pages of a part of our site 8 45
PHP 5.6 and 7.x 4 37
Php logic to add to date card 9 38
How to hide contact form only if no validation errors 4 23
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…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

740 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