Why wont my file upload?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Why wont my file upload?

Post 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';
			}
		}
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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())
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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?
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post 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();
	}	
	
}
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Count your "{" and "}". You're probably missing one or more of them.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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"; 
                        } 
                }
Post Reply