Jump to content

User Avatar not uploading even tho its correct image type (.jpg/.png)


Russia

Recommended Posts

Hey guys I have this script that edits a users profile, but the problem is that it doesnt want to upload it to the server and keeps saying.

 

Files must be either JPEG, GIF, or PNG and less than 10,000 kb

 



<?php
if(empty($_GET['id']) ) { 
echo 'Category not specified'; 
} else {




mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("chat");
$result = mysql_query("SELECT * FROM users WHERE user_id = '$_GET[id]'");
$query = mysql_fetch_array($result);


if (isset($_POST['submit'])) { 






$target = "mainnewsimg/"; 
$target = $target . basename( $_FILES['photo']['name']); 
  
      // Set global variables to easier names
  $pic=($_FILES['photo']['name']); 


  
  
  
 if (($_FILES["photo"]["type"] == "image/gif")
  || ($_FILES["photo"]["type"] == "image/jpeg")
  || ($_FILES["photo"]["type"] == "image/png" )
  && ($_FILES["photo"]["size"] < 10000))
  { 
   if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 


    mysql_query("UPDATE users SET 
			level ='". $_POST['rank'] ."',
			email='". $_POST['email'] ."',
			fname='". $_POST['fname'] ."',
			lname='". $_POST['lname'] ."',
			avatar='$pic'
			WHERE user_id='". $_GET['id'] ."' "); 

	echo "user updated";




			  }
	  else
  {
  echo "file hasent been moved to uploads";
  }
	  }
	  else
  {
  echo "Files must be either JPEG, GIF, or PNG and less than 10,000 kb";
  }



//mysql_query("UPDATE Persons SET level = '36'WHERE user_id = '$_GET[id]'");


}

	?>

	<?php echo $query['username']; ?>
<form method="post" action="">
<table>
		<tbody>

		<tr><td class="first"></td>
		<td><?php echo $query['user_id']; ?></td></tr>

		<tr><td class="first">First Name</td>
		<td><input type="text" name="fname" value="<?php echo $query['fname']; ?>" ></td></tr>


		<tr><td class="first">Last Name</td>
		<td><input type="text" name="lname" value="<?php echo $query['lname']; ?>" ></td></tr>

		<tr><td class="first">Email</td>
		<td><input type="text" name="email" value="<?php echo $query['email']; ?>" ></td></tr>

		<tr><td class="first">Rank</td>
		<td><select name="rank">
<option value="0" <?php if($query['level']=="0") { echo "selected"; }?>>Unactivated</option> 
<option value="1" <?php if($query['level']=="1") { echo "selected"; }?>>Banned</option> 
<option value="2" <?php if($query['level']=="2") { echo "selected"; }?>>Regular User</option> 
<option value="3" <?php if($query['level']=="3") { echo "selected"; }?>>Donator</option> 
<option value="4" <?php if($query['level']=="4") { echo "selected"; }?>>Moderator</option> 
<option value="5" <?php if($query['level']=="5") { echo "selected"; }?>>Administrator</option> 
<option value="6" <?php if($query['level']=="6") { echo "selected"; }?>>Owner</option> 
</select>
		</td></tr>


		<tr><td class="first">User 	Avatar</td>
		<td><input type="file" name="photo"></td></tr>

		<tr><td class="first">Joined on</td>
		<td><input type="text" name="join" value="<?php echo date('d-F-Y',($query['join_date'])); ?>" disabled="disabled"></td></tr>
		<tr><td class="first">Last Access</td>
		<td><input type="text" name="access" value="" disabled="disabled"></td></tr>



		<tr><td></td>
		<td>
		<input type="submit" name="submit" value="Edit User">
		</td></tr>
		</tbody></table>


</form>




<?php } ?>

Link to comment
Share on other sites

By combining the type/size validation into one statement, with one error message, you will never know why your test is failing.

 

You need perform each validation check separately so that each one has a separate and distinct message and you should show the value that failed the test as part of the error message. Show the actual $_FILES["photo"]["type"] value so that you can see what is wrong with it.

 

And by looking at your form code, your upload isn't working anyway because you don't have a proper enctype= attribute in your form tag. Doesn't anybody check the basic requirements before doing anything?

Link to comment
Share on other sites

Okay thanks, also one more thing, lets say the person when he is updating his profile doesnt pick a new avatar, how would I do it so the form still goes through. Because if I dont select a avatar the form doesnt go through and it gives the message:

 

