PHP and images

I’ve been having a bit of trouble trying to get images to display on my site, I can’t figure what kinda of method to use… I want to store the image file paths in the database and the images themselves in the file system, but I dunno how to go about and doing it. (FYI I just started learning PHP, so I’m a bit of a newbie)

Have a read of the following article http://phpmaster.com/file-uploads-with-php/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+PHPMaster_feed+(PHPMaster)

I don’t see where it mentions how to display the image onto the site, from what I see it just shows how to upload the images to the specified folder, I already have images in the folder, but now I want to display them onto the site.

Can you show us the code that you have so far? It’s kind of hard to show you how unless we can see what you have so far.

Well I’m not sure exactly how to start it, but for right now, I’m basically using the code from the site that chris.upjohn mentioned. but all that does is upload the file as far as I know.

You basically will use the upload directory plus the name variable within the while loop to create the path to the image.


$pathtoimage = UPLOAD_DIR . $parts["filename"] . "-" . $i . "." . $parts["extension"];
echo "<img src='" . $pathtoimage . "' />";

UPLOAD_DIR is your constant that points you to the correct folder, and the rest creates the file’s name and extension.

Is the filename part in the code you specified the place where I put the file path for the image?

Edit: It seems all of this is just flat out confusing for me, i guess I went into something too advanced, since I’ve only been working with PHP for a couple of months now, is there a guide for complete newbies like me to help me to gradually understand these advanced things? Of course, I can easily do the simple codes like the hello world and stuff like that, but going into the stuff like file uploading, and others, seem too much for me right now.

Take your pick http://products.sitepoint.com/?tag=&filters[tag]=php&filters[difficulty]=&simpleform_submit_marker=showme

EDIT: The URL keeps screwing up so you will just need to copy it.

Thanks for the link, and I’m sorry that I couldn’t understand what you all were saying.

  1. have you reached the point where you are able to upload the images and store their names/paths in the database table?
  2. have you reached the point where you can list the information in your database table?

I don’t see where it mentions how to display the image onto the site, from what I see it just shows how to upload the images to the specified folder, I already have images in the folder, but now I want to display them onto the site.

Use the script below and let us know if this is what you are looking for:

<?php
   $dir = "images/"; // put the name of your images folder between "", example: my_folder/my_images/
   $images = glob("$dir*{jpg,png,gif}", GLOB_BRACE); // put a list of image types between {} 
   foreach ($images as $img)
   {
	   echo "<img src='$img' alt='Unable to show $img'/><br/>";
   }   
?> 

Well luckily, thanks to a tutorial on youtube, I got it worked out, now I’m trying to get the the image source tag to become a variable, but I can’t get it right, and here’s the code:

foreach ($dir_contents as $file) {
	$file_type = strtolower(end(explode('.', $file)));
	
	if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
		echo '<img src="', $dir, '/', $file, '" alt="', $file, '" width="80px;" height="100px" />';
		}
	}
}

$sql = "SELECT * FROM phpbb_anime";
$result = $db->sql_query($sql);
$row = $db->sql_fetchrow($result);
$db->sql_freeresult($result);

$name = $row['name'];
$desc = $row['description'];

$template->assign_vars(array(
    'ANIME_NAME'   		=> $name,
    'ANIME_DESCRIPTION' => $desc,
	'ANIME_PIC'			=> '<img src="', $dir, '/', $file, '" alt="', $file, '" width="80px;" height="100px" />',
   )
);

The way it is currently, the image will just display out on the page with no formatting, and I’m working with phpbb, so I’m making it a variable like everything else.

I couldn’t edit my above post so I had to double post, sorry, but anyway, problem solved. I just changed the echo portion to this

$animeimg = "<img src='" . $dir . "/" . $file . "' alt='" . $file . "' width='80px;' height='100px' />";

and then I just included that variable with all the others; with it like that, it worked beautifully! :smiley: