Writing BLOB with ODBC and PHP to Access
Hi,
i want to write into a ACCESS DB over ODBC.
I am using that code:
Quote:
<?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
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.
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
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 ""
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)
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.
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)
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 ''.
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...
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?
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..)
Re: Writing BLOB with ODBC and PHP to Access
Try using add slashes after str_replace.
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
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?
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)