Display images

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 2842 Views - Last Post: 04 July 2009 - 11:01 AM Rate Topic: -----

#1 NeekWorld   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-October 08

Display images

Post icon  Posted 30 June 2009 - 02:39 PM

ok i have searched everywhere and this will be like my 3rd post to different sites requesting some help on this.

i am trying to create an admin side of a website. this side should be able to view images that users have uploaded by username. i have the script to upload the images already. the script creates a file for the user with their username in a primary file that will be on the server to hold all users. i know im supposed to save the location of where the image is and store the location in the database. but how exactly do i go about it? do i need a second php file that has this new script or can i put a few lines of code in the one i have?

any help would be appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Display images

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,998
  • Joined: 08-August 08

Re: Display images

Posted 30 June 2009 - 03:13 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 NeekWorld   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-October 08

Re: Display images

Posted 30 June 2009 - 03:58 PM

View PostCTphpnwb, on 30 Jun, 2009 - 02:13 PM, said:

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field


no im not getting any errors with the upload script. i am unsure how to go about createing the download/display script. i have one that is used to create like an image gallery but that is not exactly what i want. and yes i know im supposed to post my code i have actually posted it in a different thread here and since changed the code i used. if you want i can post here but it works exactly they way i want.

what im not to sure about is. if i just insert the new location in the database for the user's images. how do i request them to be display/downloaded once i have the location in the database? i dont expect you to give me code i just need an idea of how to go about it.

i saw somewhere on here that someone was going to do a tutorial on how to do this but i didnt find the tutorial.

This post has been edited by NeekWorld: 30 June 2009 - 04:00 PM

Was This Post Helpful? 0
  • +
  • -

#4 RPGonzo   User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Display images

Posted 30 June 2009 - 04:47 PM

If your storing the location of the image in the DB than just pull it when needed and wrap it in a img tag ...
Was This Post Helpful? 0
  • +
  • -

#5 NeekWorld   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-October 08

Re: Display images

Posted 30 June 2009 - 04:57 PM

View PostRPGonzo, on 30 Jun, 2009 - 03:47 PM, said:

If your storing the location of the image in the DB than just pull it when needed and wrap it in a img tag ...


ok that makes sense but what if i dont know the total number of images to be displayed?
and i have another question. ive been playing with it. it is still uploading the images fine to the file. but the sql statement is not actually inserting the file location or the info i set it to enter into the database. im not getting an error on it. its just not inserting it into the database.

here is the code im using for the insert

if ($sizeOK && $typeOK){
		switch($_FILES['image']['error']) {
			case 0:
				// $username would normally come from a session variable
				$username = $_SESSION['login'];
				// if the subfolder doesn't exist yet, create it
				if (!is_dir(UPLOAD_DIR.$username)) {
					mkdir(UPLOAD_DIR.$username);
				}
				// make sure file of same name does not already exist
				if (!file_exists(UPLOAD_DIR.$username.'/'.$file)) {
					// get the date and time
					ini_set('date.timezone', 'Europe/London');
					$now = date('Y-m-d-His');
					// move the file to the upload folder and rename it
					$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now.$file);
					
					mysql_select_db($database, $conn);

					//sql query to insert file name into database
					$sql2 = "SELECT userName FROM users WHERE userName='$username'";   
	 				$result = mysql_query($sql2, $conn) or die(mysql_error());   
	 				$num = mysql_num_rows($result);
					
					if ($num > 0)  // user id found	   
					{
						$sql = "INSERT INTO images (image_id, userName, filename)
								VALUES ('', '$username', '$success')";   
					}
					else if ($num == 0)  // user id was not found		   
	 				{
						$message = "The User ID, '$_POST[login]' doesnt exist. <br>";
						$_SESSION['$message'] = $message;
					}
  
				}
				else {
					// get the date and time
					ini_set('date.timezone', 'Europe/London');
					$now = date('Y-m-d-His');
					$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now.$file);
					
					mysql_select_db($database, $conn);

					//sql query to insert file name into database
					$sql2 = "SELECT userName FROM users WHERE userName='$username'";   
	 				$result = mysql_query($sql2, $conn) or die(mysql_error());   
	 				$num = mysql_num_rows($result);
					
					if ($num > 0)  // user id found	   
					{
						$sql = "INSERT INTO images (image_id, userName, filename)
								VALUES ('', '$username', '$success')";   
					}
					else if ($num == 0)  // user id was not found		   
	 				{
						$message = "The User ID, '$_POST[login]' doesnt exist. <br>";
						$_SESSION['$message'] = $message;
					}
				}
				if ($success) {
					$result = "$file uploaded successfully";
				}
				else {
					$result = "Error uploading $file. Please try again.";
				}
			break;

