Uploading image into page table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Xeal
Forum Newbie
Posts: 5
Joined: Thu Jan 29, 2004 1:46 am
Location: Sweden

Uploading image into page table

Post by Xeal »

Hi!
I have been working a little with php code and my current project is to make something like deviantart ( http://www.deviantart.com).
The difference is that the image will be put in a table data at the same row with the information to it ( input from html text fields).

But now comes the question:

This image in the table data should be an image link to a new page where the image shows up.
Which means that I also need some way to automatically make a thumbnail of the image.

Help in any way is very much appreciated!!
User avatar
bluenote
Forum Commoner
Posts: 93
Joined: Sat Mar 01, 2003 4:59 am
Location: Heidelberg, Germany

Uploading image into page table

Post by bluenote »

Hi,

at first i think storing images directly in the db is a *bad* idea. It's much better and more performant if you store the image in the file system and the "/usr/local/Web/shared/images/your_image" path in the db.

Here's a short (and working) example:

Code: Select all

<?php

if (isset ($_POST["is_uploaded_file"])) {
				
				$filetype = $_FILES["your_image"]["type"];
				$filesize = $_FILES["your_image"]["size"];
				$filename = $_FILES["your_image"]["name"];
				$filetemp = $_FILES['your_image']['tmp_name'];
				$fileerror = $_FILES['your_image']['error'];
				
				$tmp_size = $filesize / 1024;
				
				if (($filetype=='image/jpeg') || ($filetype=='image/pjpeg') || ($filetype=='image/jpg')) {
					
					$extension = ".jpg";}
					
					else if ($filetype=='image/gif') {
						
						$extension = ".gif";}
	
$filename_prefix = "your_name";					
						$filename = "$filename_prefix$extension";
						
						if ($filesize <= $max_file_size) {
							
							if (is_uploaded_file($filetemp)){
								
								move_uploaded_file($filetemp,"$uploaddir_staff/".$filename_new_b."$extension");
								
								chmod ("/$uploaddir_staff/$filename_new_a", 0644);
								
								$query = "INSERT INTO ...}
								
								else { ...}}}
?>
This snippet needs an enctype=\"multipart/form-data\" form in front with an input field like this:

Code: Select all

<?php
echo  "<input type="radio" name="is_uploaded_file[]" value="1">&nbsp;&nbsp;<input type="file" name="your_image" value="" size="20">";
?>
It checks wether the file size is not larger than your MAX_FILE_SIZE (to be defined as an hidden field in the form, or, much better and safer, in the php.ini) and wether the files upload has worked; then, it moves the re-named file to its destination directory and chmod it to be redable for all.

To generate a thumb of this image, you could use the
getimagesize
imagecopyresized
functions documented at http://www.php.net to generate the thumb for display on the webpage; the link you can generate using the storage path and some environment vars.

Hope this helps a little. Greez,
-bluenote
Post Reply