PDA

View Full Version : Uploading Images to mySQL


greens85
10-02-2007, 02:25 PM
Hi All,

I'm currently trying to produce a form that allowed users to upload images to the mySQL database, as im new to this i thought it would be best to look at tutorials on this. Using them i have managed to produce the following code but when i try and upload an image it says the image file size is too big (even if i just click straight on submit).

Can anybody see where i might be going wrong:

<label>Please Select An Image To Upload:</label>
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input name="userfile[]" type="file" />
<input type="submit" value="Submit Image" />
</form>
<?php
// check if a file was submitted
if(!isset($_FILES['userfile'])) {
echo '<p>Please select a file</p>';
}
else
{
try {
upload();
// submission was successful
echo '<p>Thank you for submitting</p>';
}
catch(Exception $e) {
echo $e->getMessage();
echo 'Sorry, could not upload file';
}
}
?>
<?php
// the upload function
function upload(){

if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {

// check the file is less than the maximum file size
if($_FILES['userfile']['size'] < $maxsize)
{
// prepare the image for insertion
$imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
// $imgData = addslashes($_FILES['userfile']);

// get the image info..
$size = getimagesize($_FILES['userfile']['tmp_name']);

// put the image in the db...
// database connection
mysql_connect("localhost", "myusername", "mypasswrd") OR DIE (mysql_error());

// select the db
mysql_select_db ("dbcontent") OR DIE ("Unable to select db".mysql_error());

// our sql query
$sql = "INSERT INTO images
( image_id , image_type ,image, image_size, image_name)
VALUES
('', '{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name']}')";

// insert the image
if(!mysql_query($sql)) {
echo 'Unable to upload file';
}
}
}
else {
// if the file is not less than the maximum allowed, print an error
echo
'<div>File exceeds the Maximum File limit</div>
<div>Maximum File limit is '.$maxsize.'</div>
<div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].' bytes</div>
<hr />';
}
}
?>

Daemonspyre
10-02-2007, 04:38 PM
This is a PHP issue, not a MySQL issue.

However, that being said, you have a hidden field MAX_FILE_SIZE, and a variable $maxsize, BUT -- you don't define $maxsize.

You are getting the error because $maxsize is empty, so the value will always be larger, even if the value is 0.

You need to make sure that you set $maxsize to something ( $maxsize = 10000000; or $mazsize = $_REQUEST["MAX_FILE_SIZE"]; ) so that you can do the comparison.

HTH!

Fumigator
10-02-2007, 04:41 PM
This is really a PHP question and will get moved to that forum soon I imagine.

But to give you a shove in the right direction, your script will kick out that "file exceeded size" message if the upload fails for any reason, not just if the file is too big. You need to track down the actual problem with the upload by checking $_FILES['userfile']['error'] for starters.

Also I don't see a call to the move_upload_file() function, so the file you upload is going to get lost in the ether if you don't move it to somewhere permanent.

See this page in the PHP manual (http://us.php.net/manual/en/features.file-upload.php) for more info.

(edit: Daemonspyre beat me)

greens85
10-02-2007, 04:54 PM
you have a hidden field MAX_FILE_SIZE, and a variable $maxsize, BUT -- you don't define $maxsize.

Doesnt this piece of code with the form define the maxsize:

<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />

CFMaBiSmAd
10-02-2007, 04:58 PM
The following test in the code will also be true if the size of the uploaded file exceeds the POST_MAX_SIZE setting -
if(!isset($_FILES['userfile'])) {
echo '<p>Please select a file</p>';
}

Daemonspyre
10-02-2007, 06:45 PM
Doesnt this piece of code with the form define the maxsize:

<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />

Why would it? They are named totally differently AND you are asking a web page to dynamically assign its own variables and do so only on posting of the form. Now, if your hidden field was named 'maxsize', that *may* be true, but not in its current state.

Take what Fumigator and CFMaBiSmAd said, as well as defining the $maxsize, and see what happens.

Your best bet is to start off simple -- once the form posts, echo all the variables that you want to check to make sure that they are getting their values set by the form.

i.e.

echo $_REQUEST['max_file_size'] . '<br />';
echo $_FILES['userfile']['error'] . '<br />';

etc...

greens85
10-03-2007, 04:20 PM
Hi again,

I have now got the image to upload, however i cant get the browser to display the image. I have used to following code in order to try and display the image.

<?php
//Connect to mySQL and Select The Database
$connection = mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db('dbcontent', $connection) or die(mysql_error());

$query5 = mysql_query("SELECT * FROM upload WHERE id=1");
while ($data1 = mysql_fetch_array($query5)){
echo $data1[2];
}
?>

However the browser is outputting the following message:

image/pjpeg

Pressumably this is just telling me what type of file resides in the database, doesnt anyone have any ideas why my code is causing this to happen

Thanks in advance

Fumigator
10-03-2007, 04:49 PM
Are you storing the image as a blob type? If you are, then you should know you can't just echo the column like any other text type column, you must either output the image in its own window using an image header i.e. header("Content-type: jpg"), or imbed the PHP script in an <img> tag i.e. <img src="getImage.php">.

Is there a compelling reason you are storing the images directly in the database? In most cases it is better to store the images in a directory and then just store the path to the file in the database.

greens85
10-04-2007, 09:17 AM
Yeah the image is stored directly in the databases as a blob type, there is no specific reason for this other than i am new to php and used a tutorial to learn what i needed to learn (storing images). This is the way the tutorial showed.

I'd be more than happy to change the way that the image is stored if you could point me in the right direction of a tutorial or even offer me some advice on here.

Thanks

greens85
10-04-2007, 12:49 PM
Thats has now been sorted out, however im now having trouble with resizing the image, does anyone know of any good tutorials for resizing???

Thanks