CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Jan 2006
    Posts
    13

    Writing BLOB with ODBC and PHP to Access

    Hi,

    i want to write into a ACCESS DB over ODBC.
    I am using that code:

    <?php
    $conn = odbc_connect("mydb", "Sa", "") or die ('Error connecting to mysql');
    ?>
    <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.
    I want to keep it as general as possible - also if performance decreases - because i want to have it general and migrate so mysql later.
    Strange: the upload only works with very small files.
    I tried out several things with odbc_longreadlen() and odbc_binmode() but nothing helped.

    does somebody has an idea?

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

    Thx for help!
    PL

  2. #2
    Join Date
    Nov 2004
    Location
    Slough, UK
    Posts
    184

    Re: Writing BLOB with ODBC and PHP to Access

    Does your code actually work? If not, what is the problem?

    If all the BLOB data has not been saved you may want to check that you ave set up the correct ODBC driver for Access and that the field you are saving the data to is of the correct size.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || ClickOnline ||

    Did I ever say I was an expert?

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Please mark threads resolved by going to the thread tools menu and clicking the Mark Thread Resolved button.

    Has a post really helped you? Please Rate it.

  3. #3
    Join Date
    Jan 2006
    Posts
    13

    Re: Writing BLOB with ODBC and PHP to Access

    nope... doesn't work.
    when i try to upload a word document with 20kB i get this error:
    Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] Syntaxfehler in Abfrageausdruck ''ÐÏ*¡±á\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0>\0\0þÿ \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0#\0\0\0\0\0\0\0\0
    \0\0%\0\0\0\0\0\0þÿÿÿ\0\0\0\0\"\0\0\0ÿÿÿÿÿÿÿÿÿ
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
    ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ'., SQL state 37000 in SQLExecDirect in C:\Programme\XAMPP\htdocs\uploadtest.php on line 64
    Error, query failed

    Line 64 is this one: $queryexe = odbc_exec($conn, $query) or die('Error, query failed');

    i am using the standard english access driver 4.X
    Last edited by Player1005; January 21st, 2006 at 12:11 PM.

  4. #4
    Join Date
    Nov 2004
    Location
    Slough, UK
    Posts
    184

    Re: Writing BLOB with ODBC and PHP to Access

    Are you escaping the content in the word document?

    In access, you need to replace an double quotes " with ""
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || ClickOnline ||

    Did I ever say I was an expert?

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Please mark threads resolved by going to the thread tools menu and clicking the Mark Thread Resolved button.

    Has a post really helped you? Please Rate it.

  5. #5
    Join Date
    Jan 2006
    Posts
    13

    Re: Writing BLOB with ODBC and PHP to Access

    yes, you are right!
    I changed that to
    PHP Code:
            $content addslashes($content);
            
    $content str_replace("'""''"$content); 
    Now it looks as if the upload would work, but am not able to show the file...
    (picture)
    Last edited by Player1005; January 22nd, 2006 at 12:43 PM.

  6. #6
    Join Date
    Nov 2004
    Location
    Slough, UK
    Posts
    184

    Re: Writing BLOB with ODBC and PHP to Access

    You don't need addslashes() for access, in fact you must not use it, it will corrupt the word document.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || ClickOnline ||

    Did I ever say I was an expert?

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Please mark threads resolved by going to the thread tools menu and clicking the Mark Thread Resolved button.

    Has a post really helped you? Please Rate it.

  7. #7
    Join Date
    Jan 2006
    Posts
    13

    Re: Writing BLOB with ODBC and PHP to Access

    I am getting an error when I dont use addslashes().

    Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] Syntaxfehler in Zeichenfolge in Abfrageausdruck ''GIF89aÔ'., SQL state 37000 in SQLExecDirect in C:\Programme\XAMPP\htdocs\uploadtest.php on line 65

    (tried to upload a *.gif this time)

  8. #8
    Join Date
    Nov 2004
    Location
    Slough, UK
    Posts
    184

    Re: Writing BLOB with ODBC and PHP to Access

    My mistake, I see you are using single quotes in your SQL for string literals. Try it with addslashes as the ODBC functiosn for PHP may be smart enough to trnaslate them into the escape sequence for Access. If it does not do this, you will need to str_replace all single quotes ' with two consecutive single quotes ''.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || ClickOnline ||

    Did I ever say I was an expert?

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Please mark threads resolved by going to the thread tools menu and clicking the Mark Thread Resolved button.

    Has a post really helped you? Please Rate it.

  9. #9
    Join Date
    Jan 2006
    Posts
    13

    Re: Writing BLOB with ODBC and PHP to Access

    well... with str_replace AND addslashes it looks as if the upload would work.

    but it is not possilbe to download the set again...

  10. #10
    Join Date
    Nov 2004
    Location
    Slough, UK
    Posts
    184

    Re: Writing BLOB with ODBC and PHP to Access

    Its not possible? What do you mean?

    Does a content filter block the download? Is it against the law? Or does the download link keep moving around the page when you try to click it?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || ClickOnline ||

    Did I ever say I was an expert?

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Please mark threads resolved by going to the thread tools menu and clicking the Mark Thread Resolved button.

    Has a post really helped you? Please Rate it.

  11. #11
    Join Date
    Jan 2006
    Posts
    13

    Re: Writing BLOB with ODBC and PHP to Access

    I download it and save it to my desktop. when i try to open it i am getting "can't read file header" (a gif picture..)

  12. #12
    Join Date
    Nov 2004
    Location
    Slough, UK
    Posts
    184

    Re: Writing BLOB with ODBC and PHP to Access

    Try using add slashes after str_replace.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || ClickOnline ||

    Did I ever say I was an expert?

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Please mark threads resolved by going to the thread tools menu and clicking the Mark Thread Resolved button.

    Has a post really helped you? Please Rate it.

  13. #13
    Join Date
    Jan 2006
    Posts
    13

    Re: Writing BLOB with ODBC and PHP to Access

    Then I am getting a datatype mismatch error:
    Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC Microsoft Access Driver] Datentypen in Kriterienausdruck unverträglich., SQL state 22005 in SQLExecDirect in C:\Programme\XAMPP\htdocs\uploadtest.php on line 65
    Error, query failed

  14. #14
    Join Date
    Nov 2004
    Location
    Slough, UK
    Posts
    184

    Re: Writing BLOB with ODBC and PHP to Access

    Forget the upload for a second and try doing some tests with the content. Try the following strings and see what you get:

    $content = "'hello''hello''hello'";
    $content = '"hello"';
    $content = addslashes("'hello''hello''hello'");
    $content= addslashes('"hello"');

    Which ones fail?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || ClickOnline ||

    Did I ever say I was an expert?

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Please mark threads resolved by going to the thread tools menu and clicking the Mark Thread Resolved button.

    Has a post really helped you? Please Rate it.

  15. #15
    Join Date
    Jan 2006
    Posts
    13

    Re: Writing BLOB with ODBC and PHP to Access

    is see.. this is step by step now :-)

    the response is (after print $content)

    'hello''hello''hello'
    "hello"
    \'hello\'\'hello\'\'hello\'
    \"hello\"

    (no error messages)

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured