/    Sign up×
Community /Pin to ProfileBookmark

uploading an image to a mysql database

i am a newbie to both mysql and php… i am trying to create an online quiz in which the questions and choices will be coming from a mysql database… i was able to do that… however, i also wanna upload images to my database and use them for the questions and choices…

i’ve tried following the instructions in creating a file uploader from this website ([url]http://www.php-mysql-tutorial.com/php-mysql-upload.php)[/url]… but as soon as i click the upload button, i am getting this message…

[CODE]Warning: filesize() [function.filesize]: stat failed for C:WINDOWSTEMPphp13.tmp in c:Inetpubwwwroottry.php on line 10

Warning: fread() [function.fread]: Length parameter must be greater than 0. in c:Inetpubwwwroottry.php on line 10

File Blue hills.jpg uploaded[/CODE]

then i checked the database and i was able to see information about the file i uploaded (name, size, etc…) except for the image or the content itself… and also… when i followed the exact thing about displaying the image through a link (still from the same website), it just gave me “the page cannot be displayed”… i’m not sure if it has something to do with my configuration… could somebody please enlighten me with these things… i really, really, really need your help… please… please… please…

? ? ? ?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@chazzyJan 05.2006 — it really really really helps please please please to post your code thanks.

also please post your table structure.
Copy linkTweet thisAlerts:
@ShrineDesignsJan 05.2006 — databases are for data, file systems are for files[/quote]you should store the file path or file name in the database not the file itself
Copy linkTweet thisAlerts:
@chazzyJan 05.2006 — you should store the file path or file name in the database not the file itself[/QUOTE]

Just because it's not the best solution doesn't mean that we really really really shouldn't tell him how to do it.
Copy linkTweet thisAlerts:
@am_a_newbieauthorJan 06.2006 — as i have said i followed the code from the website http://www.php-mysql-tutorial.com/php-mysql-upload.php ... anyway, the table structure is like this...

CREATE TABLE upload (

id INT NOT NULL AUTO_INCREMENT,

name VARCHAR(30) NOT NULL,

type VARCHAR(30) NOT NULL,

size INT NOT NULL,

content MEDIUMBLOB NOT NULL,

PRIMARY KEY(id)

);[/QUOTE]


the code to upload a file:

[code=php]
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fileError = $_FILES['userfile']['error'];

$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
mysql_connect("localhost","username","password") or die("Couldn't connect to server");
mysql_select_db("database") or die("Couldn't connect to database");
$query = "INSERT INTO table (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($query) or die('Error, query failed');

echo "<br>File $fileName uploaded<br>$fileError";
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</body>
</html>
[/code]


the code to view the image uploaded:

[code=php]
<?
if(isset($_GET['id']))
{
mysql_connect("localhost","username","password") or die("Couldn't connect to server.");
mysql_select_db("database") or die("Couldn't connect to database.");

$id = $_GET['id'];
$query = "SELECT name, type, size, content FROM table WHERE id = '$id'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);

header("Content-Disposition: attachment; filename=$name");
header("Content-length: $size");
header("Content-type: $type");
echo $content;

exit;
}

?>
<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
mysql_connect("localhost","username","password") or die("Couldn't connect to server.");
mysql_select_db("database") or die("Couldn't connect to database.");
$query = "SELECT id, name FROM table";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
}

else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="download.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?

}
}
?>
</body>
</html>
[/code]


hope you could help me... please... please... please...

? ? ? ?
×

Success!

Help @am_a_newbie spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...