Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with error checking 1

Status
Not open for further replies.

mufka

ISP
Dec 18, 2000
587
US
I've got a script that will add images to a MySQL database. The image files are on the server and the script takes all of the files in the directory and puts them in the database as blob.

I'm trying to get some error checking to work. One of the issues is that if a file is too big, it does one of two things: It either successfully imports that one file that is too big and then stops with no error; or it imports every other file successfully and stops with no error when it gets to the file that is too big. I'd like to get it to spit out an error in either case so I can see why it is doing it. I'm also puzzled because if the file was too big, it shouldn't ever successfully import, but it does. My code is:

Code:
$path = "/pathtofiles/";
$listing = opendir($path);
while (false !== ($filename = readdir($listing))) {
	if ($filename !== "." && $filename !== "..") {
		$file = "$path$filename";
		$file_size = filesize($file);
		$file_type = mime_content_type($file);
		$description = substr($filename, 0, strrpos($filename, '.')); 
		$data = addslashes(fread(fopen($file, "r"), filesize($file)));
		$result="INSERT INTO images (description,filedata,filename,filesize,filetype)
		VALUES 
		('$description','$data','$filename','$file_size','$file_type')";
			if (!mysql_query($result))
			  {
		      echo ("<p><b>Could not add $filename</b>");
			  }	else {
					$id= mysql_insert_id();
					print "<p>Successfully added $filename: ID <b>$id</b>";
			  }
	}
}
 
try this code. just pass it the name of the field in your form.

Code:
<?php
function checkUpload($fieldName){
	//check for errors
	$msg = "OK";
	if (isset($_FILES[$fieldName])){
		switch ($_FILES[$fieldName]['error']){
			case 0:
				break;
			case 1:
			case 2:
				$msg = "The uploaded file is too large"; 
				break;
			case 3:
				$msg = "The uploaded file was only partially uploaded";
				break;
			case 4:
				$msg = "No file was uploaded";
				break;
			case 6:
			case 7:
			case 8;
				$msg = "There was a problem saving ths uploaded file.";
				break;
		}
	} else {
		$msg = "No file was uploaded";
	}
	return $msg;
}
?>

and you might want to construct your upload form to make use of the ini_get and MAX_FILE_SIZE directives. like so:

Code:
function displayUploadForm($msg=NULL){
	
	$maxsize = ini_get("upload_max_filesize");
	//if the shorthand notation has been used, convert to byte notation
	if (preg_match('/(\d*)(m|g|k)/i', $maxsize, $match)){
		switch (strtolower($match[2])){
			case "k":
				$_maxsize = $match[1] * 1024;
			break;
			
			case "m":
				$_maxsize = $match[1] * 1024 * 1024;
			break;
			case "g":
				$_maxsize = $match[1] * 1024 * 1024 * 1024;
			break;
		}
	}
	
	$msg = (empty($msg)) ? NULL: '<div class="errorMessage">'.$msg.'</div>';

	$contentObject = <<<HTML
$msg
<form method="post" action="{$_SERVER['PHP_SELF']}" enctype="multipart/form-data" >
<fieldset>
	<legend>Upload images</legend>
	<p>You can upload gif, png or jpegs either singly or in bulk as a zip file</p>
	<input type="hidden" name="MAX_FILE_SIZE" value="$_maxsize" />
	Choose file (max $maxsize): <input type="file" name="upload" /><br/>
	<input type="submit" name="submit" value="Upload" />
	<input type="hidden" name="action" value="uploadfile" />
</fieldset>
</form>
HTML;
	echo $contenObject;
}
 
oops, missed a conditional. please use this version of the form code instead

Code:
function displayUploadForm($msg=NULL){
	
	$maxsize = ini_get("upload_max_filesize");
	//if the shorthand notation has been used, convert to byte notation
	if (preg_match('/(\d*)(m|g|k)/i', $maxsize, $match)){
		switch (strtolower($match[2])){
			case "k":
				$_maxsize = $match[1] * 1024;
			break;
			
			case "m":
				$_maxsize = $match[1] * 1024 * 1024;
			break;
			case "g":
				$_maxsize = $match[1] * 1024 * 1024 * 1024;
			break;
		}
	} [red]else {
		$_maxsize &= $maxsize;
	}[/red]
	
	$msg = (empty($msg)) ? NULL: '<div class="errorMessage">'.$msg.'</div>';

	$contentObject = <<<HTML
$msg
<form method="post" action="{$_SERVER['PHP_SELF']}" enctype="multipart/form-data" >
<fieldset>
	<legend>Upload images</legend>
	<p>You can upload gif, png or jpegs either singly or in bulk as a zip file</p>
	<input type="hidden" name="MAX_FILE_SIZE" value="$_maxsize" />
	Choose file (max $maxsize): <input type="file" name="upload" /><br/>
	<input type="submit" name="submit" value="Upload" />
	<input type="hidden" name="action" value="uploadfile" />
</fieldset>
</form>
HTML;
	echo $contenObject;
}
 
I'm not using a form. Does that matter? Does the function checkUpload go in the while loop? I want it to import what it can, skip what it can't and tell me what it did.

Separately, I'd like to use a form but I haven't been able to find one that will upload recursively (every file in a folder).
 
sorry. i misread your post.

could you try this code? make sure that you have correctly set the tableName variable in the top of the class. and (obviously) include your database connection code before the class is instantiated.

i have not tested this code. there is a possibility that the code will fail if you do not have enough memory in the memory_limit directive. there may be ways to work around this if you hit this problem.