This post has been edited by NeekWorld: 30 June 2009 - 04:58 PM

Was This Post Helpful? 0
  • +
  • -

#6 RPGonzo   User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Display images

Posted 30 June 2009 - 06:46 PM

VALUES ('', '$username', '$success')



If your image_id column is set to auto increment than you really don't even need to try and set a empty value ... it will do it on its own ..

If i remember right move_uploaded_file will only return a boolean result or nothing at all ... so inserting $success wont insert what you need ... you need to insert the $username.'/'.$now.$file which will hold the exact directory and file name to recall the file later ...

To only show as many images as the user has uploaded is not a problem ... use a while statement with a SELECT query that ONLY grabs images tagged with the username of your choice

$query = "SELECT filename FROM images WHERE userName='John Doe'";
$result = mysql_query($query);

while ($val = mysql_fetch_array($result)) {
   // do work with images ... 
   // list($width,$height) = getimagesize($val['filename']);
   // echo '<img src="'. $val['filename'] .'" height="'. $height .'" width="'. $width .'"/>';
}



The while loop will only run as long as their are results still being pulled ... in other words until all the images that were successfully inserted into the database are already present ...

hope that helps ...

This post has been edited by RPGonzo: 30 June 2009 - 06:49 PM

Was This Post Helpful? 0
  • +
  • -

#7 korin43   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-June 09

Re: Display images

Posted 30 June 2009 - 06:47 PM

You're inserting "true" instead of the file name:
$sql = "INSERT INTO images (image_id, userName, filename)
								VALUES ('', '$username', '$success')";  


Should be:
$image_location = UPLOAD_DIR.$username.'/'.$now.$file;
$sql = "INSERT INTO images (image_id, userName, filename)
								VALUES ('', '$username', '$image_location')";


After that you can pull it out of the database with something like (assuming $username is set):
$sql = "SELECT filename FROM images WHERE userName='$username'";
$query = mysql_query($sql);
while($image = mysql_fetch_assoc($query)){
  if(is_file($image['filename'])){
	echo "<img src=\"{$image['filename']}\" />";
  }
}


This post has been edited by korin43: 30 June 2009 - 06:50 PM

Was This Post Helpful? 0
  • +
  • -

#8 RPGonzo   User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Display images

Posted 30 June 2009 - 06:58 PM

