Im having real problems with uploading files to Ms SQL server database and retrieving them uncorrupted. Small files of any file type work fine but as soon an i approach sizes of 100k or more they start to corrupt or truncate.
Here are the scripts ive coded...
upload.php
<?
function replace_apostrophe ($string) {
$string = str_replace ("'","***apos***",$string);
$string = str_replace ('"',"***quot***",$string);
return $string;
}
$dbase = odbc_connect("Management", "", "");
if( ! $dbase )
{Error_handler( "Error in odbc_connect" , $dbase );}
if(isSet($img1)){
$binary_junk = fread(fopen($img1, "r"), filesize($img1));
$binary_junk = addslashes(replace_apostrophe($binary_junk));
$query = "INSERT INTO
images (ntext, filename, filesize, filetype)
VALUES (cast('$binary_junk' as ntext), '$img1_name', '$img1_size', '$img1_type')";
$result=odbc_exec( $dbase,$query );
if($result){
?>You submitted the file:<BR><BR>
NAME : <? echo($img1); ?><BR>
ORIGINAL NAME : <? echo($img1_name); ?><BR>
SIZE : <? echo($img1_size); ?><BR>
TYPE : <? echo($img1_type); ?><BR>
<BR><BR>
<?
}
}
else {
echo "
<form enctype='multipart/form-data' action='upload.php' method='post'>
Uploading:
<input type='file' name='img1'>
<input type='submit' name='upload' value='http://www.phpbuilder.com/board/archive/index.php/Upload'>
</form>
";
}
echo("<FORM enctype='multipart/form-data' action='download.php' method='POST'>");
$select_query = "SELECT id, filename from images";
$result = odbc_exec($dbase,$select_query);
if(!$result)
{Error_handler( "Error in download" , $dbase );}
echo("Downloading: ");
echo("<SELECT NAME='image_id'>");
while(odbc_fetch_row($result)){
$image_id=odbc_result($result,"id");
$image_name=odbc_result($result,"filename");
echo("<OPTION VALUE='http://www.phpbuilder.com/board/archive/index.php/".$image_id."'>".$image_name);
}
?>
</SELECT>
<INPUT TYPE="SUBMIT" VALUE=http://www.phpbuilder.com/board/archive/index.php/"View/Download">
</FORM>
NOTE : Files Downloaded MUST be saved, not opened directly from the server.
download.php
<?
function Error_Handler( $msg, $dbase ) {
echo "$msg \n";
odbc_close( $dbase);
exit();
}
function apostrophe_back ($string) {
$string = str_replace ("***apos***","'",$string);
$string = str_replace ("***quot***",'"',$string);
return $string;
}
$dbase = odbc_connect( "Management", "", "");
if( ! $dbase )
{Error_handler( "Error in odbc_connect" , $dbase );}
$query = odbc_exec ($dbase, "SET TEXTSIZE 800000");
$query3 = odbc_exec( $dbase,"SELECT image, ntext, filetype, filename
FROM images WHERE id = ".$image_id);
if(!$query3) {
Error_handler( "Error in download" , $dbase );
} else {
while(odbc_fetch_row($query3)) {
$image_type=odbc_result($query3,"filetype");
$image=stripslashes(stripslashes(apostrophe_back(odbc_result($query3,"ntext"))));
$filename =odbc_result($query3,"filename");
}
header("Content-type: application/download");
header("Content-Disposition: attachment; filename= $filename");
echo($image);
}
?>
Any clues to where the truncation may be occuring would be a great help!
Here are the scripts ive coded...
upload.php
<?
function replace_apostrophe ($string) {
$string = str_replace ("'","***apos***",$string);
$string = str_replace ('"',"***quot***",$string);
return $string;
}
$dbase = odbc_connect("Management", "", "");
if( ! $dbase )
{Error_handler( "Error in odbc_connect" , $dbase );}
if(isSet($img1)){
$binary_junk = fread(fopen($img1, "r"), filesize($img1));
$binary_junk = addslashes(replace_apostrophe($binary_junk));
$query = "INSERT INTO
images (ntext, filename, filesize, filetype)
VALUES (cast('$binary_junk' as ntext), '$img1_name', '$img1_size', '$img1_type')";
$result=odbc_exec( $dbase,$query );
if($result){
?>You submitted the file:<BR><BR>
NAME : <? echo($img1); ?><BR>
ORIGINAL NAME : <? echo($img1_name); ?><BR>
SIZE : <? echo($img1_size); ?><BR>
TYPE : <? echo($img1_type); ?><BR>
<BR><BR>
<?
}
}
else {
echo "
<form enctype='multipart/form-data' action='upload.php' method='post'>
Uploading:
<input type='file' name='img1'>
<input type='submit' name='upload' value='http://www.phpbuilder.com/board/archive/index.php/Upload'>
</form>
";
}
echo("<FORM enctype='multipart/form-data' action='download.php' method='POST'>");
$select_query = "SELECT id, filename from images";
$result = odbc_exec($dbase,$select_query);
if(!$result)
{Error_handler( "Error in download" , $dbase );}
echo("Downloading: ");
echo("<SELECT NAME='image_id'>");
while(odbc_fetch_row($result)){
$image_id=odbc_result($result,"id");
$image_name=odbc_result($result,"filename");
echo("<OPTION VALUE='http://www.phpbuilder.com/board/archive/index.php/".$image_id."'>".$image_name);
}
?>
</SELECT>
<INPUT TYPE="SUBMIT" VALUE=http://www.phpbuilder.com/board/archive/index.php/"View/Download">
</FORM>
NOTE : Files Downloaded MUST be saved, not opened directly from the server.
download.php
<?
function Error_Handler( $msg, $dbase ) {
echo "$msg \n";
odbc_close( $dbase);
exit();
}
function apostrophe_back ($string) {
$string = str_replace ("***apos***","'",$string);
$string = str_replace ("***quot***",'"',$string);
return $string;
}
$dbase = odbc_connect( "Management", "", "");
if( ! $dbase )
{Error_handler( "Error in odbc_connect" , $dbase );}
$query = odbc_exec ($dbase, "SET TEXTSIZE 800000");
$query3 = odbc_exec( $dbase,"SELECT image, ntext, filetype, filename
FROM images WHERE id = ".$image_id);
if(!$query3) {
Error_handler( "Error in download" , $dbase );
} else {
while(odbc_fetch_row($query3)) {
$image_type=odbc_result($query3,"filetype");
$image=stripslashes(stripslashes(apostrophe_back(odbc_result($query3,"ntext"))));
$filename =odbc_result($query3,"filename");
}
header("Content-type: application/download");
header("Content-Disposition: attachment; filename= $filename");
echo($image);
}
?>
Any clues to where the truncation may be occuring would be a great help!