Jump to content

Can't poll images out of array with loop


artaudo

Recommended Posts

Hello Everyone! 

 

I have a would be simple little script that is supposed to read images in a file, and read them into a browser dynamically.  Here is the code:

 

[hr width=100%]

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>Project Me</title>

</head>

<style type="text/css">

ul {list-style-type: none;

 

}

 

li {float: left;

padding:10px;

margin: 10px;

font: bold 10px Tahoma

 

}

 

img {display: block;

border:1px solid #333300;

margin-bottom:5px;

}

</style>

 

<body>

<h5>Picute Perfect Project</h5>

 

<ul>

<?php

// define location of photo images

// this must be a locaiton accessible by the script owner

$photosDir = './photos';

 

// define which file extensions are images

$photosExt = array('gif','jpg','jpeg','tif','tiff','bmp','png',);

 

// initalize array to hold filenames of images found

$photosList = array();

 

// read directory contents

// build photo list

if (file_exists($photosDir)){

$dp = opendir($photosDir) or die ('Error: Cannot open directory');

while ($file = readdir($dp)){

if ($file != '.' && $file != '..') {

$fileData = pathinfo($file);

if (in_array($fileData['extension'], $photosExt)){

$photosList[] = "$photosDir/$file";

}

}

}

closedir($dp);

} else {

die ('Error: Directory does not exist.');

}

 

// iterate over photo list

// display each image and filename

if (count($photosList)) > 0) {

for ($x=0; $x<count($photosList); $x++){

?>

 

<li>

<img height="150" width="200" src="<?php echo $photosList[$x]; ?>" />

<?php echo basename($photoList[$x]); ?><br />

<?php echo round(filesize($photosList[$x])/1024) . ' KB'; ?>

</li>

 

<?php

}

} else {

die('Error: No images found in directory');

}

?>

</ul>

</body>

</html>

 

I believe the problem is at the  very end ... when it is trying to read

 

if (count($photosList)) > 0) {

for ($x=0; $x<count($photosList); $x++){

 

This is line 47.

I don't know if $photoList is even creating an array or not.  I have not seen enough behaviors of closedir() ither to tell if this is the problem.  I know this is a simple read and write issue.  There is no real explanation or Error message ...just, error at line 47 and the path.  I ran a 'realpath()' also to make sure the directory was correct.

 

Parse error: parse error in C:\xampp\htdocs\ferlic.proj\new.site\photos\gallery.php on line 47

 

I am giving alot of detail on this because it is a very simple issue that will be used to solve several scripts. 

Link to comment
Share on other sites

Thanks for the Prompt response.  I will get into it. Also, I got the crap kicked out of me by Ubuntu. I am terminal/command line whipped. Had to get rid of it.  I typically don't have the time.  Especially to go form a wizard in XP to Terminal/command line.  I'll pick up what I can ...especially from this lesson if it will help 9 out of 10. For now, someone else will get to be more infantry than me at it.

 

But, nonetheless, where should I place this code...?

 

error_reporting(E_ALL);

ini_set('display_errors', 1);

Link to comment
Share on other sites

I've been counting open and closing brackets also.  They all look good to me.  Hope your not sending me out for "blinker fluid" or a box of "grid squares."

if (count($photosList)) > 0)

Count the number of opening and closing brackets

 

 

 

 

 

I'll clarify:

 

if (count($photosList)) > 0)

Link to comment
Share on other sites

Replacing that script has caused an error now at the }else{ after closedir($dp).

If sure its going to be something simple like this.  Perhaps someone might try to throw the script into a folder full of images and see what happens.  It's suppose to be that easy.  Thanks for your help.

 

Link to comment
Share on other sites

But, nonetheless, where should I place this code...?

 

error_reporting(E_ALL);

ini_set('display_errors', 1);

And this is a standard when developing code.

Put it at the top of your main scripts (not necessarily included files) and it says to display all warnings, errors, etc. Useful for identifting errors and other potential problems

Link to comment
Share on other sites

I am still having issues with this script. 

 

error_repotring(E_ALL);

ini_set('display_errors',1);php -1

 

 

I've placed the script you provided me at the top of the page, below the <?php.

 

1)  The issue now is with reading the directory.

 

$photosDir = './photos';

 

I try to use " " instead of ' '.  I've also tried using the whole address path and not just ./photos.  I have even tried taking the gallery.php out of ./photos (logical) and manipulated the path.  I feel like I am trying to think of the Korean word for rice here, or trying to pronounce mineral water in French. 

 

2)  Besides the dir path issue, it seems to keep getting hung up on the }else{  when I take out the error checking script.  I know this all has to boil down to just simple syntax. 

 

I am solving mysql query issues with greater success that this old girl... as well as diving into Joomla.  I know these sorts of things happen from time to time, and that is why I'm happy to know you all.  I am gradually building my own "knowledge set library" so to speak.  Can someone possibly throw the script into a folder with a bunch of images to see what happens?

 

3)  Otherwise, can anyone suggest a php editor ?  I remember "Interdev," back in my .asp days ... you could hit F1 on a highlighted function and it would break down the function/varibable parameters for you.

 

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.