Rikke Posted June 2, 2007 Share Posted June 2, 2007 Hi all, Im new in this forum. I´ve tried to solve an uploading script, but ran into probs. I want to upload imgs to a MySql db (yes I acctually do want that). I have a html form and php enginge. I have managed to put in the textfields into db. Also there is SOMETHING that comes into blob-field but it seems to be the word "Array" sooner than actual binary data. I have one html-doc with the input and form fields, and then I have a php-doc that does all the file input work. The code is somewhat copy pasted from different scripts. I do that a lot, and for the most time is does the job, and I also get to learn about new functions, but sometimes unfortunately, no deep learning of what the functions are all about.... The script should be a news update db. I want users to be able to upload an img, header and short text, from whereever they might be around the world. Heres my html code: <html> <body> <form action="insert.php" method="post" enctype="multipart/form-data"> Rubrik: <input type="text" name="rubrik" /><br> Text: <input type="text" name="text" /><br> Artno: <input type="text" name="artno" /><br> Picture: <input type="file" name="img" size="40"><br> <input type="submit" /> </form> </body> </html> Heres my PHP code: [code=php:0]<?php $con = mysql_connect("localhost","user","pw"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db", $con); $sql="INSERT INTO nyheter (`rubrik`, `text`, `artno`, `img`) VALUES ('$_POST[rubrik]','$_POST[text]','$_POST[artno]','$_FILES[img]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> [/code] My guess from an amateur point of view is that the file goes into a temp lib somewhere, and I have to reffer to that lib and tell MySql where to find it... But Im on thin ice here. I also read something about "add slashes" and "max filesize" wich I cannot make anything out from. Can someone pls help me out? Quote Link to comment https://forums.phpfreaks.com/topic/53966-uploading-img-to-mysql-not-working/ Share on other sites More sharing options...
Barand Posted June 2, 2007 Share Posted June 2, 2007 The file you uploaded will be stored in this filename : $_FILES['img']['tmp_name']; You need to get the contents of that file , file_get_contents(), and store that in the database. $imagedata = file_get_contents($_FILES['img']['tmp_name']); Having put it in the database, have you considered how you are going to display the stored images? It's much easier to move the uploaded file to a folder on the server and store its location in the database. Quote Link to comment https://forums.phpfreaks.com/topic/53966-uploading-img-to-mysql-not-working/#findComment-266788 Share on other sites More sharing options...
Rikke Posted June 2, 2007 Author Share Posted June 2, 2007 Having put it in the database, have you considered how you are going to display the stored images? It's much easier to move the uploaded file to a folder on the server and store its location in the database. Hi Barand, I have a page that already prints the pictures alright. Also a bit of a workaround with two pages; one printing the image with help of the id field, and the main page that uses include to show what was printed on the other page. Ive understood that the solution in not optimal, but I want to give it a try. Basically it is to be able to have users uploading news from anywhere in the world without giving ftp access to my server. I could imagine using a script that uploads the picture to a temp folder instead, and putting just filename and location into db instead, but unfortunately I know even less how to get such a script up and running. $imagedata = file_get_contents($_FILES['img']['tmp_name']); Should I replace the value $_FILES with the above code? Cause this is where I fail. I dont understand how to actually USE the tips Ive got. I need a very hands on example of the solution :-S Quote Link to comment https://forums.phpfreaks.com/topic/53966-uploading-img-to-mysql-not-working/#findComment-266899 Share on other sites More sharing options...
Barand Posted June 2, 2007 Share Posted June 2, 2007 <?php $imagedata = file_get_contents($_FILES['img']['tmp_name']); $sql="INSERT INTO nyheter (`rubrik`, `text`, `artno`, `img`) VALUES ('$_POST[rubrik]','$_POST[text]','$_POST[artno]','$imagedata')"; ?> A couple of other pointers SQL injection - dangerous writing anything from the client (GET,POST or COOKIE) direct to your database String index values should be quoted ie $_POST['artno'] otherwise the php processor assumes they are constants and looks for the definition. If it can't find the definition it then treats them as string values, but it all wastes time. Quote Link to comment https://forums.phpfreaks.com/topic/53966-uploading-img-to-mysql-not-working/#findComment-267062 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.