I have been reading the article by Florian Dittmer.

http://www.phpbuilder.net/columns/florian19991014.php3

I have used the mod that Patrick Kebekus made for it to be used for php4.32.

http://www.phpbuilder.net/annotate/message.php3?id=1017206

It works fine and loads into mysql. Except that nothing gets inserted into the "description" column of the database, which is a minor problem.

My biggest problem is that the getdata.php3 scripts in Florians article does not seem to work. I am assuming that it is because it is not modified to be used with php 4.

When I try to view the image in the mysql database by making an html to view the image source, I do not get the image that is in mysql. I get nothing except for the standard image error - red[x] image.

Can anyone tell me what is wrong with the getdata script, and why it will not show the image from mysql?

Also I would like to know why there is no info. inserted into the "description" column in mysql when the file is submitted?

Here is my php 4 modified store.php script:

<?php 

// store.php - by Florian Dittmer <dittmer@gmx.net> 
// moded for php4 by [email]Patrick.MKC@gmx.de[/email] 
// Example php script to demonstrate the storing of binary files into 
// an sql database. More information can be found at [url]http://www.phpbuilder.com/[/url] 
?> 

<HTML> 
<HEAD><TITLE>Store binary data into SQL Database</TITLE></HEAD> 
<BODY> 

<?php 
// code that will be executed if the form has been submitted: 

if (isset($_REQUEST['submit'])) { 

// connect to the database 
// (you may have to adjust the hostname,username or password) 

MYSQL_CONNECT("localhost","root","mshp"); 
mysql_select_db("MSHP"); 

$data = addslashes(fread(fopen($_FILES['form_data']['tmp_name'], "r"), $_FILES['form_data']['size'])); 

$result=MYSQL_QUERY("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ". 
"VALUES ('".$form_description."','".$data."','".$_FILES['form_data']['name']."','".$_FILES['form_data']['size']."','".$_FILES['form_data']['type']."')"); 

$id= mysql_insert_id(); 
print "<p>This file has the following Database ID: <b>$id</b>"; 

MYSQL_CLOSE(); 

} else { 

// else show the form to submit new data: 
?> 

<form method="post" action="store.php" enctype="multipart/form-data"> 
File Description:<br> 
<input type="text" name="form_description" size="40"> 
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000"> 
<br>File to upload/store in database:<br> 
<input type="file" name="form_data"> 
<p><input type="submit" name="submit" value="submit"> 
</form> 

<?php 

} 

?> 

</BODY> 
</HTML> 

Here is what I have for the getdata.php3 script

<?php 

// getdata.php3 - by Florian Dittmer <dittmer@gmx.net> 
// Example php script to demonstrate the direct passing of binary data 
// to the user. More infos at [url]http://www.phpbuilder.com[/url] 
// Syntax: getdata.php3?id=<id> 

if($id) { 

// you may have to modify login information for your database server: 
@MYSQL_CONNECT("localhost","root","mshp"); 

@mysql_select_db("MSHP"); 

$query = "select bin_data,filetype from binary_data where id=$id"; 
$result = @MYSQL_QUERY($query); 

$data = @MYSQL_RESULT($result,0,"bin_data"); 
$type = @MYSQL_RESULT($result,0,"filetype"); 

Header( "Content-type: $type"); 
echo $data; 

}; 
?> 

And here is my HTML page to test the image in the mysql database:

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=windows-1250">
    <meta name="generator" content="PSPad editor, [url]www.pspad.com[/url]">
    <title>Untitled</title>
  </head>
  <body>
  <img src="getdata.php3?id=3">

  </body>
</html>

