RE: [PHP-WIN] Inserting images into a blob thro' a PHP Script............

From: Date: Tue, 08 Aug 2000 06:45:24 +0000
Subject: RE: [PHP-WIN] Inserting images into a blob thro' a PHP Script............
References: 1  Groups: php.windows 
Request: Send a blank email to php-windows+get-1620@lists.php.net to get a copy of this message
Some comments (which may or may not help): 1. It is generally good programming style to put things in functions. 2. I use application/x-www-form-urlencoded as the enctype instead of multipart/form-data as this eliminates the need to upload the file to the temp directory (problem 1). 3. I also base64 encode the incoming bytestream before writing it to the db - so no need to use addslashes (problem 2). I have a sample script that displays an input form for uploading (JPG, GIF & PNG) images. I can send this to you separately. (Coz I don't have it here at work). You will, of course, have to tweak this and add the db stuff for your own requirements, but the basic stuff you need is there. Regards, Ignatius > -----Original Message----- > From: Vinod [mailto:vinod@indokorea.net] > Sent: Tuesday, 8 August 2000 16:11 > To: php-windows@lists.php.net > Subject: [PHP-WIN] Inserting images into a blob thro' a PHP > Script............ > > > Hi evereybody, > > We were trying to upload and insert images into a mysql db > thro' a PHP Script.We are facing the following problems: > > 1)The file is not getting uploaded or moved to the temp dir. > > 2)When we place the image in the temp dir. manually and > insert it, the image gets stored in the db.But while > retrirving it we just get raw data.When we inspect the MySql > db we just find similar records as below: > > id filename filesize filetype bin_data > -- -------- -------- -------- -------- > > 25 39.jpg 78086 image/pjpeg ÿØÿà > > > We are at loss to find out where we are wrong though we have > spent hell a lot of time.Please respond with guidelines to > get this thing working. > > GIVEN BELOW IS OUR CODE: > > > ===================================================== > <HTML> > <HEAD><TITLE>Store binary data into SQL Database</TITLE></HEAD> > <? > > if ($id) { > $conn_id = mysql_connect ( 'localhost', 'root', > 'admin'); > mysql_select_db ( 'test'); > > $query = "select * from binary_data where id = $id"; > $res_id = mysql_query ($query, $conn_id); > > if (! $res_id) { > printf ( "MySQL Error: (%d) %s<br><br>%s\n", > mysql_errno (), > mysql_error (), > $query); > mysql_close ($conn_id); > exit (); > } > > $row = mysql_fetch_array ($res_id); > > if ($row) { > $data = $row[ "bin_data"]; > // echo "data from table = $data"; > $type = $row[ "filetype"]; > > $filename = $row[ "filename"]; > > $query = "update binary_data set hits = hits + 1 > where id = $id"; > $res_id = mysql_query ($query, $conn_id); > > if (! $res_id) { > printf ( "MySQL Error: (%d) %s<br><br>%s\n", > mysql_errno (), > mysql_error (), > $query); > mysql_close ($conn_id); > exit (); > } > > mysql_close ($conn_id); > > // Header ( "Content-disposition: filename=".$filename); > > // Header ( "Content-type: $type"); > > $fp1 = fopen ('c:\uptmp\tmp.jpg', 'w'); > $fint = fwrite($fp1,$data); > fclose ($fp1); > echo "<br><img src='c:\\uptmp\\tmp.jpg'>"; > $data = stripslashes($data); > // echo $data; > // echo "<br><h3>The Image</h3><br><img > src='$data'>"; > exit (); > } > > else { > printf ( "There were no rows found with id = %d\n", $id); > mysql_close ($conn_id); > } > } > > printf ( "<html>\n"); > printf ( "<head>\n"); > printf ( "<title>File Upload</title>\n"); > printf ( "</head>\n"); > printf ( "<body bgcolor=\"#FFFFFF\">\n"); > > printf ( "<p>\n"); > printf ( "This is my test page for sending files to and > getting files from a mysql database with php3.\n"); > printf ( "</p>\n"); > > printf ( "<p>\n"); > printf ( "<a href=\"fileman.phps\">Click Here</a> to view > the source.\n"); > printf ( "</p>\n"); > > if ($submit == 'Upload File') { > $write_dir = "/uptmp/"; > exec ( "mv ".$input_file. " > '".$write_dir.$input_file_name. "'"); > > printf ( "<p>\n"); > printf ( "Your file %s was successfully > uploaded.<br>\n", $input_file_name); > > $file_name = $write_dir.$input_file_name; > printf ( "Your file with path = %s <br>\n", $file_name); > $file_size = filesize ($file_name); > $fp = fopen ($file_name, 'r'); > $data = addslashes(fread ($fp, $file_size)); > // $data = stripslashes($data); > // echo $data; > fclose ($fp); > > mysql_connect ( 'localhost', 'root', 'admin'); > mysql_select_db ( 'test'); > > $res_id = mysql_query ( "insert into binary_data values ( > 0, > '$description', > '$data', > '$input_file_name', > '$input_file_size', > '$input_file_type', > '0' > )"); > > $new_id = mysql_insert_id (); > > mysql_close (); > > if ($new_id > 0) { > printf ( "Database id for your file: %s<br>\n", $new_id); > } > else { > printf ( "Your file was not successfully inserted!\n"); > } > > printf ( "</p>\n"); > } > > printf ( "<p>\n"); > printf ( "*** Send File ***\n"); > printf ( "</p>\n"); > > printf ( "<p>\n"); > printf ( "<form action=\"fp11.php3\" method=\"POST\" > enctype=\"multipart/form-data\">\n"); > printf ( "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" > value=\"10485760\">\n"); > printf ( "File description: &nbsp; \n"); > printf ( "<input type=\"text\" > name=\"description\"><br>\n"); > printf ( "File name: &nbsp; \n"); > printf ( "<input type=\"file\" > name=\"input_file\"><br>\n"); > printf ( "<input type=\"submit\" name=\"submit\" > value=\"Upload File\">\n"); > printf ( "</form>\n"); > printf ( "<p>\n"); > > printf ( "<p>\n"); > printf ( "*** Retrieve File ***\n"); > printf ( "</p>\n"); > > printf ( "<p>\n"); > printf ( "<form action=\"fp11.php3\" > method=\"POST\">\n"); > printf ( "Database Id: &nbsp; \n"); > printf ( "<input type=\"text\" name=\"id\" > value=\"$new_id\"><br>\n"); > printf ( "<input type=\"submit\" value=\"Retrieve > File\">\n"); > printf ( "</form>\n"); > printf ( "<p>\n"); > > printf ( "</body>\n"); > printf ( "</html>\n"); > ?> > ============================================================== > > > P.S.:We are using PHP3 WITH MYSQL 3.22 ON APACHE WEBSERVER 1.3 > > > CAN ANYONE HELP US WITH THIS CSRIPT OR GIVE US SOME NEW IDEAS > AS TO HOW TO UPLOAD IMAGES ? > > Thanks and best regards, > Vinod >

« previous php.windows (#1620) next »