Code:
<?php

$ingest = new dataIngest;
$ingest->addImagesToDatabase('/path/to/images');
$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
	
	private 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);
	}
	
	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;
	}
	
	public function addImagesToDatabase($dir){
		clearstatcache();
		$dir = realpath($dir) . '/';
		//open the directory
		$this->log($dir, 'Opening directory for parsing', 'event');
		
		$fh = opendir($dir);
		if ($fh){
			$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');
						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)){
						$fileName = $file;
						$this->log ($dir.$file, 'writing to the database' ,'event');
						$result = $this->writeToDatabase(array($file, file_get_contents($dir . $file), $size, $type));
						if ($result){
							$this->log ($dir.$file, 'Saved to the database', 'event');
						} else {
							$this->log ($dir.$file, 'Error saving to the database.<br/>' . mysql_error(), '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');
				}
			}
		}
	}
	
	private function writeToDatabase (&$data){
		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 getFileTypefromExtension($file){
		$bits = explode ('.', $file);
		$extension = end($bits);
		
		switch(strtolower($fileSuffix[1])){
			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";
		}
	}
}
?>

 
It doesn't seem to work. Just a blank screen. I changed the tablename, changed the path, and included the password file.

When I add
Code:
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
I get:
Fatal error: Cannot pass parameter 1 by reference in /pathtofile/script.php on line 99
That is referring to:
Code:
 $result = $this->writeToDatabase(array($file, file_get_contents($dir . $file), $size, $type));
 
ok. that's annoying.

change this method as follows:

Code:
private function writeToDatabase ([red][s]&[/s][/red]$data){
		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;
	}
 
With that change, I get
Warning: mysql_real_escape_string() expects parameter 2 to be resource, integer given in /pathtofile/script.php on line 117

Fatal error: Call to private method dataIngest::showLog() from context '' in /pathtofile/script.php on line 7

Lines 7 and 117 are:
Code:
$ingest->showLog();

array_walk($data, 'mysql_real_escape_string');
 
two issues there. the second is easy to fix. change private to public in front of private function showlog()

the first is more interesting. it shows that something is wrong with the $data array.

could you try again and see what happens. if we're still getting that message we'll have to diagnose the array a bit more.
 
Making progress. I still get the mysql_real_escape_string() error, but it moves past it. Now I get

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄ' at line 1

 
lots of errors, i think.

try this replacement
Code:
<?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 addImagesToDatabase($dir){
		clearstatcache();
		$dir = realpath($dir) . '/';
		//open the directory
		$this->log($dir, 'Opening directory for parsing', 'event');
		
		$fh = opendir($dir);
		if ($fh){
			$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)){
						$fileName = $file;
						$this->log ($dir.$file, 'writing to the database' ,'event');
						$result = $this->writeToDatabase(array($file, file_get_contents($dir . $file), $size, $type));
						if ($result){
							$this->log ($dir.$file, 'Saved to the database', 'event');
						} else {
							$this->log ($dir.$file, 'Error saving to the database.<br/>' . mysql_error(), '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){
			$_data[] = 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 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";
		}
	}
}
?>
 
The end result is more or less the same as my original script (but with more feedback). It dies on the oversize file and goes no further. So I guess as far as error checking, it is a success. One difference with this one is that it never writes the oversize file, where with mine, sometimes it does. Is there a way to alter it so that it will skip the oversize file but keep going with the rest?
 
how oversize is oversize? we can add some file size checks into the process. what is the total memory that you allow to the php process?
 
The PHP limit is 24MB. But the blob limit is 2MB (at least that's what it says, but I have gotten 7MB into a blob with my script but inconsistently - I don't understand that). I guess it would be best to limit it to 2MB.
 
blobs are not limited to 2MB. a tinyblob is limited to2^8 bytes, medium 2^16 and large 2^32.

what is max_allowed_packet set to in your mysql server?

this sounds like something else is going wrong. can you look in your server logs and determine whether there are any relevant errors being recorded. we need to find out _why_ an oversize file is being failed.
 
MySQL is limiting largeblob to 2MB. It's a shared host limitation. And according the the provider, there is "no limit" on max_allowed_packet but I don't think they have a clue. I can't see the server logs.
 
ok.

so you have files of more than 2MB that need storage, but mysql will not store them for you. somehow a host has managed to hack the mysql core to change the way it behaves. this would lead me to think the following:

1. run away from this host as fast as you can: you don't want to store data in a database where some monkey has hacked the core.
2. don't store binaries in a database (it's a _really_ bad idea anyway). store them in the file system and then keep a pointer in the database.
3. i really don't believe the host: i cannot credit that they would hack the core. makes no sense at all.
4. i do not believe that there can be 'no limit' on the max_allowed_packet. a default would be assumed. I believe that the default is 1MB and the max is 1GB.

i suspect that there is still something else going on. but by all means try this code. it limits the file size to 5MB and uploads to the database in 1MB chunks.

Code:
<?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";
		}
	}
}
?>
 
You are right that the host is horrid. They are entirely incompetent and if I didn't know how to troubleshoot most of my problems, I would run away. (Sometimes just for shiggles, I chat in with them to mess around with them. It took them an hour to figure out what LONGBLOB was.) They do fit my budget and I get all I need, except for the occasional headaches. Just too much work to move.

This script seems to work for what I need it to do. I have my error checking. Thanks.
 
i don't know what your budget is, but i would suggest you check out bluehost. I have been with them for 9 months without complaint and the package cost me $150 for two years. i also use 1&1 but would not recommend them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top