chocopi Posted June 25, 2007 Share Posted June 25, 2007 I have an avatar upload field in my form and I am trying to see if it is empty and if so then skip the validation. Here is my code <form name="edit_profile" method="post" action="<?php $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data"> <input type="file" name="avatar" id="avatar" value="" /> <input type="submit" name="submit" id="submit" value="Submit" /> <?php if($_POST) { // validation for avatar // set avatar directory $directory = 'avatars/'; // set max file size $max_file_size = '10000'; // set allowed extensions (for more, just add a comma then 'image/whatever' $allowed_files = array('image/gif'); // get avatar information // get avatar name $file_name = $_FILES['avatar']['name']; // get avatar size $file_size = $_FILES['avatar']['size']; // get avatar type image/whatever $file_type = $_FILES['avatar']['type']; // get file extension of $file_type this is only if there are more than 1 extension allowed list($blank,$file_extension) = explode('image/',$file_type); // check if there is a selection by checking if either the name, type or size is empty if(empty($file_size) or empty($file_name) or empty($file_type)) { echo ""; } else { // check file size is not bigger than max if($file_size > $max_file_size) { // if avatar is too big, give error echo "Your file is larger than 10kb.<br />"; $errors++; } // check the avatar has the correct extension if(!in_array($file_type, $allowed_files)) { // if wrong extension, give error echo "You are not allowed to upload that file type.<br />You are only allowed: .gif<br />"; $errors++; } if($errors == 0) { // get old image $old_file = $directory.''.$page_id.'.gif'; // check if old avatar exists if(file_exists($old_file)) { // if avatar exists delete it unlink($old_file); } // copy avatar to directory copy($_FILES['avatar']['tmp_name'], "".$directory."".$_FILES['avatar']['name']) or die("Your avatar could not be copied correctly"); // rename the file to the user id so it can be pulled in other files easily rename("".$directory."".$_FILES['avatar']['name'], "".$directory."".$page_id.".".$file_extension); } } } ?> I think that is all of the code. The problem is I only want for the validation, and updating to be done if something is in the field. I currently am not using: $_POST['avatar']; and then using validation on it because I was originally but it was not working So I am now trying to check the if either the $file_name, $file_size or $file_type are empty with this line: if(empty($file_size) or empty($file_name) or empty($file_type)) And I know from testing that if the field is left blank then the name and size will be blank and size will be 0 and these are all covered by empty(). But even if I put something into this field it will skip it Any help would be greatly appreciated If you need any more information, just ask, PS sorry for all the tabbing in the code ~ Chocopi Quote Link to comment Share on other sites More sharing options...
Orio Posted June 25, 2007 Share Posted June 25, 2007 Check this page, it will provide you all of the info you need: http://www.php.net/manual/en/features.file-upload.errors.php Orio. Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 25, 2007 Author Share Posted June 25, 2007 kool thanks Orio, I knew there would be something to make it simple I have this if($var['error'] == 4) but what is the $var actually meant to be out of all my other variables ? ~ Chocopi Quote Link to comment Share on other sites More sharing options...
Orio Posted June 25, 2007 Share Posted June 25, 2007 <?php if($_FILES['avatar']['error'] == 4) echo "Error..."; ?> Orio. Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 25, 2007 Author Share Posted June 25, 2007 Cheers thats sorted that but now it isnt uploading the picture ~ Chocopi Quote Link to comment Share on other sites More sharing options...
chocopi Posted June 25, 2007 Author Share Posted June 25, 2007 ok i am stumped, i have been messing with this code all day, and when i finally get it working, the copy stops I have even tried using move_uploaded_file() instead of copy() but to no luck, so if someone can check my code to see if there are any mistakes that i have missed. i know it runs the copy as i have put echos before and after it. Thanks, ~ Chocopi Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.