Help with a bug or error


#1

Hi everyone, I need some help, go figure. I have a php photo upload tool that someone built for me a while back but it doesn’t seem to work and nothing I do seams to fix it.

Everytime I try to load the image it gives me the following error:

“Error! Problem [6] reading full size image”

I have included the php file below I use to load the image. I don’t know if I maybe have something setup on the server wrong or what. It used to work on our old server, but never tried it till now on our new server.

PHP file Below:
THANKS


<? /* -*-hm--html-*-
 *---
 *
 *	File:		LoadPic.php
 *	CVS:		$Revision: 1.3 $
 *	Description:	Load a picture into the database
 *	Author:		
 *	Created:	Fri Apr 25 21:11:58 2003
 *	Modified:	Sat Oct 18 12:33:57 2003
 *	Modified by:	
 *	Language:	PHP
 *	Package:	N/A
 *	Status:		
 *
 *	
 *
 *	Load a photo (and optionally create a category) into the
 *	photo database.
 *
 *	LoadPhoto.php will ask the user for a photo and information
 *	about a photo, and then load the photo into the photo directory
 *	Creating a thumbnail in the "Thumbs" subdirectory, and storing
 *	the photo information in the 'Images' table, creating a category
 *	if needed.
 *
 *	Uses "Connect.php" to determine database (and other) info.
 *	Uses "PhotoUtil.php" for utility functions
 *
 *	Photo information is stored in the "Images" table
 *	Category information is stored in the "Categories" table
 *---
 */

if (!$Back) $Back = getenv("HTTP_REFERER");
require_once("Connect.php");
require_once("PhotoUtil.php");

$numCat = 0;
$categories = array();
$categories["none"] = 0;
$categories["Add New"] = -1;
$numCat = BuildCategories($categories);

