my script is uploading the image file in database but it's no show the message
"Your files is successfully store in database or other Messages". Help Please.Thanks.
Code:
<?php
if ($_POST['Submit']) {
if ($_POST['MAX_FILE_SIZE'] >= $_FILES['file']['size']) {
//print_r($_FILES);
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test");
$photo = addslashes(fread(fopen($_FILES['file']['tmp_name'], "r"), $_FILES['file']['size']));
$query = sprintf("INSERT INTO image(Image, FileType) VALUES('%s', '%s')", $photo, $_FILES['file']['type']);
if (mysql_query($query)) {
$messages[] = "Your files is successfully store in database";
} else {
$messages[]= mysql_error();
}
}
else {
$messages[]="The file is bigger than the allowed size please resize";
}
}
?>
<html>
<head>
<title>Add Image</title>
</head>
<body>
<?
if (isset($messages)) {
foreach ($messages as $message) {
print $message. "<br>" ;
}
}
?>
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="hidden" name="MAX_FILE_SIZE" value="96000">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
I don't see anything immediately wrong with your code. The only thing I can think of is perhaps it is only returning one message so the foreach is pointless. You could try doing a quick check to see if the $messages var is an array or just a single value. If it's an array then foreach them and print them out but, if it's just a single value then skip the foreach and just print it. That's more of a tip than anything I guess. Not sure if it will solve your problem. Doing it that way would make your code more efficient though. Then PHP doesn't have to call up all the foreach stuff for a single value (which is overkill).
One thing you should check is if your code is spitting out any PHP errors. If you have errors disabled on your server then I'm not sure what you have to do to see the errors. In a log somewhere I would assume. But, if you can find any errors pertaining to your code, that might help us if you post it.
Could it be the short "<?" tag instead of the "<?php" tag? (The latter always works, the former only if short_open_tags is enabled in your PHP config.)
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Thanks for your reply. I managed to correct the error. I'm trying to create the script to be able to view the images after been loaded in database.The same concept when we posted how messages in forum. Do you've some guidance? Thank in advance.
Bookmarks