Jump to content

Insert image name and filetype into database on upload?


Anders Moen

Recommended Posts

Hey!I want to do something like that when someone uploads an image, they image will be added into a folder, called for example upload and in the database it should also display the filetype and filename.So if I upload an image called Anders.jpg from C:\Documents and Settings\All users\My folder\Anders.jpg the image Anders.jpg will be added into a folder called upload + in the database it says [*img]upload/Anders.jpg[/img*] (without stars of course), and then it prints out the image he/she uploaded.How?Upload code:

<?phpif (isset($_POST['submit'])) {if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000))  {  if ($_FILES["file"]["error"] > 0)	{	echo "Error: " . $_FILES["file"]["error"] . "<br />";	}  else	{	echo "Upload: " . $_FILES["file"]["name"] . "<br />";	echo "Type: " . $_FILES["file"]["type"] . "<br />";	echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";	echo "Stored in: " . $_FILES["file"]["tmp_name"];	}  }else  {  echo "Invalid file";  }}?>

and then the insert code

<?phpif (isset($_POST['submit'])) {$con = mysql_connect("localhost","peter","abc123");if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("my_db", $con);$sql="INSERT INTO person (FirstName, LastName, Age)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";if (!mysql_query($sql,$con))  {  die('Error: ' . mysql_error());  }echo "Your image was added";mysql_close($con);}?>

But what should I change FirstName, LastName and Age with? I don't know.And by the way, I'm gonna use mysql_real_escape_string so don't use VALUES ('$_POST[name]). The last thing I need is MySQL injections :)

Link to comment
Share on other sites

Use move_uploaded_file to put the file in the destination directory. Look up the reference for move_uploaded_file to get plenty of examples, both in the documentation and the comments for it. You already have the file name, so I assume you don't need me to tell you how to get that in the database. And you'll probably want to add image/jpeg to the list of mime types, I think IE is the only thing that uses image/pjpeg. I may be wrong about that though.

But what should I change FirstName, LastName and Age with? I don't know.
What do you mean? They are all coming from post, what do you want to change them with? What else would you insert into a table called person?
And by the way, I'm gonna use mysql_real_escape_string so don't use VALUES ('$_POST[name]).
Let me guess. You couldn't be bothered to do it right the first time.
Link to comment
Share on other sites

What? I didn't understand a word of what you just said =/I mean that when someone upload a file, the filename and filetype gets inserted into a table like this: [*img]http://mysite.com/upload/filename.filetype[/img*] without the stars of course. Do you understand now?

Link to comment
Share on other sites

I understood the first time. You have the filename, which includes the extension: echo "Upload: " . $_FILES["file"]["name"] . "<br />";you have the mime type: echo "Type: " . $_FILES["file"]["type"] . "<br />";So what's the problem? Do you not know how to insert those in a database and read them back out?

Link to comment
Share on other sites

Oh! I'll try afterwards, I think I know why I couldn't make it work last time now.I forgot to put $_FILES in front on the $file = $_FILES['type']; etcMaybe it'll work later tonight when I'm gonna try it then

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...