A question about uploading an image to my database.
(same question as in other topic, but I need to finish it within 12 hours..)
I used the tutorial recommended above, but it seems as if I'm to plain dumb to get it to work, I followed the instructions, but it's not working, I get errors about these lines:
Warning: fread(): supplied argument is not a valid stream resource in /home/vhosting/21/sintpietersgent.be/www/leerlingenraad/seminarie/insert.php on line 26Code:$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
Warning: fclose(): supplied argument is not a valid stream resource in /home/vhosting/21/sintpietersgent.be/www/leerlingenraad/seminarie/insert.php on line 28
This is the entire code:
bestellingenCP.php
insert.php (the problem..)HTML Code:<table> <form enctype="multipart/form-data" action="insert.php" method="post" name="changer"> <input name="MAX_FILE_SIZE" value="102400" type="hidden"> <tr><td>Afbeelding:</td><td><input name="image" accept="image/jpeg" type="file"></td></tr> <tr><td>Naam:</td><td><input name="naam" type="text" /></td></tr> <tr><td>Prijs:</td><td><input name="prijs" type="text" /></td></tr> <tr><td>Omschrijving:</td><td><textarea name="omschrijving" ></textarea></td></tr> <tr><td></td><td><input value="Submit" type="submit"></td></tr> </form> </table>
Code:<?php
$host="localhost";
$name = "correct";
$pass = "correct";
$dbname = "correct";
$dbi = mysql_connect($host, $name,$pass) or
die("Kan niet verbinden met de database. Error :" . mysql_error());
mysql_select_db($dbname,$dbi);
if (isset($_FILES['image'])) {
$tmpName = $_FILES['image']['tmp_name'];
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
$naam = $_POST['naam'];
$prijs = $_POST['prijs'];
$omschrijving = $_POST['omschrijving'];
$query = "INSERT INTO bestellingen (naam,prijs,beschrijving,image) VALUES
('$naam','$prijs','$omschrijving','$data')";
$results = mysql_query($query, $dbi);
}
else {
print "No image selected/uploaded";
}
mysql_close($dbi);
?>
A note referring to my previous post:
I learned that an upploaded file ( $_FILES['userfile']['tmp_name'] ) has permission 600 and that I have to use chmod() to change permission.
Do you know where and how i would apply it?
Thank you in advance for your replies! I really appreciate it
Use this function PHP: chmod - Manual when you have uploaded this image.. just after move_uploaded_file() function..
Thanks a Lot buddy!
great tutorial, just what i needed
thanks for this tutorial.
thanks for the tutorial really nice hehe
um.. is it posible that pic_id can hold 2 or more picture at the same time?
table would be :
pic_id : int
pic_1: blob
pic_2: blob
pic_3: blob
....etc...
then if i call pic_id all pictures will appear? kinda like that hehe
or updating the pics?
just wondering hihihi.. is there a simple/short code for that? >.> like this tutorial?
thanks,
hardinera
Last edited by hardinera; 07-30-2009 at 01:44 AM.
Helllo Jordon,
I followed your instruction -
DB and Table
DATABASE Name : binary;
TAble Name : tbl_images (
> id tinyint(3) unsigned NOT NULL auto_increment,
> image blob NOT NULL,
> PRIMARY KEY (id)
> );
insert.phpHTML Code:<html> <head> </head> <body> <h1>Upload Your Image</h1> <form enctype="multipart/form-data" action="insert.php" method="post" name="changer"> <input name="MAX_FILE_SIZE" value="102400" type="hidden"> <input name="image" accept="image/jpeg" type="file"> <input value="Submit" type="submit"> </body> </html>
End of insert.phpCode:<html>
<head></head>
<body>
<?php
// Create MySQL login values and
// set them to your login information.
$username = "root";
$password = "1234";
$host = "localhost";
$database = "binary";
// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Select your database
mysql_select_db ($database);
// Make sure the user actually
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {
// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];
// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
// Create the query and insert
// into our database.
$query = "INSERT INTO tbl_images ";
$query .= "(image) VALUES ('$data')";
$results = mysql_query($query, $link);
// Print results
print "Thank you, your file has been uploaded.";
}
else {
print "No image selected/uploaded";
}
// Close our MySQL Link
mysql_close($link);
?>
</body>
</html>
view.php
end of view.phpCode:<html>
<head>
</head>
<body>
<?php
$username = "root";
$password = "1234";
$host = "localhost";
$database = "binary";
@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
@mysql_select_db($database) or die("Can not select the database: ".mysql_error());
$id = 1;
//$id = $_GET['id'];
if(!isset($id) || empty($id)){
die("Please select your image!");
}else{
$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['image'];
header('Content-type: image/jpg');
echo $content;
}
?>
</body>
</html>
Problem 1: I can add 1 image,even if I tried to insert another image, it shows successfull,but when I open the mysql talbe, I see only one record
Problem 2: view.php open my Dreamweaver and shows garbage data.
Can you please help me?
Last edited by Orjan; 07-31-2009 at 12:38 AM. Reason: Use code tags
if i replace insert with update
$query = "UPDATE members SET image='$data' WHERE id=1";
but nothing happens >.>
am i missing sumthing?
Last edited by hardinera; 07-30-2009 at 09:56 PM.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
There are currently 9 users browsing this thread. (0 members and 9 guests)
Bookmarks