<?php
$ingest = new dataIngest;
$ingest->addImagesToDatabase('/path/to/directory/to/ingest/');
//$ingest->showLog();
class dataIngest{
private $permittedTypes = array( 'image/jpg', 'image/jpeg', 'image/png', 'image/gif',
'image/bmp' ,'image/tiff', 'image/pdf');
private $tableName = ''; //enter your database table name here
public function __construct(){
$this->maxFileSize = 5242880;
$this->maxChunkSize = 1048576;
}
private function updateDatabase($data){
$result = true;
foreach ($data as &$d){
$d = mysql_real_escape_string($d);
}
//array_walk($data, 'mysql_real_escape_string');
//
$sql = sprintf("Update $this->tableName set fileData = concat(fileData, '%s') where id=%d", $data[1], $data[0]);
$result = @mysql_query($sql);
return $result;
}
public function addImagesToDatabase($dir){
clearstatcache();
$dir = realpath($dir) . '/';
//open the directory
$this->log($dir, 'Opening directory for parsing', 'event');
$dh = opendir($dir);
if ($dh){
$this->log($dir, 'Directory opened', 'event');
} else {
$this->log ($dir, ' Directory cannot be opened', 'error');
if (!is_readable($dir)){
$this->log ($dir, 'Directory is not readable', 'error');
}
}
//iterate the directory
while ( false !== ($file = readdir($fh))){
$this->log($dir . $file, 'looging at this file', 'event');
if (!is_file($dir . $file)){
if (is_dir($dir . $file) && $file != '.' && $file != '..'){
if (is_readable($dir . $file)){
$this->log ($dir . $file, 'Recursing into this directory', 'event');
$this->addImagesToDatabase ($dir.$file);
} else {
$this->log ($dir.$file, 'Cannot parse as the directory is not readable' ,'error');
}
} else {
//do nothing
}
} else {
if (is_readable ($dir . $file)){
$this->log ($dir.$file, 'gathering data' ,'event');
$type = $this->getfiletype($dir.$file);
$size = filesize ($dir.$file);
if (in_array($type, $this->permittedTypes)){
if ($size <= $this->maxFileSize){
$fileName = $file;
$this->log ($dir.$file, 'writing to the database' ,'event');
if ($size >= $this->maxChunkSize){
$this->log ($dir.$file, 'writing to the database in chunks', 'event');
$fh = fopen($dir.$file, 'rbt');
$done = null;
while (false !== ($section = fread($fh, $this->maxChunkSize))){
if(empty($done)){
$result = $this->writeToDatabase(array($file, $section, $size, $type));
$id = mysql_insert_id();
$done = true;
} else {
$result = $this->updateDatabase(array($id, $section));
}
if ($result){
$this->log ($dir.$file, 'Chunk saved to the database', 'event');
} else {
$this->log ($dir.$file, 'Error saving chunk to the database.<br/>' . mysql_error(), 'error');
}
}
} else {
$this->log ($dir.$file, 'Writing to the database in one go', 'event');
$result = $this->writeToDatabase(array($file,file_get_contents($dir . $file), $size, $type));
if ($result){
$this->log ($dir.$file, 'File saved to the database', 'event');
} else {
$this->log ($dir.$file, 'Error saving file to the database.<br/>' . mysql_error(), 'error');
}
}
} else {
$this->log ($dir.$file, 'skipping file as it is too large', 'error');
}
} else {
$this->log ($dir.$file, 'skipping file as not a permitted file type', 'error');
}
} else {
$this->log ($dir.$file, 'file is not readable', 'error');
}
}
}
}
public function showLog(){
echo <<<CSS
<style type="text/css">
.error{
background-color: red;
}
.event {
background-color: #fff7e7;
}
.box {
border: dotted black thin;
}
</style>
CSS;
echo "<table>\r\n";
foreach ($this->events as $e){
echo <<<HTML
<tr class="{$e->type}">
<td>{$e->item}</td>
<td>{$e->message}</td>
</tr>
HTML;
}
echo "</table>\r\n";
}
private function log($item, $message, $type){
$this->events[] = (object) array('item'=>$item, 'message'=>$message, 'type'=>$type);
$this->showlog();
$this->events = array() ; //really ugly...
}
private function getFileType($file){
if (class_exists('finfo')){
$fi = new finfo(FILEINFO_MIME);
$mime_type = $fi->buffer(file_get_contents($file));
} elseif (function_exists('mime_content_type')){
$mime_type = mime_content_type($file);
} else{
$mime_type = $this->getFileTypeFromExtension($file);
}
return empty($mime_type) ? 'unknown' : $mime_type;
}
private function writeToDatabase ($data){
$result = true;
foreach ($data as &$d){
$d = mysql_real_escape_string($d);
}
//array_walk($data, 'mysql_real_escape_string');
//
$sql = vsprintf("Insert into $this->tableName (id, fileName, fileType, fileSize, fileData) values (NULL,'%s','%s', '%s', '%s')", $data);
$result = @mysql_query($sql);
return $result;
}
private function needsChunkifying($size){
return ($size >= $this->maxChunkSize);
}
private function getFileTypefromExtension($file){
$bits = explode ('.', $file);
$extension = end($bits);
switch(strtolower($extension)){
case "js" : return "application/x-javascript";
case "json" : return "application/json";
case "jpg" :
case "jpeg" :
case "jpe" :
return "image/jpg";
case "png" :
case "gif" :
case "bmp" :
case "tiff" :
return "image/".strtolower($extension);
case "css" : return "text/css";
case "xml" : return "application/xml";
case "doc" :
case "docx" : return "application/msword";
case "xls" :
case "xlt" :
case "xlm" :
case "xld" :
case "xla" :
case "xlc" :
case "xlw" :
case "xll" : return "application/vnd.ms-excel";
case "ppt" :
case "pps" : return "application/vnd.ms-powerpoint";
case "rtf" : return "application/rtf";
case "pdf" : return "application/pdf";
case "html" :
case "htm" :
case "php" : return "text/html";
case "txt" : return "text/plain";
case "mpeg" :
case "mpg" :
case "mpe" : return "video/mpeg";
case "mp3" : return "audio/mpeg3";
case "wav" : return "audio/wav";
case "aiff" :
case "aif" : return "audio/aiff";
case "avi" : return "video/msvideo";
case "wmv" : return "video/x-ms-wmv";
case "mov" : return "video/quicktime";
case "zip" : return "application/zip";
case "tar" : return "application/x-tar";
case "swf" : return "application/x-shockwave-flash";
default : return "unknown/$extension";
}
}
}
?>