As a note ... if(is_file($image['filename'])){ would be a good idea on the page the public sees BUT if this is for the admins to see .. it would be a good idea not to use it simply to be able to see broken image links and either fix them or remove them ...
Was This Post Helpful? 0
  • +
  • -

#9 NeekWorld   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-October 08

Re: Display images

Posted 30 June 2009 - 10:13 PM

View PostRPGonzo, on 30 Jun, 2009 - 05:46 PM, said:

VALUES ('', '$username', '$success')



If your image_id column is set to auto increment than you really don't even need to try and set a empty value ... it will do it on its own ..

If i remember right move_uploaded_file will only return a boolean result or nothing at all ... so inserting $success wont insert what you need ... you need to insert the $username.'/'.$now.$file which will hold the exact directory and file name to recall the file later ...

To only show as many images as the user has uploaded is not a problem ... use a while statement with a SELECT query that ONLY grabs images tagged with the username of your choice

$query = "SELECT filename FROM images WHERE userName='John Doe'";
$result = mysql_query($query);

while ($val = mysql_fetch_array($result)) {
   // do work with images ... 
   // list($width,$height) = getimagesize($val['filename']);
   // echo '<img src="'. $val['filename'] .'" height="'. $height .'" width="'. $width .'"/>';
}



The while loop will only run as long as their are results still being pulled ... in other words until all the images that were successfully inserted into the database are already present ...

hope that helps ...


ok i was thinking that i inserted the incorrect one. was going to do the

$_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now.$file

part instead of the $success in there, but shouldnt it still have at least put in the userName and id number?

View PostRPGonzo, on 30 Jun, 2009 - 05:58 PM, said:

As a note ... if(is_file($image['filename'])){ would be a good idea on the page the public sees BUT if this is for the admins to see .. it would be a good idea not to use it simply to be able to see broken image links and either fix them or remove them ...


well i have restricted access to portions of the page. so half of the page is visible to users and half is only for admin access. i didnt post all the code what im having a problem with is displaying the images for the admin side and saving the location of the uploaded images from the user

This post has been edited by NeekWorld: 30 June 2009 - 10:15 PM

Was This Post Helpful? 0
  • +
  • -

#10 korin43   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 30-June 09

Re: Display images

Posted 30 June 2009 - 10:31 PM

So you're not getting any errors but you don't have anything in your database? Have you checked in phpmyadmin or something?
Was This Post Helpful? 0
  • +
  • -

#11 NeekWorld   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-October 08

Re: Display images

Posted 30 June 2009 - 10:35 PM

View Postkorin43, on 30 Jun, 2009 - 09:31 PM, said:

So you're not getting any errors but you don't have anything in your database? Have you checked in phpmyadmin or something?


yes i checked phpMyAdmin everytime i tried to insert something, and i checked the folder i have the images sent to. the images are in the folder but the database still shows nothing. the only records in the images table are the ones i inserted manually. and i get no error message when i execute the script either from the php or the sql statement. so am puzzled at to why it is not inserting at least something into the database.
Was This Post Helpful? 0
  • +
  • -

#12 noorahmad   User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Display images

Posted 30 June 2009 - 11:16 PM

You are only Assigning Your Query to a variable, Not Executing it.
look here
$sql = "INSERT INTO images (image_id, userName, filename)
VALUES ('', '$username', '$success')";
//Here You Muse mysql_query function to Execute the Query



Try this code.
<?php
($sizeOK && $typeOK){
        switch($_FILES['image']['error']) {
            case 0:
                // $username would normally come from a session variable
                $username = $_SESSION['login'];
                // if the subfolder doesn't exist yet, create it
                if (!is_dir(UPLOAD_DIR.$username)) {
                    mkdir(UPLOAD_DIR.$username);
                }
                // make sure file of same name does not already exist
                if (!file_exists(UPLOAD_DIR.$username.'/'.$file)) {
                    // get the date and time
                    ini_set('date.timezone', 'Europe/London');
                    $now = date('Y-m-d-His');
                    // move the file to the upload folder and rename it
                    $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now.$file);
                   
                    mysql_select_db($database, $conn);

                    //sql query to insert file name into database
                    $sql2 = "SELECT userName FROM users WHERE userName='$username'";  
                     $result = mysql_query($sql2, $conn) or die(mysql_error());  
                     $num = mysql_num_rows($result);
                   
                    if ($num > 0)  // user id found      
                    {
                        $sql = "INSERT INTO images (image_id, userName, filename)
                                VALUES (NULL, '$username', '$success')";  
                    }
                    else if ($num == 0)  // user id was not found          
                     {
                        $message = "The User ID, '$_POST[login]' doesnt exist. <br>";
                        $_SESSION['$message'] = $message;
                    }
 
                }
                else {
                    // get the date and time
                    ini_set('date.timezone', 'Europe/London');
                    $now = date('Y-m-d-His');
                    $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now.$file);
                   
                    mysql_select_db($database, $conn);

                    //sql query to insert file name into database
                    $sql2 = "SELECT userName FROM users WHERE userName='$username'";  
                     $result = mysql_query($sql2, $conn) or die(mysql_error());  
                     $num = mysql_num_rows($result);
                   
                    if ($num > 0)  // user id found      
                    {
                        $sql = "INSERT INTO images (image_id, userName, filename)
                                VALUES (NULL, '$username', '$success')";
								mysql_query($sql)or die(mysql_error());
                    }
                    else if ($num == 0)  // user id was not found          
                     {
                        $message = "The User ID, '$_POST[login]' doesnt exist. <br>";
                        $_SESSION['$message'] = $message;
                    }
                }
                if ($success) {
                    $result = "$file uploaded successfully";
                }
                else {
                    $result = "Error uploading $file. Please try again.";
                }
            break
?>



Hope it Help You :)

This post has been edited by noorahmad: 30 June 2009 - 11:18 PM

Was This Post Helpful? 0
  • +
  • -

#13 NeekWorld   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-October 08

Re: Display images

Posted 01 July 2009 - 12:27 PM

View Postnoorahmad, on 30 Jun, 2009 - 10:16 PM, said:

You are only Assigning Your Query to a variable, Not Executing it.
look here
$sql = "INSERT INTO images (image_id, userName, filename)
VALUES ('', '$username', '$success')";
//Here You Muse mysql_query function to Execute the Query



Try this code.
<?php
($sizeOK && $typeOK){
        switch($_FILES['image']['error']) {
            case 0:
                // $username would normally come from a session variable
                $username = $_SESSION['login'];
                // if the subfolder doesn't exist yet, create it
                if (!is_dir(UPLOAD_DIR.$username)) {
                    mkdir(UPLOAD_DIR.$username);
                }
                // make sure file of same name does not already exist
                if (!file_exists(UPLOAD_DIR.$username.'/'.$file)) {
                    // get the date and time
                    ini_set('date.timezone', 'Europe/London');
                    $now = date('Y-m-d-His');
                    // move the file to the upload folder and rename it
                    $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now.$file);
                   
                    mysql_select_db($database, $conn);

                    //sql query to insert file name into database
                    $sql2 = "SELECT userName FROM users WHERE userName='$username'";  
                     $result = mysql_query($sql2, $conn) or die(mysql_error());  
                     $num = mysql_num_rows($result);
                   
                    if ($num > 0)  // user id found      
                    {
                        $sql = "INSERT INTO images (image_id, userName, filename)
                                VALUES (NULL, '$username', '$success')";  
                    }
                    else if ($num == 0)  // user id was not found          
                     {
                        $message = "The User ID, '$_POST[login]' doesnt exist. <br>";
                        $_SESSION['$message'] = $message;
                    }
 
                }
                else {
                    // get the date and time
                    ini_set('date.timezone', 'Europe/London');
                    $now = date('Y-m-d-His');
                    $success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$username.'/'.$now.$file);
                   
                    mysql_select_db($database, $conn);

                    //sql query to insert file name into database
                    $sql2 = "SELECT userName FROM users WHERE userName='$username'";  
                     $result = mysql_query($sql2, $conn) or die(mysql_error());  
                     $num = mysql_num_rows($result);
                   
                    if ($num > 0)  // user id found      
                    {
                        $sql = "INSERT INTO images (image_id, userName, filename)
                                VALUES (NULL, '$username', '$success')";
								mysql_query($sql)or die(mysql_error());
                    }
                    else if ($num == 0)  // user id was not found          
                     {
                        $message = "The User ID, '$_POST[login]' doesnt exist. <br>";
                        $_SESSION['$message'] = $message;
                    }
                }
                if ($success) {
                    $result = "$file uploaded successfully";
                }
                else {
                    $result = "Error uploading $file. Please try again.";
                }
            break
?>



Hope it Help You :)


