not uploading image file in mysql database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Ghost rider
Forum Newbie
Posts: 1
Joined: Thu Nov 06, 2008 11:43 am

not uploading image file in mysql database

Post by Ghost rider »

I'm using WAMP to test my php and mysql code. however, when I check in database is not up loading image files . Also I'm not sure about this tempfile code! I don't know where to create this tempfile to load the image before could be transfer to the server.Thanks. There are two scripts.

UPLOAD.PHP
<?php

require ('db_conn.php'); // connect to database

if(isset($_POST['submit'])) // Has a file been uploaded?

{
// See if it is of one of the allowed mime types, change these if you like
if (($_FILES['uploadedfile']['type'] == "image/gif")||
($_FILES['uploadedfile']['type'] == "image/jpeg"))
{


// This is the temporary place where it got put on the server after it was uploaded
$tempfile = $_FILES['uploadedfile']['tmp_name'] ; WHERE TO CREATE THIS FILE, I DON'T KNOW

// addslashes so as not to break anything
$data = addslashes(fread(fopen($tempfile, "rb"), filesize($tempfile)));

// pull out useful bits in case they are needed.
$filetype = $_FILES['uploadedfile']['type'];
$filesize = $_FILES['uploadedfile']['size'];
$filename = $_FILES['uploadedfile']['name'];

// Stick it into the database.
$query = ("INSERT INTO uploads1(data, filename, filesize, filetype) VALUES
('$data','$filename','$filesize','$filetype')");
mysql_query($query);
}
// If it wasn't one of the allowed file types do this:
else
{
echo "Invalid File type<br>";
}
}

// Form to upload a new file, make sure you get the "enctype" or it won't work
echo "
<form enctype='multipart/form-data' name='fileupload' action='upload.php' method='POST'>

<input type='file' name='uploadedfile'>
<input type='submit' name='upload1' value='Upload File'>

</form>
";

?>



SETUPDB.PHP
<?php

require ('db_conn.php'); // Connect to the database

mysql_query("drop table uploads1"); // if there already is a uploads table drop it.
//This will let you use this as a reset button while testing

// Create a table to store the files in.
mysql_query("create table uploads1 (uploadid integer not null auto_increment primary key,
data longblob, filename varchar(255), filesize integer, filetype varchar(255) )");

// uploadid = the unique key for each entry, autonumbered.
// data (longblob) - the contents of the file.
//There are other forms of blobs that will work, this allows the largest.
// filename - the original filename
// filesize
// filetype - mimetype.


?>
Post Reply