Hi all. I'm having a problem uploading images with PHP - I've done it successfully before, but it's been a while. Now, the HTML form has the appropriate enctype and MAX_SIZE hidden field, everything runs correctly through the traps that I have set up, which leads me to believe that the application actually does think it's uploading the image. Problem is that it's just not uploading the image. The code is as follows....

$func=addTeam($_POST, $HTTP_POST_FILES['img']);
if($func){
print("didn't work");
}else{
print("worked");
}

function addTeam($infoArr, $fileArr){
	if($infoArr['tuition'] == ""){
		$tuition=0.00;
	}else{
		$tuition=$infoArr['tuition'];
	}
	if(uploadImage($fileArr, "teams")){
		$qry="INSERT INTO tblteams (name, category, img, level, tuition) VALUES (\"".addslashes($infoArr['tName'])."\", \"".$infoArr['cat']."\", \"".$fileArr['name']."\", \"".addslashes($infoArr['lvl'])."\", $tuition);";
		$rs=mysql_query($qry);
		$id=mysql_insert_id();
		if(!$rs){
			$retVar = false;
		}else{
			$retVar = addRoster($infoArr, $id);
		}
	}
	return($retVar);
}

//upload photos....
function uploadImage($fileArr, $fileDest){
	$fileDir = "../images/".$fileDest;
	if($fileArr['name']!=""){
		if(is_uploaded_file($fileArr['tmp_name'])){
//			if(($fileArr['type'] == "image/gif") || ($fileArr['type'] == "image/jpeg")){
			if(getimagesize($fileArr['tmp_name'])){
				if(move_uploaded_file($fileArr['tmp_name'], "$fileDir".$fileArr['name'])){
					$retVar=true;
				}else{
					$retVar=false;
				}
			}else{
				$retVar=false;
			}
		}else{
			$retVar=false;
		}
	}else{
		$retVar=false;
	}
	return($retVar);
}

I may have gotten myself confused with all the error checking, but I don't think that I have - everything seems to run fine. The "images/teams/" directory is chmod777, so that's not the problem. And the record inserts itself properly into the database, which should only happen if the file is successfully moved from the temporary directory to the permanent. And yet there's no file. Anyone have any ideas on what I'm missing here? I've looked through the manual, Sam's Teach Yourself PHP in 24 hours, Core PHP Programming, and past code that did work on the same server, and yet I can't find the fault. I've got more books that I could look through, but I'm in the process of moving and I'm not sure in which box they're packed.

    Try checking $fileArr['error'] at the beginning of upload function. If it's not equal to 0, you can find error codes in php manual (There is a whole chapter about file upload handling)

      Thanks wilku, but the wierd thing is that $fileArr['error'] is showing 0, meaning that it really does think that the image is uploaded. But it's not... Maybe I'm sending it to a different directory, but I really don't think so... Oh well, off to search the server, then experiment and see if I can figure out what's happening. If anybody has any other ideas, I'm more than willing to listen!

        I'm a doofus. When I called the uploadImage() function, I didn't put the trailing slash on the directory argument. So, instead of calling

        uploadImage($fileArr, "teams")

        , I call

        uploadImage($fileArr, "teams/")

        and it works like a charm. :bemused: Sorry about the time wasting - but thanks for taking a look, wilku.

          Write a Reply...