Page 1 of 1

Why wont my file upload?

Posted: Sat Feb 17, 2007 3:00 am
by psychotomus
i get no error message

Code: Select all

$pos = strpos($_FILES["file"]["name"],'.');
		$file_type =  strtolower(substr($_FILES["file"]["name"], $pos + 1, strlen($_FILES["file"]["name"]) - $pos +1));
		if ($file_type == "jpg" || $file_typ == "png" || $file_type =="gif")
		{
			if (move_uploaded_file($_FILES["file"]["tmp_name"], "avatars/" . $_FILES["file"]["name"]))
			{
				if ($file_type == "jpg")
				{
					
					$filename = $userID . '.jpg';
					rename("avatars/" . $_FILES["file"]["name"],$filename);
					unlink("avatars/" . $_FILES["file"]["name"]);
					$src = imagecreatefromgif($filename);
				}
				elseif ($file_type == "png")
				{
					$filename = $userID . '.png';
					rename("avatars/" . $_FILES["file"]["name"],$filename);
					unlink("avatars/" . $_FILES["file"]["name"]);
					$src = imagecreatefromgif($filename);
				}
				else 
				{
					$filename = $userID . '.gif';
					rename("avatars/" . $_FILES["file"]["name"],$filename);
					unlink("avatars/" . $_FILES["file"]["name"]);
					$src = imagecreatefromgif($filename);
				}
					
				$w = imagesx($im); 
				$h = imagesy($im);
				
				if ($w > 150 || $h > 150)
					$eRR = 'avatar size can only be 150x150';
				else
					$avatar_url = "http://www.sundaybrew.com/avatars/" . $filename;
			}
			else
			{
				echo 'error uploading file';
			}
		}

Posted: Sat Feb 17, 2007 3:36 am
by Mordred
This has so many problems that it is better if you throw it away and write it again. Copy/pasting is not a good programming practice ;)
Reread carefully the section on handling file uploads in the manual. Use the correct imagecreate* functions for each type. Get rid of the rename/unlink nonsense. You have $file_typ instead of file_type.

Posted: Sat Feb 17, 2007 4:29 am
by psychotomus
link? im not gettign rid of unlink thingie, it keeps minimum files on server stored by user id instead of letting them upload like 100 different avatars.

Posted: Sat Feb 17, 2007 7:02 am
by superdezign
You are incorrectly obtaining the file type. Use $_FILES['file']['type'].

Also, don't use imagecreatefromgif when you are not making a *.gif file.

And a good reason you don't get error messages is because you don't check for errors nearly enough.

Code: Select all

if($src = imagecreatefromgif())

Posted: Sat Feb 17, 2007 8:48 am
by feyd
Never use $_FILES['file']['type']. It is supplied by the submitting agent and not even remotely checked by PHP. For common images, use getimagesize(), otherwise you can use mime_content_type() or other more thorough methods. The file extension has nothing to do with the file type either.

Posted: Sat Feb 17, 2007 8:49 am
by Mordred
psychotomus wrote:link? im not gettign rid of unlink thingie, it keeps minimum files on server stored by user id instead of letting them upload like 100 different avatars.
O, rly? (I want an emoticon for that!) :) Have you actually tried it?

Let's see, you do this:
1. move: tmp_name -> name
2. rename: name -> userId
3. delete: name

It should be obvious that step 3 is unnecessary, because you've just renamed "name" to "userId". It would be nice if you were deleting "userId" before renaming, but well.... Moreover, step 1 and 2 can be shortened to move tmp_name->useId. So the correct thing to do is:
1. if exist userId, delete userId
2. move tmp_name->userId


Forgot to point out your copy/paste problem:

Code: Select all

$w = imagesx($im); //shouldn't it be $src instead of $im?

Posted: Sat Feb 17, 2007 6:50 pm
by psychotomus
heres my modified version that still doesn't work any ideas?

Code: Select all