Any help would be greatly appreciated.
Thanks!

    I still have not figured out the problems.

    Anyone else know what can be wrong?

      do you have register globals on or off on the server? quick way to find out is put a script called info.php on the server and in it put <?php phpinfo(); ?>. call it up from the web and look for register_globals line and see if its on or off.

      if it is off, you will have to change

      $result=MYSQL_QUERY("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".

      "VALUES ('".$form_description."','".$data."','".$FILES['form_data']['name']."','".$FILES['form_data']['size']."','".$_FILES['form_data']['type']."')");

      to

      $result=MYSQL_QUERY("INSERT INTO binary_data (description,bin_data,filename,filesize,filetype) ".

      "VALUES ('".$REQUEST['form_description']."','".$data."','".$FILES['form_data']['name']."','".$FILES['form_data']['size']."','".$FILES['form_data']['type']."')");

        Register_Globals is On.

        Storing the image in the database is fine. I can run the store.php and upload the image. I can then view it by using MySQL Front in the database.

        My problem is when I am trying to view the image via the html page <img src=>.

        So I know it goes into the database, but for some reason it is not coming back out.

        I am using Win2k, Apache, PHP4, and MySQL.

          I am not sure though but isnt it like this:
          You already sent headers to the borwser from your HTML page and how come u use header() in getdata.php again?

            Not sure why the header is sent again. I was just doing what the tutorial said.

            I took out the header part in getdata.php and it still does not work.

            I still do not get an image. At this point I am lost at what to try or not try. 😕

            Has anyone tried taking my code to see if it will work on thier machines?

              Try getting rid of the '@' symbols. They hide error messages. You shouldn't really use them unless you have things already working the way you want. You'll want to see any error messages that it gives you.

              Also, instead of going to your html test page, try going directly to:

              http://yousite.com/getdata.php?id=3

              and see if you get any error messages back.

                I took out the @ symbols and went directly to the page and there were no errors.

                It gave all the hex code for the pic.

                Such as:

                ?JFIF???LEAD Technologies Inc. V1.01?(#(#!#.+(0=fB=88=}YJf??????????&#45539;?&#1968;??&#831;&#990;&#56235;&#57343;??&#49151;?5=xBBx?????????? }!1AQa"q2???#B??R&#1136;$3br? %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz???????????????????????????????????&#131;&#261;&#391;&#521;&#658;&#1236;&#1366;&#1496;&#1626;&#6307;&#18790;&#31273;&#44146;&#56211;&#56694;?w!1AQaq"2?B????#3R&#55318;&#56498;&#1098;$4&#6513;&'(

                  If you are getting the image displayed as text, it is because it isn't sending the Content-type. Put that command:

                  Header( "Content-type: $type");

                  back in your getdata.php3 file. Remember, you are calling this as part of the header for the page containing the picture. You can't have anything printing out on the getdata.php3 page at all. Don't put any 'echo' 'print' or any thing before that header function.

                  Also, in the getdata.php3 file, make sure you don't have any whitespace, carriage returns, tabs or anything else before the:

                  <?php

                  Anything you send from that page will force headers to be sent other than those you want, and you can't send headers twice.

                    Ok, gave that a try and it said the header is already sent.

                    Here is my code:
                    getdata.php

                    <?php
                    if($id) {
                    
                    // you may have to modify login information for your database server:
                    MYSQL_CONNECT("localhost","root","mshp");
                    
                    mysql_select_db("MSHP");
                    
                    $query = "select bin_data,filetype from binary_data where id=$id";
                    $result = MYSQL_QUERY($query);
                    
                    $data = MYSQL_RESULT($result,0,"bin_data");
                    $type = MYSQL_RESULT($result,0,"filetype");
                    Header( "Content-type: $type"); 
                    
                    echo $data;
                    
                    };
                    ?>
                    

                    Here is my HTML page to view data:

                    <html>
                      <head>
                        <meta http-equiv="content-type" content="text/html; charset=windows-1250">
                        <meta name="generator" content="PSPad editor, [url]www.pspad.com[/url]">
                        <title>Untitled</title>
                      </head>
                      <body>
                      <img src="getdata.php?id=1"> 
                    
                      </body>
                    </html>
                    

                    Here is my result:

                    Warning: Cannot modify header information - headers already sent by (output started at c:\inetpub\mshpar\test\layers\getdata.php:1) in c:\inetpub\mshpar\test\layers\getdata.php on line 14
                    ?JFIF???LEAD Technologies Inc. V1.01?(#(#!#.+(0=fB=88=}YJf??????????‡‹?&#1968;??&#831;&#990;&#1028095;??—¿?5=xBBx?????????? }!1AQa"q2???#B??R&#1136;$3br? %&'(

                      did you ever figure out how to get this to work?? I am having the same problem...

                        You're sending something to the browser before sending the header. If you understand how http headers work (which every good web developer should) then you'll know that this obviously won't work.

                        If you send ANYTHING before the header function it will die, i.e. whitespace, carriage returns... anything. Once you start sending the browser any data you cannot send it a header.

                        Save your getdata.php file with a .txt extension and give us a link so we can see exactly what you source file looks like (change any sensitive information of course, i.e. db passwords and such... oh wait, just checked your other posts and it looks like you've given everyone in the world your root password to your db... I hope it only accepts connections from localhost).

                          That is not my password for the db. I just repeated the abbreviation of my database in the db and password.

                          I have tried to take all the whitespace and carriage returns out and do not see anything that could cause it to die.

                          here is the .txt file.

                            I've taken your code, and created a table with the same fields you have. I ran it, and it works.

                            My table looks like:

                            id -> autoincrement & index
                            description -> varchar(100)
                            bin_data -> blob
                            filename -> varchar(100)
                            filesize -> mediumint
                            filetype ->varchar(30)

                            The only things I changed in the getdata.php and store.php files were the username, password, database and tablename. I didn't create a db just for this, just made a table in an existing db. Shouldn't affect anything.

                            I'm sorry, but I can't find anything wrong with the code, at least nothing that should cause this not to work.

                            Did you every get the description to be stored in the database? Using your code, it stores it ok for me.

                            Chris

                              Yeah I got it to post the description.

                              What components are you using to run it?

                              I am using Win2K, Apache, PHP4, MySql 3.23.430-nt, and IE6.

                              Maybe it is a compatibility issue on my end. It is really getting confusing of why it will not show the image in the database.

                                Linux, Apache, PHP4, MySQL 3.23, IE6

                                Since I posted, I tried a few more pictures. Out of the 6 that I tried, 1 didn't work. It only showed a broken image icon. Don't know why, pictures were all from the same source, same type, and uploaded the same - all pictures displayed fine ftp'ing them to the server.

                                Most places I've read discourage storing images in the database. I've never done it. I've stored, paths, alt text, filenames, etc, but never the image. I think it is probably slower, and more overhead for the server.... and with my very unreliable and unscientific test - 5/6 isn't acceptable to me.

                                Just my $.02

                                Chris

                                  Write a Reply...