The code below is what i am using to upload a photo to store it in a database, is it being stored correctly?? as it will not let me view them properly, any help is great!!!

$title = $_POST["title"];
$userfile = $_FILES['userfile']['tmp_name'];
$userfile_name = $_FILES['userfile']['name'];	
$userfile_size = $_FILES['userfile']['size'];
$userfile_type = $_FILES['userfile']['type'];
$userfile_error = $_FILES['userfile']['error'];


	if ($userfile=='none')
	   {
		echo 'You have not attempted to upload a file!!';
		?>
			<a href="uploadphoto.php"><br />Please try again...</a>
		<?php
		exit;
	   }

	if ($userfile_size==0)
	   {
		echo 'The file you are trying to upload has 0 length!!';
		?>
			<a href="uploadphoto.php"><br />Please try again...</a>
		<?php
		exit;
	   }


	if (strpos($userfile_type , 'image/') !== 0)
	   {
		echo 'The file you are trying to upload is not an image!!';
		?>
			<a href="uploadphoto.php"><br />Please try again...</a>
		<?php
		exit;
	   }



	if($userfile_error > 0)
	   {
		echo 'A prolem has occured whilst trying to upload the photo!!';
		?>
			<a href="uploadphoto.php"><br />Please try again...</a>
		<?php
		exit;
	   }

$PSize = filesize($userfile);
$mysqluserfile = addslashes(fread(fopen($userfile, "r"), $PSize));

$upfile = 'C:/wamp/www/Playhill/images/'.$userfile_name;
	
	if (is_uploaded_file($userfile))
	   {
		
		if(!copy($userfile, $upfile))
		   {
			echo 'Sorry the file could not be moved!!';
		?>
			<a href="uploadphoto.php"><br />Please try again...</a>
		<?php
		exit;
		   }	


mysql_query("INSERT INTO photos (title, name) VALUES ('$title','$mysqluserfile')");
	} else {
		echo"You did not upload any picture"; 

		?>
			<a href="uploadphoto.php"><br />Please try again...</a>
		<?php
}


	echo '<br />Your image below has been uploaded succesfully!<br /> <br />';

Recommended Answers

All 9 Replies

Member Avatar for diafol

Are you storing the actual file in the DB and on disk? Looks like to me, but haven't looked too closely. Why? Do you really need the file in the DB, will a url do?

I am trying to store the file in mysql database? is what i am doing corect?

Member Avatar for diafol

Yeah, fine. It's just that you may find it easier to store files in your image folder and have a reference to those images in your DB (url, description, tags etc). Ripping info from a db and 'creating' a file from said info may be a bit intensive.

A technique I've used, and it's by no means the only way to do it, is to store uploaded images with names like image_x.png etc, where x = primary key value of the DB entry referring to said image.

Image table:

id (PK)/faux_name (name you wish to call the file)/extension (png/jpg/gif etc)/description (e.g. for alt parameter)/tags (list of keywords)

...
10 landscape gif cloudy day in Swansea Swansea, clouds, landscape, town
...

my images folder can then hold files thus:

image_1.png, ..., image_10.gif, ..., image_56.jpg etc

The way to set this up would be to upload the file and save it to your images folder. On successful upload, add image data to DB (strip extension from filename), get faux_name from filename (default) or can be overridden by form textbox entry, description from form textarea, tags from form textarea (textboxes can also be used). Use mysql_insert_id() to get the primary key value for the file and rename the uploaded file to something like "image_" . $pkid . "." . $ext.

If you do this, you can change the faux_name (displayed in a web page) to whatever you like, but the reference to the actual filename (image_10.gif) persists. This method does have disadvantages though - duplicate faux_names are permissible unless you index the field as unique. Also, you need to search the DB for faux_names first for the PK if you want to check the existence of an image file.

Sorry if I'm rambling, just an idea.

you think it would be alot easier to store them in an image folder and reference it?? maybe thats the option i should take, wud any off my code be worth keeping then?

Member Avatar for diafol

you think it would be alot easier to store them in an image folder and reference it?? maybe thats the option i should take, wud any off my code be worth keeping then?

It's your choice - both methods have their pros and cons. Strangely, the majority of your code will work with both methods. You just need to drop the file reading stuff and just paste a reference in its stead for my proposed method.

can you help with which exact stuff needs dropped?? thanks

Member Avatar for diafol

$mysqluserfile = addslashes(fread(fopen($userfile, "r"), $PSize));

//DELETE ABOVE

//get the image extension (jpg, png etc) - place into $ext

mysql_query("INSERT INTO photos (title, name) VALUES ('$title','$mysqluserfile')");

//REPLACE ABOVE WITH title,faux_name, etc.
$id = mysql_insert_id();

//rename the uploaded file to format image_$id . "." . $ext

Job done.

Thanks, have that working, just another thing,

when i sometimes try to upload an image it says it has no length even if it has? why is it doin this, i am using pretty mush the code posted if you look at that, thanks!

Member Avatar for diafol

What's the actual error message? I suggest you copy the impt bit of the message and slap it straight into google. Should give you some good answers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.