Writing BLOB to database with ODBC

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Player1005
Forum Newbie
Posts: 2
Joined: Fri Jan 06, 2006 6:12 pm

Writing BLOB to database with ODBC

Post by Player1005 »

twigletmac | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi,

i want to write a complete document (image, pdf, .doc, whatever) into a ACCESS DB over ODBC. 
I am using that code:

Code: Select all

<?php
// This is an example of config.php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'somedatabase';
// This is an example opendb.php
$conn = odbc_connect("mydb", "Sa", "") or die ('Error connecting to mysql');
//mysql_select_db($dbname);
?> 
<html>
<head>
<title>Upload File To Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?
if(isset($_POST['upload']))
{      $fileName = $_FILES['userfile']['name'];
        $tmpName  = $_FILES['userfile']['tmp_name'];
        $fileSize = $_FILES['userfile']['size'];
        $fileType = $_FILES['userfile']['type'];
        
        $fp = fopen($tmpName, 'r');
        $content = fread($fp, $fileSize);
        $content = addslashes($content);
        fclose($fp);
        
        if(!get_magic_quotes_gpc())
        {            $fileName = addslashes($fileName);        }
        
        print $fileName;
        print $fileSize;
        print $fileType;
        
        $query = "INSERT INTO file (filename, filesize, filetype, filecontent ) VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

        	odbc_longreadlen($queryexe , 2000000);
odbc_binmode($queryexe , ODBC_BINMODE_RETURN);
        $queryexe = odbc_exec($conn, $query) or die('Error, query failed');                    

        echo "<br>File $fileName uploaded<br>";
}        
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
  <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" class="box" id="userfile">
         </td>
      <td width="80"><input name="upload" type="submit" class="box" id="upload" value="  Upload  "></td>
    </tr>
  </table>
</form>
</body>
</html>
i got that code from here -> http://www.php-mysql-tutorial.com/php-mysql-upload.php
it was originally a code for uploading files to mysql and i changed it to ODBC-code.
Strange: the upload only works with very small TEXTfiles.
I tried out several things with odbc_longreadlen() and odbc_binmode() but nothing helped.

does somebody has an idea?

-> by the way: if somebody would a good and general tutorial how to handle/upload BLOB s would be great! Am php newbie.

Thx a lot for help!
PL


twigletmac | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Post Reply