i actually have that line in the one i was playing with after i posted the code

this is what i have now and it still doesnt execute the query

mysql_select_db($database, $conn);

					//sql query to insert file name into database
					$sql2 = "SELECT userName FROM users WHERE userName='$username'";   
	 				$result = mysql_query($sql2, $conn) or die(mysql_error());   
	 				$num = mysql_num_rows($result);

if ($num > 0)  // user id found	   
					{
						$sql = "INSERT INTO images (filename, userName)
								VALUES ('UPLOAD_DIR.$username.'/'.$now.$file', '$username')"; 
		 				$result = mysql_query($sql, $conn) or die("Can't execute insert query.");   
  
					}
					else if ($num == 0)  // user id was not found		   
	 				{
						$message = "The User ID, '$_POST[login]' doesnt exist. <br>";
						$_SESSION['$message'] = $message;
					}

Was This Post Helpful? 0
  • +
  • -

#14 NeekWorld   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 05-October 08

Re: Display images

Posted 01 July 2009 - 01:59 PM

ok so i fixed that problem. i forgot to execute on both sections. but now im not getting the images to show up this is the section i have for the code. i used a dynamic table to insert the info so it will load what ever is in the record set. but all it gives me is either blank or the file location. any thoughts?

<table border="1">
	  <?php do { ?>
		<tr>
		  <td>file name</td>
		  <td><?php echo '<img src="'. $row_Recordset2['filename'].'"/>'; ?></td>
		</tr>
		<tr>
		  <td>User Name</td>
		  <td><?php echo $row_Recordset2['userName']; ?></td>
		</tr>

		<?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?>
	</table>


this line gives me a blank space in the table
<?php echo '<img src="'. $row_Recordset2['filename'].'"/>'; ?>


but if i change it to
<?php echo  $row_Recordset2['filename']; ?>

if will give me the file location
Was This Post Helpful? 0
  • +
  • -

#15 peter yianni   User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 310
  • Joined: 04-June 09

Re: Display images

Posted 01 July 2009 - 02:14 PM

View PostNeekWorld, on 1 Jul, 2009 - 12:59 PM, said:

ok so i fixed that problem. i forgot to execute on both sections. but now im not getting the images to show up this is the section i have for the code. i used a dynamic table to insert the info so it will load what ever is in the record set. but all it gives me is either blank or the file location. any thoughts?

<table border="1">
	  <?php do { ?>
		<tr>
		  <td>file name</td>
		  <td><?php echo '<img src="'. $row_Recordset2['filename'].'"/>'; ?></td>
		</tr>
		<tr>
		  <td>User Name</td>
		  <td><?php echo $row_Recordset2['userName']; ?></td>
		</tr>

		<?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?>
	</table>


this line gives me a blank space in the table
<?php echo '<img src="'. $row_Recordset2['filename'].'"/>'; ?>


but if i change it to
<?php echo  $row_Recordset2['filename']; ?>

if will give me the file location



i was having the same problem this might help

<a href="../jocky_history.php?horse=<?php echo $row_jockyhistory['name_id']; ?>">

just change to your setings
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3