Hi all. I have a question. I'm trying to upload JPEG files into a BLOB database column. In doing so, I am encoding the file with base64_encode() as per mySQL.com comments.

So, when I try my script I get the following output:

You must have values for each drop-down. Don't forget to select a file to upload either.

Warning: Wrong parameter count for fread() in /home/ridgeswi/public_html/main/scripts/photos/upload.php on line 90
Adding the photo to the database failed!!

Here's my script:

<?php
function error($enum){
	switch($enum){
		case 1:
			echo "<font style='color: #f00;'>You must have values for each drop-down.  Don't forget to select a file to upload either.</font><br />";
			break;

	case 2:
		echo "<font style='color: #f00;'>Invalid File Type(s).  The only allowed file types are: JPG and JPEG</font><br />";
		break;

	case 3:
		echo "<font style='color: #f00;'>Current file does <b>NOT</b> exist!!</font><br />";
		break;

	case 4:
		echo "<font style='color: #f00;'>The file size is too large.  The maximum allowed size is 5 Mb.</font><br />";
		break;

	case 5:
		echo "<font style='color: #f00;'>Adding the photo to the database failed!!</font><br />";
		break;

	case 6:
		echo "<font style='color: #f00;'>Copying the file from the temp directory failed!!</font><br />";
		break;

	case 7:
		echo "<font style='color: #f00;'>Creating the thumbnail failed!!</font><br />";
		break;

	case 8:
		echo "<font style='color: #0f0;'>A thumbnail for the image ".$_FILES['file']['name']." was successfully created!!<br />";
		break;

	case 9:
		echo "<font style='color: #f00;'>Resizing for the image ".$_FILES['file']['name']." FAILED!!<br />";
		break;

	case 10:
		echo "<font style='color: #0f0;'>Resizing the image ".$_FILES['file']['name']." was successfully completed!!<br />";
		break;
}
}

$max = $_REQUEST['number'];
$maxSize = (1024*1024)*5;
$i = '1';

for($i; $i<=$max; $i++){
	echo "<h2>".$_FILES['file']['name']."</h2>
			<p class='article'>";

$yeari = 'year'.$i;
$cati = 'cat'.$i;
$eventi = 'event'.$i;
$photoi = 'photo'.$i;

if($_REQUEST[$yeari] == 'null' || 
	$_REQUEST[$cati] == 'null' || 
	$_REQUEST[$eventi] == 'null' || 
	!isset($_REQUEST[$photoi]) || 
	$_REQUEST[$photoi] == 'none'){
	error('1');
}

if($_FILES['file']['size']>$maxSize){
	error('4');
}

$year = $_REQUEST[$yeari];
$cat = $_REQUEST[$cati];
$event = $_REQUEST[$eventi];

$photo = base64_encode(fread(fopen($_FILES['file']['tmp_name'], "r")));

$q1 = "INSERT INTO `Photos` (pyid, pcid, peid, Photograph) VALUES ('$year', '$cat', '$event', '$photo')";
$r1 = mysql_query($q1);
if(!$r1 || $r1 === FALSE){
	error('5');
}
else{
	if(copy($_FILES['file']['tmp_name'], $uploadDir.$newname) === FALSE){
		error('6');
	}
	else{
		if($thumb = exif_thumbnail($_FILES['file']['tmp_name'], 75, 75, $type) === FALSE){
			error('7');
		}
		else{
			$thumbed = base64_encode(fread(fopen($thumb, "r")));
			$tql = "INSERT INTO `Photos` (Thumbnail) VALUES ('$thumbed')";
			$trslt = mysql_query($tql);
			error('8');
		}
		if($photo1 = exif_thumbnail($_FILES['file']['tmp_name'], 500, $type2) === FALSE){
			error('9');
		}
		else{
			$resized = base64_encode(fread(fopen($photo1, "r")));
			$lql = "UPDATE `Photos` (Photograph) VALUES ('$resized')";
			$lrslt = mysql_query($lql);
			error('10');
		}
	}
}
}
?>

Any idea what is going on?

I'd also like some direction with creating a thumbnail, and then uploading it to the database (this will be done with both the thumb & the image).

Here's what I'm trying to accomplish:

User uploads photo, photo is resized twice (once for thumbnail, once for enlargement of regular photo). I want to cut this script to as short as possible.

Any help would be greatly appreciated.

~Brett

    You can put the file straight into the database with LOAD_FILE - like

    $tempName=$_FILES['filename']['tmp_name'];
    
    	if ( is_uploaded_file( $tempName ) )
    	{
    		$tempName = mysql_escape_string($tempName);	//GAH!  This is the key to all my LOAD_DATA probs
    
    		$query = "INSERT INTO files (filename,filetype,data) VALUES ('$fileName','$fileType',LOAD_FILE('$tempName') )";
    
    		$res=mysql_query($query);
    		if (!$res)
    		{
    			echo "No Insert: ".mysql_error()."<br>"; 
    			die("GOODBYE");
    		}
    	}
    

    But I haven't used it for ages as there's loads of arguments for not storing files in databases.

    Another way of doing it is loading the file, escaping it, then throwing it in like this:

    		if ( is_uploaded_file( $tempName ) )
    		{
    			$fileData = mysql_escape_string(file_get_contents($tempName));
    
    		$query="INSERT INTO files (filename,filetype,data) VALUES ('$fileName','$fileType','$fileData')";
    	}

      GREAT!!

      Thanks for the help. But now, I don't know what's going on. Everything is submitting, but I can't use exif_thumbnail() to resize the image. It tells me that it can't find the function exif_thumbnail().

      Is there a better way to resize the image?

      ~Brett

        Write a Reply...