Files must be either JPEG, GIF, or PNG and less than 10,000 kb

 

 
<?php
if(empty($_GET['id']) ) { 
echo 'Category not specified'; 
} else {


mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("chat");
$result = mysql_query("SELECT * FROM users WHERE user_id = '$_GET[id]'");
$query = mysql_fetch_array($result);


if (isset($_POST['submit'])) { 




$target = "mainnewsimg/"; 
$target = $target . basename( $_FILES['photo']['name']); 
  
      // Set global variables to easier names
  $pic=($_FILES['photo']['name']); 

  
  
  
 if (($_FILES["photo"]["type"] == "image/gif")
  || ($_FILES["photo"]["type"] == "image/jpeg")
  || ($_FILES["photo"]["type"] == "image/png" )
  && ($_FILES["photo"]["size"] < 10000000))
  {

$tmpName = $_FILES['photo']['tmp_name'];
        
list($width, $height, $type, $attr) = getimagesize($tmpName);

if($width <= 90 && $height <= 90)
{



  
   if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 


    mysql_query("UPDATE users SET 
			level ='". $_POST['rank'] ."',
			email='". $_POST['email'] ."',
			fname='". $_POST['fname'] ."',
			lname='". $_POST['lname'] ."',
			avatar='$pic'
			WHERE user_id='". $_GET['id'] ."' "); 

	echo "user updated";



			  }
	  else
  {
  echo "file hasent been moved to uploads";
  }
  
  }
  else
  {
  echo "size too big";
  }
  
	  }
	  else
  {
  echo "Files must be either JPEG, GIF, or PNG and less than 10,000 kb";
  }


//mysql_query("UPDATE Persons SET level = '36'WHERE user_id = '$_GET[id]'");

}

	?>

	<?php echo $query['username']; ?>
<form enctype="multipart/form-data" method="post" action="">
<table>
		<tbody>

		<tr><td class="first"></td>
		<td><?php echo $query['user_id']; ?></td></tr>

		<tr><td class="first">First Name</td>
		<td><input type="text" name="fname" value="<?php echo $query['fname']; ?>" ></td></tr>

		<tr><td class="first">Last Name</td>
		<td><input type="text" name="lname" value="<?php echo $query['lname']; ?>" ></td></tr>

		<tr><td class="first">Email</td>
		<td><input type="text" name="email" value="<?php echo $query['email']; ?>" ></td></tr>

		<tr><td class="first">Rank</td>
		<td><select name="rank">
<option value="0" <?php if($query['level']=="0") { echo "selected"; }?>>Unactivated</option> 
<option value="1" <?php if($query['level']=="1") { echo "selected"; }?>>Banned</option> 
<option value="2" <?php if($query['level']=="2") { echo "selected"; }?>>Regular User</option> 
<option value="3" <?php if($query['level']=="3") { echo "selected"; }?>>Donator</option> 
<option value="4" <?php if($query['level']=="4") { echo "selected"; }?>>Moderator</option> 
<option value="5" <?php if($query['level']=="5") { echo "selected"; }?>>Administrator</option> 
<option value="6" <?php if($query['level']=="6") { echo "selected"; }?>>Owner</option> 
</select>
		</td></tr>

					<tr><td class="first">Current Avatar</td>
		<td>
		<?php echo "<img width='90' height='90' src=mainnewsimg/".$query['avatar'] .">"; ?>	   
		</td></tr>



		<tr><td class="first">User 	Avatar</td>
		<td><input type="file" name="photo"></td></tr>

		<tr><td class="first">Joined on</td>
		<td><input type="text" name="join" value="<?php echo date('d-F-Y',($query['join_date'])); ?>" disabled="disabled"></td></tr>
		<tr><td class="first">Last Access</td>
		<td><input type="text" name="access" value="" disabled="disabled"></td></tr>


		<tr><td></td>
		<td>
		<input type="submit" name="submit" value="Edit User">
		</td></tr>
		</tbody></table>

</form>


<?php } ?>


I want the form to still update but like setting the value to the current image if a user didnt pick a new avatar.

 

Link to comment
Share on other sites

Be aware that you need to include jpeg and png in the following format for IE:

$_FILES["photo"]["type"] == "image/pjpeg"
$_FILES["photo"]["type"] == "image/x-png"

 