if ($_FILES["file"]["tmp_name"] <> "")
{
	$pos = strpos($_FILES["file"]["name"],'.');
	$file_type =  strtolower(substr($_FILES["file"]["name"], $pos + 1, strlen($_FILES["file"]["name"]) - $pos +1));
	$filename = $userId . "." . $file_type;
	if ($file_type == "jpg" || $file_typ == "png" || $file_type =="gif")
	{
	
		if ($file_type == "jpg")
		{
			if (move_uploaded_file($_FILES["file"]["tmp_name"], "avatars/" . $filename))
			{
				if(!($src = imagecreatefromjpeg("avatars/" .$filename)))			                                                   echo 'error creating image';
			else
			{
				$eRR = "Could not upload file";
			}
		}
		elseif ($file_type == "png")
		{
			if (move_uploaded_file($_FILES["file"]["tmp_name"], "avatars/" . $filename))
			{
				if(!($src = imagecreatefrompng("avatars/" .$filename)))
					echo 'error creating image';
			else
			{
				$eRR = "Could not upload file";
			}
		}
		else 
		{
			if (move_uploaded_file($_FILES["file"]["tmp_name"], "avatars/" . $filename))
			{
				if(!($src = imagecreatefromgif("avatars/" .$filename)))
					echo 'error creating image';
			else
			{
				$eRR = "Could not upload file";
			}
		}
			
		$w = imagesx($src); 
		$h = imagesy($src);
		
		if ($w > 150 || $h > 150)
			$eRR = 'avatar size can only be 150x150';
		else
			$avatar_url = "http://www.sundaybrew.com/avatars/" . $filename;
	}
	else
	{
		echo 'Only jpg, png and gif file formats allowed';
		exit();
	}	
	
}

Posted: Sat Feb 17, 2007 7:59 pm
by psychotomus
ok. finally got error reporting so i know where te problem is.

Parse error: parse error, unexpected $ in /var/www/vhosts/sundaybrew.com/httpdocs/profileedit.php on line 163

Code: Select all

<tr bgcolor="#000066" background="/images/modtitle-blank.gif"> 
  <td align="left"><div align="center"></div>
	<div align="center"><font color="#FFFFFF"><strong>Profile Updated</strong></font></div></td>
</tr>
<tr>
	<td>
<?

if ($_FILES["file"]["tmp_name"] <> "")
{
	$pos = strpos($_FILES["file"]["name"],'.');
	$file_type =  strtolower(substr($_FILES["file"]["name"], $pos + 1, strlen($_FILES["file"]["name"]) - $pos +1));
	$filename = $userId . "." . $file_type;
	if ($file_type == "jpg" || $file_typ == "png" || $file_type =="gif")
	{
	
		if ($file_type == "jpg")
		{
			if (move_uploaded_file($_FILES["file"]["tmp_name"], "avatars/" . $filename))
			{
				if(!$src = imagecreatefromjpeg("avatars/" .$filename))
					echo 'error creating image';
			}
			else
			{
				$eRR = "Could not upload file";
			}
		}
		elseif ($file_type == "png")
		{
			if (move_uploaded_file($_FILES["file"]["tmp_name"], "avatars/" . $filename))
			{
				if(!$src = imagecreatefrompng("avatars/" .$filename))
					echo 'error creating image';
			else
			{
				$eRR = "Could not upload file";
			}
		}
		else 
		{
			if (move_uploaded_file($_FILES["file"]["tmp_name"], "avatars/" . $filename))
			{
				if(!$src = imagecreatefromgif("avatars/" .$filename))
					echo 'error creating image';
			else
			{
				$eRR = "Could not upload file";
			}
		}
			
		$w = imagesx($src); 
		$h = imagesy($src);
		
		if ($w > 150 || $h > 150)
		{
			$eRR = 'avatar size can only be 150x150';
		}
		else
		{
			$avatar_url = "http://www.sundaybrew.com/avatars/" . $filename;
		}
	}
}
elseif ($_POST['txtAvatarURL'] <> "")
{

	$pos = strpos($_POST['txtAvatarURL'],'.');
	$file_types =  explode(".",$_POST['txtAvatarURL']);
	$file_type = $file_types[count($file_types)-1];
	$file_contents = file_get_contents($_POST['txtAvatarURL']);


	if ($file_type == "jpg")
	{
		$filename = $userID . '.jpg';
		rename("avatars/" . $_POST['txtAvatarURL'],$filename);
		if (!$imagefile = fopen("avatars/" . $filename, "x")) 
		{
			if (!$imagefile = fopen("avatars/" . $filename, "w")) 
			{
				echo 'cant open file';
				exit();
			}
		}
		fwrite($imagefile,$file_contents);
		fclose($imagefile);
		$src = imagecreatefromjpeg("avatars/" . $filename);
	}
	elseif ($file_type == "png")
	{
		$filename = $userID . '.png';
		rename("avatars/" . $_POST['txtAvatarURL'],$filename);
		if (!$imagefile = fopen("avatars/" . $filename, "x")) 
		{
			if (!$imagefile = fopen("avatars/" . $filename, "w")) 
			{
				echo 'cant open file';
				exit();
			}
		}
		fwrite($imagefile,$file_contents);
		fclose($imagefile);

		$src = imagecreatefrompng("avatars/" .$filename);
	}
	else if ($file_type == "gif")
	{
		$filename = $userID . '.gif';
		rename("avatars/" . $_POST['txtAvatarURL'],$filename);
		if (!$imagefile = fopen("avatars/" . $filename, "x")) 
		{
			if (!$imagefile = fopen("avatars/" . $filename, "w")) 
			{
				echo 'cant open file';
				exit();
			}
		}
		fwrite($imagefile,$file_contents);
		fclose($imagefile);

		$src = imagecreatefromgif("avatars/" .$filename);
	}
	else
	{
		$eRR = 'Only jpg, png and gif file formats allowed';
	}
	
	$w = imagesx($src); 
	$h = imagesy($src);
	
	if ($w > 150 || $h > 150)
		$eRR = 'avatar size can only be 150x150';
	else
		$avatar_url = "http://www.sundaybrew.com/avatars/" . $filename;
		
	if ($eRR == "")
		$avatar_url = "http://www.sundaybrew.com/avatars/" . $filename;
}

if ($eRR == "")
{
	$conn=mysql_connect($dbhost, $dbuser, $dbpass);
	mysql_select_db($dbname) or die ("Unable to connect to MySQL");

	$query2="UPDATE t_users SET u_state='" . str_replace("\'", "'", $_POST['txtState']) . "', u_avatar_url='" .$avatar_url . "',u_url='" . str_replace("\'", "'", $_POST['txtUrl']) . "',u_about='" . nl2br(strip_tags(str_replace("\'", "'", $_POST['txtAbout']))) . "',u_interest='" . strip_tags(str_replace("\'", "'", $_POST['txtInterest'])) . "',u_movies='" . strip_tags(str_replace("\'", "'", $_POST['txtMovies'])) . "',u_music='" . strip_tags(str_replace("\'", "'", $_POST['txtMusic'])) . "' WHERE u_nick='".$userNick."'";
		
	//echo $query."<br>";
	$qresult2 = mysql_query($query2) or die (mysql_error());
	echo ("Your profile has been updated. <a href=\"/myaccount\">Click here</a> to go to your account page.");

	mysql_close($conn);
}
else
{
	echo $eRR;
}
?>	
	
	
	
	</td>
</tr> //<-errror is here

Posted: Sat Feb 17, 2007 9:55 pm
by feyd
Count your "{" and "}". You're probably missing one or more of them.

Posted: Sun Feb 18, 2007 4:44 am
by Mordred
At a glance, these two places, may be more:

Code: Select all

elseif ($file_type == "png") 
                { 
                        if (move_uploaded_file($_FILES["file"]["tmp_name"], "avatars/" . $filename)) 
                        { 
                                if(!$src = imagecreatefrompng("avatars/" .$filename)) 
                                        echo 'error creating image'; 
//---------->
                        else 
                        { 
                                $eRR = "Could not upload file"; 
                        } 
                } 
                else 
                { 
                        if (move_uploaded_file($_FILES["file"]["tmp_name"], "avatars/" . $filename)) 
                        { 
                                if(!$src = imagecreatefromgif("avatars/" .$filename)) 
                                        echo 'error creating image'; 
//---------->
                        else 
                        { 
                                $eRR = "Could not upload file"; 
                        } 
                }