note 56157 added to function.base64-encode

From: Date: Thu, 25 Aug 2005 10:05:20 +0000
Subject: note 56157 added to function.base64-encode
Groups: php.notes 
Request: Send a blank email to php-notes+get-94172@lists.php.net to get a copy of this message
I've used base64_encode and base64_decode for file attachment both in MySQL (using a BLOB field) and MSSQL (using a TEXT field). For MSSQL remember to set in PHP.INI file both mssql.textsize and mssql.textlimit to 2147483647. Here's the code: ######### MSSQL(mssql_)/MySQL(mysql_) file attach $val=$HTTP_POST_FILES['lob_upload']['tmp_name']; $valn=$HTTP_POST_FILES['lob_upload']['name']; $valt=$HTTP_POST_FILES['lob_upload']['type']; $data=base64_encode(addslashes(fread(fopen($val, "r"), filesize($val)))); mssql_connect("srv","usr","pass") or die (""); mssql_select_db("db") or die (""); $query = "UPDATE $table SET $field='$data', $fieldname='$valn', $fieldtype='$valt' WHERE DocID='$DocID'"; $result = mssql_query($query) or die(mssql_error()); mssql_close(); ######### MSSQL(mssql_)/MySQL(mysql_) open file attached mssql_connect("srv","usr","pass") or die (""); mssql_select_db("db") or die (""); $query = "SELECT $field,$fieldtype FROM $table WHERE DocID='$DocID'"; $result = mssql_query($query) or die(mssql_error()); $row = mssql_fetch_array($result); header("Content-type: $row[1]"); echo stripslashes(base64_decode($row[0])); This strategy is good for Microsoft Word, Acrobat PDF, JPG image and so on (even zipped files!!!) ---- Manual Page -- http://www.php.net/manual/en/function.base64-encode.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56157 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56157&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56157&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56157&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56157&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56157&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56157&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56157&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94172) next »