<?php
$idir = "C:/wamp/www/test2/images/"; // Path To Images Directory
$tdir = "C:/wamp/www/test2/thumbs/"; // Path To Thumbnails Directory
$twidth = "200"; // Maximum Width For Thumbnail Images
$theight = "150"; // Maximum Height For Thumbnail Images
if (!isset($_GET['subpage']))
{ // Image Upload Form Below ?>
<form method="post" action="?subpage=upload" enctype="multipart/form-data">
<input type="file" name="imagefile" class="form" id="imagefile">
<br /> <br />
Comments about image
<br />
<input type="text" name="comments"/>
<br /><br />
<input name="Upload" type="submit" value="Upload" class="form">
</form>
<?php
} else if
(isset($_GET['subpage']) && $_GET['subpage'] == 'upload') { // Uploading/Resizing Script
$fileName = $_FILES['imagefile']['name'];
$tmpName = $_FILES['imagefile']['tmp_name'];
$fileSize = $_FILES['imagefile']['size'];
$fileType = $_FILES['imagefile']['type'];
$filecomments = $_POST['comments'];
$filePath = $uploadDir . $fileName;
$filePath = $tdir . $fileName;
include 'library/config.php';
include 'library/opendb.php';
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO upload2 (name, size, type, path, comments) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$filecomments')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
include 'library/closedb.php';
$url = $_FILES['imagefile']['name']; // Set $url To Equal The Filename For Later Use
if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") {
$file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
$copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']); // Move Image From Temporary Location To Permanent Location
if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location
print 'Image uploaded successfully.<br />'; // Was Able To Successfully Upload Image
$simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height
if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $twidth / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $twidth / $currwidth; // Length Ratio For Height
$newwidth = $twidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail
imagetruecolortopalette($simg, false, 256); // Create New Color Pallete
$palsize = ImageColorsTotal($simg);
for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image
$colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used
ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use
}
imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dimg, "$tdir" . $url, 100); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
print 'Image thumbnail created successfully.'; // Resize successful
} else {
print '<font color="#FF0000">ERROR: Unable to upload image.</font>'; // Error Message If Upload Failed
}
} else {
print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is '; // Error Message If Filetype Is Wrong
print $file_ext; // Show The Invalid File's Extention
print '.</font>';
}
function createGallery( $pathToImages, $pathToThumbs )
{
// echo "Creating gallery.html <br />";
$output = "<html>";
$output .= "<head><title>Upload</title></head>";
$output .= "<body>";
$output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
$output .= "<tr>";
// open the directory
$dir = opendir($pathToThumbs );
$counter = 0;
// loop through the directory
while (false !== ($fname = readdir($dir)))
{
// strip the . and .. entries out
if ($fname != '.' && $fname != '..')
{
$output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname} \">";
$output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
$output .= "</a><br />{$fname}</td>";
$counter += 1;
if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
}
}
// close the directory
closedir($dir );
$output .= "</tr>";
$output .= "</table>";
$output .= "</body>";
$output .= "</html>";
// open the file
$fhandle = fopen( "gallery.html", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );
// close the file
fclose( $fhandle );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
// createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that
// the path will be a relative path working
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");
}
?>
23 Replies - 1841 Views - Last Post: 11 June 2009 - 06:16 AM
#1
How to display image and comments together
Posted 05 June 2009 - 08:49 AM
Replies To: How to display image and comments together
#2
Re: How to display image and comments together
Posted 05 June 2009 - 09:34 AM
Ghost rider, on 5 Jun, 2009 - 05:49 PM, said:
<?php
$idir = "C:/wamp/www/test2/images/"; // Path To Images Directory
$tdir = "C:/wamp/www/test2/thumbs/"; // Path To Thumbnails Directory
$twidth = "200"; // Maximum Width For Thumbnail Images
$theight = "150"; // Maximum Height For Thumbnail Images
if (!isset($_GET['subpage']))
{ // Image Upload Form Below ?>
<form method="post" action="?subpage=upload" enctype="multipart/form-data">
<input type="file" name="imagefile" class="form" id="imagefile">
<br /> <br />
Comments about image
<br />
<input type="text" name="comments"/>
<br /><br />
<input name="Upload" type="submit" value="Upload" class="form">
</form>
<?php
} else if
(isset($_GET['subpage']) && $_GET['subpage'] == 'upload') { // Uploading/Resizing Script
$fileName = $_FILES['imagefile']['name'];
$tmpName = $_FILES['imagefile']['tmp_name'];
$fileSize = $_FILES['imagefile']['size'];
$fileType = $_FILES['imagefile']['type'];
$filecomments = $_POST['comments'];
$filePath = $uploadDir . $fileName;
$filePath = $tdir . $fileName;
include 'library/config.php';
include 'library/opendb.php';
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO upload2 (name, size, type, path, comments) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$filecomments')";
mysql_query($query) or die('Error, query failed : ' . mysql_error());
include 'library/closedb.php';
$url = $_FILES['imagefile']['name']; // Set $url To Equal The Filename For Later Use
if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") {
$file_ext = strrchr($_FILES['imagefile']['name'], '.'); // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
$copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']); // Move Image From Temporary Location To Permanent Location
if ($copy) { // If The Script Was Able To Copy The Image To It's Permanent Location
print 'Image uploaded successfully.<br />'; // Was Able To Successfully Upload Image
$simg = imagecreatefromjpeg("$idir" . $url); // Make A New Temporary Image To Create The Thumbanil From
$currwidth = imagesx($simg); // Current Image Width
$currheight = imagesy($simg); // Current Image Height
if ($currheight > $currwidth) { // If Height Is Greater Than Width
$zoom = $twidth / $currheight; // Length Ratio For Width
$newheight = $theight; // Height Is Equal To Max Height
$newwidth = $currwidth * $zoom; // Creates The New Width
} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
$zoom = $twidth / $currwidth; // Length Ratio For Height
$newwidth = $twidth; // Width Is Equal To Max Width
$newheight = $currheight * $zoom; // Creates The New Height
}
$dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail
imagetruecolortopalette($simg, false, 256); // Create New Color Pallete
$palsize = ImageColorsTotal($simg);
for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image
$colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used
ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use
}
imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It)
imagejpeg($dimg, "$tdir" . $url, 100); // Saving The Image
imagedestroy($simg); // Destroying The Temporary Image
imagedestroy($dimg); // Destroying The Other Temporary Image
print 'Image thumbnail created successfully.'; // Resize successful
} else {
print '<font color="#FF0000">ERROR: Unable to upload image.</font>'; // Error Message If Upload Failed
}
} else {
print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is '; // Error Message If Filetype Is Wrong
print $file_ext; // Show The Invalid File's Extention
print '.</font>';
}
function createGallery( $pathToImages, $pathToThumbs )
{
// echo "Creating gallery.html <br />";
$output = "<html>";
$output .= "<head><title>Upload</title></head>";
$output .= "<body>";
$output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
$output .= "<tr>";
// open the directory
$dir = opendir($pathToThumbs );
$counter = 0;
// loop through the directory
while (false !== ($fname = readdir($dir)))
{
// strip the . and .. entries out
if ($fname != '.' && $fname != '..')
{
$output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname} \">";
$output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
$output .= "</a><br />{$fname}</td>";
$counter += 1;
if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
}
}
// close the directory
closedir($dir );
$output .= "</tr>";
$output .= "</table>";
$output .= "</body>";
$output .= "</html>";
// open the file
$fhandle = fopen( "gallery.html", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );
// close the file
fclose( $fhandle );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
// createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that
// the path will be a relative path working
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");
}
?>
#3
Re: How to display image and comments together
Posted 05 June 2009 - 10:16 AM
if (!isset($_GET['subpage']))
{ // Image Upload Form Below ?>
<form method="post" action="?subpage=upload" enctype="multipart/form-data">
<input type="file" name="imagefile" class="form" id="imagefile">
<br /> <br />
Comments about image
<br />
<input type="text" name="comments"/>
<br /><br />
<input name="Upload" type="submit" value="Upload" class="form">
</form>
Here the code to display images gallery that user uploaded in server. At moment it can only display image in gallery wihout the comment the user made about image. How can I dispaly image and comments the user uploaded in gallery? Thank you.
function createGallery( $pathToImages, $pathToThumbs )
{
// echo "Creating gallery.html <br />";
$output = "<html>";
$output .= "<head><title>Upload</title></head>";
$output .= "<body>";
$output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
$output .= "<tr>";
// open the directory
$dir = opendir($pathToThumbs );
$counter = 0;
// loop through the directory
while (false !== ($fname = readdir($dir)))
{
// strip the . and .. entries out
if ($fname != '.' && $fname != '..')
{
$output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname} \">";
$output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
$output .= "</a><br />{$fname}</td>";
$counter += 1;
if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
}
}
// close the directory
closedir($dir );
$output .= "</tr>";
$output .= "</table>";
$output .= "</body>";
$output .= "</html>";
// open the file
$fhandle = fopen( "gallery.html", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );
// close the file
fclose( $fhandle );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
// createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that
// the path will be a relative path working
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");
}
?>
#4
Re: How to display image and comments together
Posted 05 June 2009 - 10:35 AM
$query = "SELECT comments FROM upload2 WHERE name = '".$filename."'";
#5
Re: How to display image and comments together
Posted 05 June 2009 - 03:51 PM
this where original image is display but tried it was select out the function gallery still not displaying. Please illustrate how to output the comments. Thank very much
function createGallery( $pathToImages, $pathToThumbs )
{
// echo "Creating gallery.html <br />";
$output = "<html>";
$output .= "<head><title>Upload</title></head>";
$output .= "<body>";
$output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
$output .= "<tr>";
// open the directory
$dir = opendir($pathToThumbs );
$counter = 0;
// loop through the directory
while (false !== ($fname = readdir($dir)))
{
// strip the . and .. entries out
if ($fname != '.' && $fname != '..')
{
$output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}{$comments} \">";
$output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
$output .= "</a><br />{$fname}</td>";
$counter += 1;
if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
}
}
// close the directory
closedir($dir );
$output .= "</tr>";
$output .= "</table>";
$output .= "</body>";
$output .= "</html>";
// open the file
$fhandle = fopen( "gallery.html", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );
// close the file
fclose( $fhandle );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
// createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that
// the path will be a relative path working
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");
}
?>
#6
Re: How to display image and comments together
Posted 05 June 2009 - 05:33 PM
<?php
...
while(false !== ($fname = readdir($dir)))
{
// strip the . and .. entries out
if($fname != '.' && $fname != '..')
{
$comments = "";
// Change this query to fit your database/table
$result = mysql_query("SELECT comments FROM table WHERE filename = '".$fname."'");
// Check if the query succeeded and with a result
if($result && mysql_num_rows($result) == 1)
{
$row = mysql_fetch_array($result);
$comments = $row['comments'];
}
$output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}\">";
$output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
$output .= "</a><br />{$fname}<br />{$comments}</td>"; // Added "<br />{$comments}"
$counter += 1;
if($counter % 4 == 0)
{
$output .= "</tr><tr>";
}
}
}
...
?>
Ghost rider, on 6 Jun, 2009 - 12:51 AM, said:
this where original image is display but tried it was select out the function gallery still not displaying. Please illustrate how to output the comments. Thank very much
function createGallery( $pathToImages, $pathToThumbs )
{
// echo "Creating gallery.html <br />";
$output = "<html>";
$output .= "<head><title>Upload</title></head>";
$output .= "<body>";
$output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
$output .= "<tr>";
// open the directory
$dir = opendir($pathToThumbs );
$counter = 0;
// loop through the directory
while (false !== ($fname = readdir($dir)))
{
// strip the . and .. entries out
if ($fname != '.' && $fname != '..')
{
$output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}{$comments} \">";
$output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
$output .= "</a><br />{$fname}</td>";
$counter += 1;
if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
}
}
// close the directory
closedir($dir );
$output .= "</tr>";
$output .= "</table>";
$output .= "</body>";
$output .= "</html>";
// open the file
$fhandle = fopen( "gallery.html", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );
// close the file
fclose( $fhandle );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
// createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that
// the path will be a relative path working
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");
}
?>
#7
Re: How to display image and comments together
Posted 06 June 2009 - 04:55 AM
<?php
...
function createGallery( $pathToImages, $pathToThumbs )
{
// echo "Creating gallery.html <br />";
$output = "<html>";
$output .= "<head><title>Upload</title></head>";
$output .= "<body>";
$output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
$output .= "<tr>";
// open the directory
$dir = opendir($pathToThumbs );
$counter = 0;
// loop through the directory
while (false !== ($fname = readdir($dir)))
{
// strip the . and .. entries out
if ($fname != '.' && $fname != '..')
{
$comments = "";
// Change this query to fit your database/table
$result = mysql_query("SELECT comments FROM upload2 WHERE filename = '".$fname."'");
// Check if the query succeeded and with a result
if($result && mysql_num_rows($result) == 1)
{
$row = mysql_fetch_array($result);
$comments = $row['comments'];
}
$output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname} \">";
$output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
$output .= "</a><br />{$fname}<br />{$comments}</td>"; // Added "<br />{$comments}"
$counter += 1;
if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
}
}
// close the directory
closedir($dir );
$output .= "</tr>";
$output .= "</table>";
$output .= "</body>";
$output .= "</html>";
// open the file
$fhandle = fopen( "gallery.html", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );
// close the file
fclose( $fhandle );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
// createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that
// the path will be a relative path working
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");
}
?>
#8
Re: How to display image and comments together
Posted 06 June 2009 - 07:17 AM
Ghost rider, on 6 Jun, 2009 - 01:55 PM, said:
<?php
...
function createGallery( $pathToImages, $pathToThumbs )
{
// echo "Creating gallery.html <br />";
$output = "<html>";
$output .= "<head><title>Upload</title></head>";
$output .= "<body>";
$output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
$output .= "<tr>";
// open the directory
$dir = opendir($pathToThumbs );
$counter = 0;
// loop through the directory
while (false !== ($fname = readdir($dir)))
{
// strip the . and .. entries out
if ($fname != '.' && $fname != '..')
{
$comments = "";
// Change this query to fit your database/table
$result = mysql_query("SELECT comments FROM upload2 WHERE filename = '".$fname."'");
// Check if the query succeeded and with a result
if($result && mysql_num_rows($result) == 1)
{
$row = mysql_fetch_array($result);
$comments = $row['comments'];
}
$output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname} \">";
$output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
$output .= "</a><br />{$fname}<br />{$comments}</td>"; // Added "<br />{$comments}"
$counter += 1;
if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
}
}
// close the directory
closedir($dir );
$output .= "</tr>";
$output .= "</table>";
$output .= "</body>";
$output .= "</html>";
// open the file
$fhandle = fopen( "gallery.html", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );
// close the file
fclose( $fhandle );
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
// createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that
// the path will be a relative path working
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");
}
?>
#9
Re: How to display image and comments together
Posted 06 June 2009 - 08:09 AM
i need Ur databse table name, fileds name... i mean whats the name of ur database ...tables name then fields name bla bla ....exactly match.
then library/config.php, library/opendb.php, library/closedb.php
Please post ur hole code with mysql details which u used. I mean i need ... all stuffs which u have to run that script.
#10
Re: How to display image and comments together
Posted 06 June 2009 - 10:50 AM
anyway I tried your suggestion be still the same problem. Please check the code! Thank in advance
function createGallery( $pathToImages, $pathToThumbs )
{
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test2", $con);
// echo "Creating gallery.html <br />";
$output = "<html>";
$output .= "<head><title>Upload</title></head>";
$output .= "<body>";
$output .= "<table cellspacing=\"1\" cellpadding=\"2\" width=\"500\">";
$output .= "<tr>";
// open the directory
$dir = opendir($pathToThumbs );
$counter = 0;
// loop through the directory
while (false !== ($fname = readdir($dir)))
{
// strip the . and .. entries out
if ($fname != '.' && $fname != '..')
{
$comments = "";
// Change this query to fit your database/table
$result = mysql_query("SELECT comments FROM upload2 WHERE filename = '".$fname."'");
// Check if the query succeeded and with a result
if($result && mysql_num_rows($result) == 1)
{
$row = mysql_fetch_array($result);
$comments = $row['comments'];
}
$output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname} \">";
$output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"2\" />";
$output .= "</a><br />{$fname}<br />{$comments}</td>"; // Added "<br />{$comments}"
$counter += 1;
if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
}
}
// close the directory
closedir($dir );
$output .= "</tr>";
$output .= "</table>";
$output .= "</body>";
$output .= "</html>";
// open the file
$fhandle = fopen( "gallery.html", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );
// close the file
fclose( $fhandle );
mysql_close($con);
}
// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
// createThumbs("test2/","thumbs/",200);
// call createGallery function and pass to it as parameters the path
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that
// the path will be a relative path working
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");
}
?>
#11
Re: How to display image and comments together
Posted 06 June 2009 - 02:24 PM
$result = mysql_query("SELECT comments FROM upload2 WHERE filename = '".$fname."'");
To:
$result = mysql_query("SELECT comments FROM upload2 WHERE filename = '".$fname."'") or die("Query failed");
if the "Query failed" doesn't show the error is elsewhere, also make sure the database connection actually get established!
#12
Re: How to display image and comments together
Posted 07 June 2009 - 12:39 AM
first of all here is table
CREATE TABLE `image`.`img` ( `ID` INT NOT NULL , `image` VARCHAR( 50 ) NOT NULL , `comments` LONGTEXT NOT NULL ) ENGINE = MYISAM ;
index.php page
<?php
$con = mysql_connect("localhost","root","");
$db = mysql_select_db("image");
$query = mysql_query("select * from img");
?>
<p><a href="upload.php">Upload New Image</a></p>
<table width="120" border="0" cellpadding="2" cellspacing="0">
<?php
while($fetch = mysql_fetch_assoc($query))
{
?>
<tr>
<td align="center"><img src="images/<?php echo $fetch['image']?>" width="100" height="100" /></td>
</tr>
<tr>
<td align="center"><?php echo $fetch['comments']?></td>
</tr>
<?php
}
?>
</table>
upload.php
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="upload_code.php">
<table width="620" border="0" cellspacing="0" cellpadding="2">
<tr>
<td colspan="2" valign="top"><label>
<input type="file" name="image" id="image" />
</label>
<label></label></td>
</tr>
<tr>
<td width="148" valign="top">Comments</td>
<td width="472" valign="top"><label>
<textarea name="comments" id="comments" cols="45" rows="5">your comments</textarea>
</label></td>
</tr>
<tr>
<td valign="top"> </td>
<td valign="top"><input type="submit" name="button" id="button" value="Upload" /></td>
</tr>
</table>
<label></label>
</form>and upload_code.php
<?php
$con = mysql_connect("localhost","root","");
$db = mysql_select_db("image");
$file = $_FILES['image']['tmp_name'];
$filename = $_FILES['image']['name'];
$comments = $_POST['comments'];
$strQuery = "Insert into img (`image`,`comments`) Values ('$filename','$comments')";
if(!move_uploaded_file($file,'images/'.$filename))
{
header("Location: upload.php");
}
mysql_query($strQuery) or die(mysql_error());
header("Location: index.php");
?>
#13
Re: How to display image and comments together
Posted 07 June 2009 - 01:03 AM
I did change the code as you illustrated and the "Query failed" Message show in brownser. Also the image that I tried to upload it didn't display in gallery. Despite details of image and comments are uploaded in database and image in directory. Thank in advance
#14
Re: How to display image and comments together
Posted 07 June 2009 - 01:15 AM
#15
Re: How to display image and comments together
Posted 07 June 2009 - 02:28 AM
Ghost rider, on 7 Jun, 2009 - 10:03 AM, said:
I did change the code as you illustrated and the "Query failed" Message show in brownser. Also the image that I tried to upload it didn't display in gallery. Despite details of image and comments are uploaded in database and image in directory. Thank in advance
|
|

New Topic/Question
Reply




MultiQuote




|