Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Saturday, May 21, 2022

How to trim the explode string in PHP



$string = 'ABC, DEF, GHI';

$expl = array_map('trim', explode(',', $string));

Thursday, December 29, 2016

Laravel 4 - unserialize(): Error at offset

Enter both command below:

php artisan cache:clear <enter>
php artisan key:generate <enter>

Done!

Sunday, November 10, 2013

[PHP] - Download mutiple BLOB data and convert into file system

Last week I was struggled on how to convert data that stored on DB BLOB into the file system. There are more than 100 rows on that tables. Whoooaaaarrgghhhh!!. So how I’m gonna save it? The user needs those files in soft copy (I mean the file will be save in a thumbdrive). After few hours googling and read some forums, I came out with the solution that can be useful to others.

 
/**
 * @author MJMZ
 * @copyright 2012
 * Description : This file will be used to convert all the BLOB
 * contents and save into the file system.
 *
 */
 
include("../../../lib/db_common.php");
 
/* get document_id, document_name, folder_id, document_fileType, document_fileName */
$sql = 'SELECT document_id,
        document_name,
        folder_id,
        document_fileType,
        document_fileName
        FROM portal_document_upload';
$result = $db->query($sql);
 
$doc = array();
 
while($rows = mysql_fetch_array($result))
{
    $doc[] = array('docId' => $rows['document_id'],
                   'exacFilename' => $rows['document_fileName'],
                   'filename' => $rows['document_name'],
                   'filetype' => $rows['document_fileType']);
}
 
foreach($doc as $a => $key)
{
    // get the file extension : This code will only work only
    // if file got 1 extension. If the file got many extension
    // such as filename.tar.gz, this code will not work at all.
    // Try find another solution for that particular file.
    $fileXtension = pathinfo($key['exacFilename'], PATHINFO_EXTENSION);
 
    // If we select the blob in the first query, it will
    // cause bad queries @ mysql log.
    // So we select the blob in foreach based on their document_id.
    $sql_file_contents = "SELECT document_fileData
                          FROM portal_document_upload
                          WHERE document_id ='".$key['docId']."'";
    $theFile = $db->query($sql_file_contents);
    $row = mysql_fetch_array($theFile);
 
    // For precaution, we find the empty string @ filename
    // and replace them with _ (underscore) in order to avoid
    // the filename missing their extension.
    $rawFile = str_replace(" ","_", $key['filename']).".".$fileXtension;

    // the path for downloaded file.
    $path = "/home/httpd/files_convert/".$rawFile;
 
    if (file_put_contents($path, $row['document_fileData']) === FALSE ) {
        echo "Could not write PDF File";
    } else {
        file_put_contents($path, $row['document_fileData']);
    }
 
 }
 
?>

Now go to the $path folder that you declared in above script. Select all files, right click and save to thumbdrive.

Tuesday, October 1, 2013

[PHP] - Encode / Decode paramater on the URL using base64


function my_base64_encode($input) {
	return strtr(base64_encode($input), 'AEIOUj', '+-_,');
}
 
function my_base64_decode($input){
	return base64_decode(strtr($input, '+-_,', 'AEIOUj'));
}

$texttoencode = 'RAIHAN';

$b = my_base64_encode($texttoencode);
// the value of $b now is UkFJS-F,

$c = my_base64_decode($b);
// the value of $c is RAIHAN