Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/53966-uploading-img-to-mysql-not-working/
Share on other sites

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.

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

<?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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.