IIRC Safari passes jpeg as:

$_FILES["photo"]["type"] == "image/jpg"

 

Might want to include that as well.

Link to comment
Share on other sites

Thanks one more thing, since the updating of a avatar is optional, how do I do it, if the person does set a new avatar it keeps the old one? Because in mine, it looks like its needed, because it wont update if a image isnt found.

 

How do I do it so its like optional?

 

Here is my current code.


<?php
if(empty($_GET['id']) ) { 
echo 'Category not specified'; 
} else {


mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("chat");
$result = mysql_query("SELECT * FROM users WHERE user_id = '$_GET[id]'");
$query = mysql_fetch_array($result);


if (isset($_POST['submit'])) { 




$target = "mainnewsimg/"; 
$target = $target . basename( $_FILES['photo']['name']); 
  
      // Set global variables to easier names
  $pic=($_FILES['photo']['name']); 

  
  
  
 if (($_FILES["photo"]["type"] == "image/gif")
  || ($_FILES["photo"]["type"] == "image/jpeg")
  || ($_FILES["photo"]["type"] == "image/png" )
  && ($_FILES["photo"]["size"] < 10000000))
  {

$tmpName = $_FILES['photo']['tmp_name'];
        
list($width, $height, $type, $attr) = getimagesize($tmpName);

if($width <= 90 && $height <= 90)
{



  
   if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 


    mysql_query("UPDATE users SET 
			level ='". $_POST['rank'] ."',
			email='". $_POST['email'] ."',
			fname='". $_POST['fname'] ."',
			lname='". $_POST['lname'] ."',
			avatar='$pic'
			WHERE user_id='". $_GET['id'] ."' "); 

	echo "user updated";



			  }
	  else
  {
  echo "file hasent been moved to uploads";
  }
  
  }
  else
  {
  echo "size too big";
  }
  
	  }
	  else
  {
  echo "Files must be either JPEG, GIF, or PNG and less than 10,000 kb";
  }


//mysql_query("UPDATE Persons SET level = '36'WHERE user_id = '$_GET[id]'");

}

	?>

	<?php echo $query['username']; ?>
<form enctype="multipart/form-data" method="post" action="">
<table>
		<tbody>

		<tr><td class="first"></td>
		<td><?php echo $query['user_id']; ?></td></tr>

		<tr><td class="first">First Name</td>
		<td><input type="text" name="fname" value="<?php echo $query['fname']; ?>" ></td></tr>

		<tr><td class="first">Last Name</td>
		<td><input type="text" name="lname" value="<?php echo $query['lname']; ?>" ></td></tr>

		<tr><td class="first">Email</td>
		<td><input type="text" name="email" value="<?php echo $query['email']; ?>" ></td></tr>

		<tr><td class="first">Rank</td>
		<td><select name="rank">
<option value="0" <?php if($query['level']=="0") { echo "selected"; }?>>Unactivated</option> 
<option value="1" <?php if($query['level']=="1") { echo "selected"; }?>>Banned</option> 
<option value="2" <?php if($query['level']=="2") { echo "selected"; }?>>Regular User</option> 
<option value="3" <?php if($query['level']=="3") { echo "selected"; }?>>Donator</option> 
<option value="4" <?php if($query['level']=="4") { echo "selected"; }?>>Moderator</option> 
<option value="5" <?php if($query['level']=="5") { echo "selected"; }?>>Administrator</option> 
<option value="6" <?php if($query['level']=="6") { echo "selected"; }?>>Owner</option> 
</select>
		</td></tr>

					<tr><td class="first">Current Avatar</td>
		<td>
		<?php echo "<img width='90' height='90' src=mainnewsimg/".$query['avatar'] .">"; ?>	   
		</td></tr>



		<tr><td class="first">User 	Avatar</td>
		<td><input type="file" name="photo"></td></tr>

		<tr><td class="first">Joined on</td>
		<td><input type="text" name="join" value="<?php echo date('d-F-Y',($query['join_date'])); ?>" disabled="disabled"></td></tr>
		<tr><td class="first">Last Access</td>
		<td><input type="text" name="access" value="" disabled="disabled"></td></tr>


		<tr><td></td>
		<td>
		<input type="submit" name="submit" value="Edit User">
		</td></tr>
		</tbody></table>

</form>


<?php } ?>



Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.