Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to upload/save image on MySQL DB

Status
Not open for further replies.

amir4oracle

Programmer
Nov 3, 2004
46
0
0
CA
To upload/save image on MySQL DB I created the following table:

CREATE TABLE test_pic
( name varchar (30), photo varchar(30) );

and to upload and save an image file in MySQL DB I wrote the following code in addpic.php file:

---------------------------------------------------------

<?php
$con = mysql_connect (" "amirak17_test", "iud");
$db = mysql_select_db ("amirak17_test", $con);

error_reporting (E_ERROR);

$target = "/images/" . basename( $_FILES['photo']['name']);

$name = $_POST ['name'];
$pic = ($_FILES['photo']['name']);

mysql_query ("INSERT INTO test_pic VALUES ('$name', '$pic')");

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded.";
else
echo "Sorry, there was a problem uploading your file.";

?>

<form enctype="multipart/form-data" action="addpic.php" method="POST">

Name: <input type="text" name = "name"> <br />
Photo: <input type="file" name = "photo"> <br />

<input type="submit" value="Add">
</form>

---------------------------------------------------------
---------------------------------------------------------

To view the contents of the db I used the following code, in viewpic.php file, which only shows the name field not the pic :

---------------------------------------------------------
<?php

$con = mysql_connect (" "amirak17_test", "iud");
$db = mysql_select_db ("amirak17_test", $con);

$data = mysql_query("SELECT * FROM test_pic") or die(mysql_error());

while($info = mysql_fetch_array( $data ))
{
echo "<img src=['photo'] ."> <br>";
echo "Name: ".$info['name'] . "<br>";
}

?>

---------------------------------------------------------
---------------------------------------------------------

What am I missing, which is not uploading the file. The code works fine on a local computer.

Your help is highly appreciated.
 
i'd guess as a start you need to name your file element the same as your array.

so change this line
Code:
  echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded.";
to
Code:
  echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded.";

this will not have worked fine on any computer.

i'd also guess that you will need a larger field size if people are to be allowed to upload long file names. or put a limit on the field input size.

are you confident that "/images" is a directory relative to your webroot? i would not be so sure and might perhaps use "images/" instead. this could well be why your code works on one pc and not another.
 
I see a number of problems with your code.

First, in the first script you are trying to use values before they exist. All of your PHP code will attempt to run before the form is ever sent to the browser. That first script would be better in the form:

Code:
<?php
if (isset ($_POST['photo']))
{
   //do all the steps to process the input
}
else
{
   //output the HTML necessary to show the user the form
}
?>


Then there is the problem of your table in MySQL. a varchar(30) is nowhere near enough room, nor of the right type, to store the binary data from a picture file. You should be using the BLOB type or something similar if you want to store the actual picture in the database.

However, I recommend that you only store the filename in the database, and store the actual file on the filesystem.






Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks jpadie and sleipnir

i guess its the matter of an entry in php.ini: upload_tmp_dir

on my local machine its value is c:\php\uploadtemp
but on the it is "no value"

so I will have to talk to the web host.

But can you tell me is there a way I can store the web pages of on my local server. Do I have to have a Static IP address from my ISP? and what is the rest of the procedure?

Thanks.
 
in the absence of an express value php will use the operating systems temporary directory to store uploaded images. remember that these get deleted at the end of the script.

therefore any errors in your code are unlikely to have anything to do with the php.ini directive. i would suggest that the primary error lies with the use of "/images" rather than a relative path.

i'm assuming your last question is not php related (i.e. you are not looking for a php solution to a remote storage issue). you can store pages anywhere you want so long as they are available to the public. you do not need a static ip - you could use a dynamic dns service such as dyndns. i would not recommend hosting a public facing site yourself unless you have a very robust internet connection and power source. where i live, for example, power surges cause my adsl line to desynch every day at least once and with remote hosting being so cheap at the moment there is really no need to host at home.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top