?>
<html>
<?
if ($submitted) {
    /* Uncomment for debugging
    echo "PhotoDir = $photoDir<br>n";
    echo "Back = $Back<br>n";
    echo "Caption = $caption<br>n";
    echo "Desc = $Desc<br>n";
    echo "Category = $category<br>n";
    echo "Category Name = $categoryName<br>n";
    echo "localName = $localName<br>n";
    echo "dlName = $dlName<br>n";
    */
?>
    <head>
	<title>Image Added</title>
	 <!-- Changed by: Lew Gaiter III, 24-Oct-2003 -->
    </head>
    <body>
        <h1>Image Added</h1>
<?
    if ($category == -1) {
	$query  = "INSERT INTO `Categories` (`catName` )n";
	$query .= "VALUES ('$categoryName')";
	$res = mysql_query($query);
	if ($res) {
	    $category = mysql_insert_id();
	    echo "Added category '$categoryName'<br>n";
	} else {
	    echo "Category Storage error: " . mysql_error() . "<br>n";
	}
    }
    /*
     * Read the image
     */
    $dir_uri = apache_lookup_uri($photoDir);
    $dir = $dir_uri->filename;
    if (@filetype($dir) != "dir") {
	if (@filetype($dir)) {
	    die("Error! $photoDir exists and is <b>not</b> a directory");
	}
	if (!mkdir($dir, 0775)) {
	    die("Error creating $dir");
	}
    }

    $dir_uri = apache_lookup_uri("$photoDir/Thumbs");
    $dir = $dir_uri->filename;
    if (@filetype("$dir") != "dir") {
	if (@filetype("$dir")) {
	    die("Error! $photoDir/Thumbs exists " .
		"and is <b>not</b> a directory");
	}
	if (!mkdir("$dir", 0775)) {
	    die("Error creating $dir");
	}
    }
    $err = 0;

    $img = "$photoDir/$localName.jpg";
    $img_uri = apache_lookup_uri($img);
    $img_fn = $img_uri->filename;
    $image = LoadPhotoJpg("theFile", $img_fn, 1024, 768);
    if ($image) {
	echo "Error! Problem [$image] reading full size image<br>n";
	$err = 1;
    }

    $tn = "$photoDir/Thumbs/$localName.jpg";
    $tn_uri = apache_lookup_uri($tn);
    $tn_fn = $tn_uri->filename;
    if (!$err) {
	$thumb = LoadPhotoJpg("theFile", $tn_fn, 100, 100);
	if ($thumb) {
	    echo "Error! Problem [$thumb] reading thumbnail<br>n";
	    $err = 1;
	}
    }

    if (!$err) {
	$query  = "INSERT INTO Images ( `imageId`, `imageCaption`,n";
	$query .= "                     `imageDesc`, `imageFile`,n";
	$query .= "                     `imageCat`)n";
	$query .= "       VALUES (0, '$caption', '$Desc',n";
	$query .= "               '$localName.jpg', '$category')";

	$res = mysql_query($query);
	if ($res) {
	    echo "Added image '$caption'<br>n";
	} else {
	    echo "Image Storage error: " . mysql_error() . "<br>n";
	}
    }
    mysql_close();
    echo "	<a href='$img'><img src='$tn'></a>n";
    echo "	<br><br>n";
    echo "	<a href='$Back'>n";
    echo "	    Return to calling page</a>n";
    echo "    </body>n";
} else {
    if (strncmp($type, "Delete", 6)) {
	if (!$Name) $Name = "Add an";
	else $Name = $Name . " an";
    } else {
	$Name = "Delete an";
    }
    if (!strncmp($type, "Delete", 6)) {
	echo "    <head>n";
 	echo "	<title>$Name Image</title>n";
	echo "    </head>n";
	echo "    <body>n";
	$query  = "SELECT imageFile FROM Images ";
	$query .= "WHERE `imageId`='$picId'";
	//echo "query = $query<br>n";
	if ($res = mysql_query($query)) {
	    $image = mysql_fetch_array($res);
	    extract($image);
	    $img = $photoDir . "/" . $imageFile;
	    $img_uri = Apache_lookup_uri($img);
	    $img_fn = $img_uri->filename;
	    $tn = $photoDir . "/Thumbs/"  . $imageFile;
	    $tn_uri = apache_lookup_uri($tn);
	    $tn_fn = $tn_uri->filename;
	    $query  = "DELETE FROM Images ";
	    $query .= "WHERE `imageId`='$picId'";
	    Echo "query = $query<br>n";
	    if (!($res = mysql_query($query))) {
		echo "Failed to remove photo info from database";
		return;
	    }
	    echo "Removing '$img_fn' and '$tn_fn'<br>n";
	    unlink($img_fn);
	    unlink($tn_fn);
	    echo "	<br><br>n";
	    echo "	<a href='$Back'>n";
	    echo "	    Return to calling page</a>n";
	    echo "    </body>n";
	}
	return;
    }
?>
    <head>
	<title><?php echo "$Name"?> Image</title>
	<script language="JavaScript">
	    function OnSubmit(theForm) {
		var theCat = theForm.category;
		var catName = theForm.categoryName;
		var ind = theCat.selectedIndex;
		var opt = theCat.options[ind];
		var theFile = theForm.theFile;
		var newCat;

		if (!theFile.value || (theFile.value == "")) {
		    alert("No file specified. " +
			  "Please specify a file to upload");
		    theFile.focus();
		    return false;
		}

		if (opt.value == "-1") {
		    newCat = prompt("Please enter the new Category name");
		    opt.text = newCat;
		}
		catName.value = opt.text;

		return true;
	    }
	</script>
    </head>
    <body onLoad="document.theForm.caption.focus()">
	<h1><center><?php echo "$Name"?> Image</center></h1>
	<br>
<?
    $tmpName = tempnam($photoDir, "Photo");
    $localName = basename($tmpName);
    $img = "/photoDir/$localName";
    $img_uri = apache_lookup_uri($img);
    $img_fn = $img_uri->filename;

    $tn = "/photoDir/Thumbs/$localName";
    $tn_uri = apache_lookup_uri($tn);
    $tn_fn = $tn_uri->filename;
?>
	<form method="post" enctype="multipart/form-data"
	      name="theForm" onSubmit="return OnSubmit(this)">
	    <center>
		<input type="hidden" name="MAX_FILE_SIZE" value=4194304>
		<input type="hidden" name="submitted" value=1>
		<table>
		    <tr>
			<td align=right>Image Caption:&nbsp;</td>
			<td><input type=text width=40 name=caption></td>
		    </tr>
		    <tr>
			<td align=right valign=middle>Description:&nbsp;</td>
			<td><textarea name=Desc 
				      rows=4 cols=40 softwrap></textarea></td>
		    </tr>
		    <tr>
			<td align=right>Select a category:&nbsp;</td>
			<td><select name=category>
				<?php OptArray($categories, 0) ?>
			    </select>
			</td>
		    </tr>
		    <tr>
			<td align=right>Select a file:&nbsp;</td>
			<td><input type="file" name="theFile" size=15></td>
		    </tr>
		</table>
		<input type=hidden name=Back value="<?echo $Back?>">
		<input type=hidden name=categoryName value="">
		<input type=hidden name=imageNum value=0>
		<input type="hidden" name="localName" size=40
		       value="<?php echo $localName?>"><br>
		<input type="hidden" name="dlName" size=40 value="">
		<br>
		<br>
		<input type="submit" name="submit" value="Load File">
	    </center>
	</form>
<?
}
?>
    </body>
</html>

