Solved

Display multiple images from directory with while loop

Posted on 2008-11-02
12
1,639 Views
Last Modified: 2012-06-21
Can't get this simple loop to display all images in directory - only first one.
<?php
include_once( $_SERVER['DOCUMENT_ROOT'].'/includes/php/databaseConfig.php' ); 
$query = "SELECT id, img_path FROM test1";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_array($result)) 
{
	echo '<p><img src="upimg/'.$row['img_path'].'></p>';
 
    }
}
?>

Open in new window

0
Comment
Question by:shareea
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 22864185
The images in the directory is not what you're displaying, you're displaying based on what's in your table test1.

How many rows in that table?
0
 

Author Comment

by:shareea
ID: 22864241
Have been trying several files out - eyeballs getting tired. Yes, seems there's only 1 row in that table but have several images in the directory. Here's my input form:
<?php
include_once( $_SERVER['DOCUMENT_ROOT'].'/includes/php/databaseConfig.php' );      
 
$max_no_img=3; // Maximum number of images value to be set here
 
echo "<form method=post action=addimgck.php enctype='multipart/form-data'>";
echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";
for($i=1; $i<=$max_no_img; $i++){
echo "<tr><td>Images $i</td><td>
<input type=file name='images[]'></td></tr>";
}
 
echo "<tr><td colspan=2 align=center><input type=submit value='Add Image'></td></tr>";
echo "</form> </table>";
 
while(list($key,$value) = @each($_FILES[images][name]))
{
if(!empty($value)){   // this will check if any blank field is entered
$filename = $value;    // filename stores the value
$add = "upimg/$filename";   // upload directory path is set
//echo $_FILES[images][type][$key];     // uncomment this line if you want to display the file type
// echo "<br>";                             // Display a line break
copy($_FILES[images][tmp_name][$key], $add);     //  upload the file to the server
chmod("$add",0777);                 // set permission to the file.
$query = "INSERT INTO test1 (img_path) VALUES ('$filename')";
mysql_query($query);
	}
}
?>

Open in new window

0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 22864253
The loop you originally posted is displaying based on what rows are in the table, so if there is only one row, then you will only see one image, no matter what is in the directory.

As for the rest -- how you get the file path/names into the table -- I'm not knowledgable about that part.  Hopefully someone else who knows file handling in php will pop in and help.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:shareea
ID: 22864264
Yeah - seem to be missing the part where filedata is converted to jpeg. Thanks for your help all the same.
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 22864283
The best technique is to put some echo statements into your code at strategic places and see exactly where the problem is occurring.  This will help you sort it out.
0
 

Author Comment

by:shareea
ID: 22864425
This snippet worked to display all images located in a directory named upimg:
<?php
 
$dirname = "upimg/";
$images = scandir($dirname);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<img src='upimg/$curimg' /><br>\n";
};
}     
?>

Open in new window

0
 
LVL 27

Accepted Solution

by:
Cornelia Yoder earned 500 total points
ID: 22864444
OK, so replace the echo with the database INSERT, and then see what you get into the table.  Be sure to follow the query with

echo mysql_error();

so you know if an INSERT failed.
0
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 22864448
Then just keep doing this, step by step, until you find where the process isn't doing what you expect.  I'll check in again tomorrow morning :)
0
 

Author Comment

by:shareea
ID: 22869198
Files are being uploaded to the directory and displaying but only first image is being entered into database table. Need to loop the query. Any suggestions?
<?php
include_once( $_SERVER['DOCUMENT_ROOT'].'/includes/php/databaseConfig.php' );      
 
$max_no_img=3; // Maximum number of images value to be set here
?>
<form method=post action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype='multipart/form-data'>
<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>
<?php for($i=1; $i<=$max_no_img; $i++){
echo "<tr><td>Images $i</td><td>
<input type=file name='images[]' ></td></tr>";
}
 
echo "<tr><td colspan=2 align=center><input type=submit value='Add Image'></td></tr>";
echo "</form> </table>";
 
while(list($key,$value) = @each($_FILES[images][name]))
{
if(!empty($value)){   // this will check if any blank field is entered
$filename = $value;    // filename stores the value
$add = "upimg/$filename";   // upload directory path is set
//echo $_FILES[images][type][$key];     // uncomment this line if you want to display the file type
// echo "<br>";                             // Display a line break
copy($_FILES[images][tmp_name][$key], $add);     //  upload the file to the server
chmod("$add",0777);                 // set permission to the file.
 
$query = "INSERT INTO test1 (img_path) VALUES ('$filename')";
mysql_query($query);
}
}
//view files in directory
$dirname = "upimg/";
$images = scandir($dirname);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<img src='upimg/$curimg' width='200px'/><br>\n";
};
}     
?>

Open in new window

0
 

Author Comment

by:shareea
ID: 22869242
And after adding echo mysql_error(); get following error:

Duplicate entry '0' for key 1
0
 

Author Comment

by:shareea
ID: 22869359
OK - worked it out.

$query = "INSERT INTO test1 (id, img_path)  VALUES ('$key','$filename')";
0
 

Author Closing Comment

by:shareea
ID: 31512569
Thanks! Mysql error() helped just like you said.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

732 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