Web Design Forum: Help with PHP - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Help with PHP Rate Topic: -----

#1 User is offline   Boyles Web Design 

  • Hi...
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,613
  • Joined: 29-August 07
  • Reputation: 2
  • Gender:Male
  • Location:Indiana, USA
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 14 January 2012 - 03:38 AM

I am learning PHP at the moment and have this script. It takes the images from a dir and adds them to a web site. Why isnt this working? I cant figure it out....


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$dirname = "/Users/jakeboyles/Desktop/CashFlowStuff";
$dp = opendir($dirname);
chdir($dirname);

while ($currentFile !== false) {
	$currentFile = readdir($dp);
	$theFiles[] = $currentFile;
}

$imageFiles = preg_grep("/jpg$|gif$|png$/", $theFiles);
$output="";
foreach ($imagesFiles as $currentFile) {
	$output .= <<<HERE
	<a href = $currentFile>
	<img src= "$currentFile"
	height = 50
	width=50>
	</a>
	
	HERE;
}


$fp = fopen("imageIndex.html","w");
fputs ($fp,$output);
fclose($fp);
//print "hello";
?>
</body>
</html>


0

#2 User is offline   Samus 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 367
  • Joined: 05-August 11
  • Reputation: 27
  • Gender:Male
  • Location:Hackney, London, UK
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 14 January 2012 - 12:01 PM

I have a better way of doing this..

<?php

$imagedir = "path/to/dir/"; //replace with the path to the images directory

$images = glob($imagedir."*.*"); // selects all images
foreach($images as $image) 
{
echo "<a href='".$image."'>".$image."</a>"
}


$images = glob($imagedir."*.*"); // selects all images

That line selects all files, so you may want to ensure that all files withhin that directory are images. If not, you can specify the type of images you want to output.

Instead of the above:

$images = glob($imagedir."*.png"); // selects all png images

That will select all .png images

This post has been edited by Samus: 14 January 2012 - 12:04 PM

0

#3 User is online   rallport 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 3,820
  • Joined: 03-January 10
  • Reputation: 266
  • Gender:Male
  • Location:England, UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 14 January 2012 - 03:18 PM

View PostSamus, on 14 January 2012 - 12:01 PM, said:

I have a better way of doing this..


glob, really? How on earth is that better? If anything, the OPs code using opendir, that doesn't work, is better than glob.

@OP - have a read about SPL and the DirectoryIterator class. See http://php.net/manua...oryiterator.php
0

#4 User is offline   Samus 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 367
  • Joined: 05-August 11
  • Reputation: 27
  • Gender:Male
  • Location:Hackney, London, UK
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 14 January 2012 - 03:30 PM

View Postrallport, on 14 January 2012 - 03:18 PM, said:

glob, really? How on earth is that better? If anything, the OPs code using opendir, that doesn't work, is better than glob.

@OP - have a read about SPL and the DirectoryIterator class. See http://php.net/manua...oryiterator.php

why not?
0

#5 User is offline   Olavi 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 10
  • Joined: 11-January 12
  • Reputation: 2
  • Gender:Male
  • Location:Norway
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 14 January 2012 - 04:36 PM

DirectoryIterator is much faster than glob.

A bit old, but read this

This post has been edited by Olavi: 14 January 2012 - 04:38 PM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users