Skip to content

Instantly share code, notes, and snippets.

@tmbritton
Created July 25, 2012 23:09
Show Gist options
  • Save tmbritton/3179265 to your computer and use it in GitHub Desktop.
Save tmbritton/3179265 to your computer and use it in GitHub Desktop.
Attempt to write images from Blobs
<?php
$db['hostname']='localhost';
$db['username']='root';
$db['password']='root';
$db['schema']='old_wkf';
if(!$link = mysqli_connect($db['hostname'], $db['username'], $db['password'])){
echo mysqli_errno($link) . ": " . mysqli_error($link) . "\n";
}
if(!mysqli_select_db($link, $db['schema'])){
echo mysqli_errno($link) . ": " . mysqli_error($link) . "\n";
}
$sql = 'SELECT ID, GARDEN_PHOTO_UPLOAD, GARDEN_PHOTO_UPLOAD_FILENAME FROM `grant_request` WHERE DECISION_YN="Y" LIMIT 10';
if($result = mysqli_query($link, $sql)){
while($row = mysqli_fetch_array($result)) {
$filename = $row['GARDEN_PHOTO_UPLOAD_FILENAME'];
$id = $row['ID'];
$blob = $row['GARDEN_PHOTO_UPLOAD'];
if(writeImage($filename, $id, $blob)){
echo 'SUCCESS! Grant id ' . $id . ' file written' . "\n";
} else {
echo 'FAIL! Grant id ' . $id . ' file not written' . "\n";
}
}
} else {
echo mysqli_errno($link) . ": " . mysqli_error($link) . "\n";
}
function getExtension($filename){
if($array = explode('.', $filename)){
$last = count($array) -1;
$ext = strtolower($array[$last]);
return $ext;
} else {
return FALSE;
}
}
function writeFile($filename, $id, $info){
$ext = '.' . getExtension($filename);
$fh = fopen('wkfimages/' . $id . $ext, 'w');
if(fwrite($fh, $info)){
return TRUE;
} else {
return FALSE;
}
}
function writeImage($filename, $id, $info){
$info = base64_decode($info);
if($im = imagecreatefromstring($info)){
if(imagejpeg($im, 'wkfimages/' . $id . '.jpg')){
return TRUE;
}
}
return FALSE;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment