View Full Version : php/mysql BlOb
jazzman121
September 4th, 2003, 07:19 PM
ok... here is my scripts dont know wats wrong it aint working maybe u guys can figure out wats wrong
I want to upload images (.jpg's) to the database... ive defined the table with a coloum file as blob
here is the code for the form to update to the database,,...
<form method="post" action="form.php?action=ft" enctype="multipart/form-data">
<input type="file" name="userfile[]">
<input type="file" name="userfile[]">
<input type="submit" name="ftsubmit" value="Submit">
</form>
if($ftsubmit){
//update the image
$path = "/ft_files/";
$attach = $HTTP_POST_FILES['userfile'];
if($attach['name'][0] != "none" && !empty($attach['name'][0]) && is_uploaded_file($attach['tmp_name'][0])) {
$db->query("UPDATE $table_ft SET file='$attach[0]' WHERE id='0'");
}
if($attach['name'][1] != "none" && !empty($attach['name'][1]) && is_uploaded_file($attach['tmp_name'][1])) {
$db->query("UPDATE $table_ft SET file='$attach[1]' WHERE id='1'");
}
}
and here is the code to show the image on the page..(view image)
$count = 0;
$query = $db->query("SELECT * FROM $table_ft ORDER BY id");
while($ftlinks = $db->fetch_array($query)) {
$count++;
$ftimage[$count] = $ftlinks[file];
}
echo "<img src=$ftimage[0]>"
echo "<img src=$ftimage[1]>"
i am not sure if the image is uploading to the db or not... but before the fields were NULL when i created it after uploading it doesnt show anything in the command prompt...( so im assumiing some data is been written into the database )
please helpp
Syntax
September 5th, 2003, 02:55 AM
ok. try with only one file.
Form page:
<form method="post" action="insert.php" name="form1" enctype=multipart/form-data>
<input type=\"file\" name="binary_file" size="40">
<input type="submit" name=submit value="Submit"></form>
Insert file in the db with this script:
$self = isset($_SERVER) ? $_SERVER["PHP_SELF"] : $HTTP_SERVER_VARS["PHP_SELF"];
@mysql_connect("$host", "$user", "$password") or die("Connection failed!");
@mysql_select_db($dbname);
$data = addslashes(fread(fopen($_FILES["binary_file"]["tmp_name"], "rb"), $_FILES["binary_file"]["size"]));
$result = @mysql_query("INSERT INTO files (Binary, Name, Size, Type)
VALUES ('$data','" . $_FILES["binary_file"]["name"] . "',
'" . $_FILES["binary_file"]["size"] . "','" . $_FILES["binary_file"]["type"] . "')")
or die("Insert failed !");
echo "File " . basename($_FILES["binary_file"]["name"]) . "uploaded.";
@mysql_close();
SQL file
CREATE TABLE files(
Id int(4) NOT NULL auto_increment,
Binary mediumblob NOT NULL,
Name varchar(50) NOT NULL default '',
Size int(10) NOT NULL default '0',
Type varchar(50) NOT NULL default '',
PRIMARY KEY (Id)
)
i think it would work.
jazzman121
September 5th, 2003, 03:12 AM
hwo do i do multiple uploads?
Syntax
September 5th, 2003, 03:23 AM
the same way. remember to rename the
<input type="file\" name="binary_file" size="40">
for example
//first file
<input type="file\" name="binary1" size="40">
//second file
<input type="file\" name="binary2" size="40">
and modify the php script with the new input file names.
jazzman121
September 5th, 2003, 10:18 AM
now say i have a .jpg file in the blob how do i show it in the page?? like how do i display the image? on a webpage?
Syntax
September 5th, 2003, 02:58 PM
you pass the script the id of the image in the db ad the script will work [i hope :) ]
if(!isset($_GET)) $_GET = $HTTP_GET_VARS;
@mysql_connect("$host", "$user", "$password");
@mysql_select_db($dbname);
$query = "SELECT Binary, Type FROM files WHERE Id = '" . $_GET["Id"] . "'";
$select = @mysql_query($query);
$result = @mysql_fetch_array($select);
$data = $result["Binary"];
$type = $result["Type"];
Header("Content-type: $type");
echo $data;
@mysql_close();
jazzman121
October 8th, 2003, 09:01 PM
ok its not working when im tryin to display the image its showing it in binary format a whole bunch of text... can someone show me a working example or something whose code i can analyse
Marz
October 8th, 2003, 09:53 PM
This is why I hate using mySQL.. Oig... Basically.. You wnat to access an image that you uploaded.. Well the place where you uploaded the file is a temporary directory placed on the server.. if you wish to access the file in a specific spot.. You must move that file after it is uploaded..
This is a script that I use to handle files incoming.. Reminder.. This is 9 files and I'm giving them names instead of using the ones thye uploaded with..
uploaddir = 'D:/webspace/sales/gordonauto/carpeople.us/www/iam/photos/';
$uploadfile1 = sprintf("%s%s%s%s", $uploaddir, "picture", $tempNum, ".jpg"); $tempNum++;
$uploadfile2 = sprintf("%s%s%s%s", $uploaddir, "picture", $tempNum, ".jpg"); $tempNum++;
$uploadfile3 = sprintf("%s%s%s%s", $uploaddir, "picture", $tempNum, ".jpg"); $tempNum++;
$uploadfile4 = sprintf("%s%s%s%s", $uploaddir, "picture", $tempNum, ".jpg"); $tempNum++;
$uploadfile5 = sprintf("%s%s%s%s", $uploaddir, "picture", $tempNum, ".jpg"); $tempNum++;
$uploadfile6 = sprintf("%s%s%s%s", $uploaddir, "picture", $tempNum, ".jpg"); $tempNum++;
$uploadfile7 = sprintf("%s%s%s%s", $uploaddir, "picture", $tempNum, ".jpg"); $tempNum++;
$uploadfile8 = sprintf("%s%s%s%s", $uploaddir, "picture", $tempNum, ".jpg"); $tempNum++;
$uploadfile9 = sprintf("%s%s%s%s", $uploaddir, "picture", $tempNum, ".jpg");
$test[1] = move_uploaded_file($_FILES['picture1']['tmp_name'], $uploadfile1);
$test[2] = move_uploaded_file($_FILES['picture2']['tmp_name'], $uploadfile2);
$test[3] = move_uploaded_file($_FILES['picture3']['tmp_name'], $uploadfile3);
$test[4] = move_uploaded_file($_FILES['picture4']['tmp_name'], $uploadfile4);
$test[5] = move_uploaded_file($_FILES['picture5']['tmp_name'], $uploadfile5);
$test[6] = move_uploaded_file($_FILES['picture6']['tmp_name'], $uploadfile6);
$test[7] = move_uploaded_file($_FILES['picture7']['tmp_name'], $uploadfile7);
$test[8] = move_uploaded_file($_FILES['picture8']['tmp_name'], $uploadfile8);
$test[9] = move_uploaded_file($_FILES['picture9']['tmp_name'], $uploadfile9);
Hope that helps out some.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.