Mod edit: Added code tags


#2

Ok, maybe I am getting somewhere… I understand the “6” in the error I think I believe the PHP file below is telling me that there is a resizing isssue with the images I am trying to load.

SO if my assumption is correct, can someone tell me how you change aceptable sizes???

Here is the other php file I think controling and returning the error:

function BuildCategories(&$categories)
{
$numCat = 0;
$query = "SELECT * FROM `Categories`";
//echo "query = $query<br>n";
if ($res = mysql_query($query)) {
while ($cat = mysql_fetch_array($res)) {
extract($cat);
$categories[$catName] = $catId;
$numCat++;
}
}
return $numCat;
}

function OptArray($a, $sel)
{
reset($a);
while (list ($key, $val) = each ($a)) {
echo "<option value='$val'";
if ($sel && ($sel == $val)) echo " selected";
echo ">$key</option>n";
}
}

/* LoadPhotoJpg() - Load a photo from a form and save it as a JPG
*
* Input: $Name - Name of the "file" field in the form
* $upfile - Name of destination file
* $wid - Max width to resize file to
* $ht - Max height to resize file to
*
* Output: 0 - Successfully loaded the file
* 1 - Failed: No data specified in form
* 2 - Failed: Form element file was an empty file
* 3 - Failed: Uploaded file not GIF/JPG/PNG
* 4 - Failed: File upload error
* 5 - Failed: Copying File to final location
* 6 - Failed: Resizing file to acceptable size
*
* Example: HTML -> <form method=post enctype="multipart/form-data">
* <input type=file size=25 name="myGraphic1">
* </form>
*
* PHP -> LoadPhoto("myGraphic1", "/my/dest/file.gif);
*
*/
function LoadPhotoJpg($Name, $upfile, $wid=0, $ht=0, $fmt = "jpg")
{
//echo "Inside LoadPhoto($Name, $upfile, $wid, $ht)<br>n";
$res = GetPhotoInfo($Name, $wid, $ht);
if (gettype($res) == "integer") return $res;

list($fName, $fSize, $fType) = $res;

/*
* We've got the file locally, now copy it to the proper location
*/
//echo "Copying '$fName' to '$upfile'<br>n";
//echo "File type = $fType<br>n";
if (!copy($fName, $upfile)) {
return 5;
}
if (($fType != "image/jpg") && ($fType != "image/jpeg")
&& ($fType != "image/pjpeg") ) {
$mog="/usr/bin/X11/mogrify -format " . $fmt;
//echo "$mog $upfile<br>n" ;
exec("$mog $upfile", $ret, $retval);
if ($retval) return 6;
}
return 0;
}

/* GetPhotoInfo - Get Information about a photo field
*
* Input: $Name - Name of the "file" field in the form
* $wid - Max width to resize file to
* $ht - Max height to resize file to
*
* Output:
* Error code (See LoadPhotoStr above)
* -or-
* Array:(File Name, File Size, File Type)
*
*/
function GetPhotoInfo($Name, $wid=0, $ht=0)
{
/*
* Get the file based on the field name the user specified.
*/
$f_tmp_name = $_FILES[$Name]['tmp_name'];
$f_name = $_FILES[$Name]['name'];
$f_size = $_FILES[$Name]['size'];
$f_type = $_FILES[$Name]['type'];

if (!$f_tmp_name) return 1;
if ($f_tmp_name == "none") return 1;

if ($f_size == 0) {
return 2;
}

/*
* Make sure we have a useable graphic file
*/
if (($f_type != "image/jpeg") &&
($f_type != "image/pjpeg") &&
($f_type != "image/gif") &&
($f_type != "image/png")) {
//echo "$Name was of type '$f_type'<br>n";
return 3;
}

if (!is_uploaded_file($f_tmp_name)) {
return 4;
}

/*
* Build file transform command - resize if requested
*/
$mog="/usr/bin/X11/mogrify ";
if ($wid && $ht) $mog = $mog . "-geometry '" . $wid . "x" . $ht . ">' ";

exec("$mog $f_tmp_name", $ret, $retval);
if ($retval) return 6;

return array($f_tmp_name, $f_size, $f_type);
}

?>

Mod Edit: Added Code Tags


#3

Not enough information about the problem and Too much code.

Try and narrow things down a bit to let us know where to start. No one wants to just chug through your large chunk of code.

What have you done to trouble shoot it? What were the results?

The more you narrow things down, the more likely you are to get meaningful help.


#4

My guess is that this line (in the second file you posted):

$mog="/usr/bin/X11/mogrify -format " . $fmt;

is the one causing you trouble since you say you moved servers. From what I can tell this script uses an external program to resize the images and that may not be on the new server or you may not have access. Try looking in to that.