April 13, 201016 yr Hey i was wondering how do u prevent a file from uploading if errors accure cause i included upload.php which is for the file upload at the top of my page but i only want it to upload the file if no errors are present during validation but insted it uploads the file even if there is errors heres my code <?php //INCLUDE SITE FUNCTION FILE require_once ("functions.php"); //CONNECT TO MYSQL doDB(); //Include upload.php include ("upload.php"); ?> <script type='text/javascript' href='js/functions.js'></script> <?php //MAIN SITE VARIABLES START $submit = (isset ($_POST['signup'])) ? TRUE : FALSE; $name = (isset ($_POST['name'])) ? $_POST['name'] : FALSE; $user = (isset ($_POST['user'])) ? $_POST['user'] : FALSE; $email = (isset ($_POST['email'])) ? $_POST['email'] : FALSE; $pass = (isset ($_POST['pass'])) ? $_POST['pass'] : FALSE; $pass_cof = (isset ($_POST['pass_cof'])) ? $_POST['pass_cof'] : FALSE; $about_me = (isset ($_POST['about'])) ? $_POST['about'] : FALSE; $email_check = "/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i"; $photo = $file_array["name"]; $filetypes = array('image/jpeg','image/pjpeg'); $ip = $_SERVER['REMOTE_ADDR']; $errors = array(); //MAIN SITE VARIABLES END //SITE VALIDATION START if ($submit) { ; if (!$name) { $errors[] = "Please enter a Firstname Lastname optional!"; } if ($name) { if (strlen($name) < 3) { $errors[] = "Name must be atleast 3 characters long!"; } } if (!$user) { $errors[] = "Please choose a username!"; } if ($user) { if (strlen($user) < 3) { $errors[] = "Username must be atleast 3 characters long!"; } elseif (strlen($user) > 100) { $errors[] = "Username may not be longer then 100 characters!"; } } if (!$email) { $errors[] = "Please enter a valid email address!"; } if ($email) { if (!preg_match($email_check, $email)) { $errors[] = "You have entered an invalid email email format must be in the form of <b>youremail@emaildomain.com</b>"; } } if (!$pass) { $errors[] = "Please choose a password!"; } if ($pass) { if (strlen($pass) < 6) { $errors[] = "Password must be atleast 3 characters long!"; } elseif (strlen($pass) > 100) { $errors[] = "Password may not be longer then 100 characters!"; } elseif ($pass !== $pass_cof) { $errors[] = "Passwords do not match!"; } } if ($about_me) { if (strlen($about_me) < 3) { $errors[] = "Your about me text must not be less then 3 characters!"; } elseif (strlen($about_me) > 6000) { $errors[] = "Your about me text may not be longer then 6000 characters!"; } } if($photo){ if($file_array["size"] > 700000){ $errors[] = "I am sorry your photo size may not be bigger then 700KB!"; } if(!in_array($_FILES['photo']['type'],$filetypes)){ echo "".$file_array["type"].""; $errors[] = "You may only upload a photo with a file type of jpeg or a file type of gif!"; } } if (count($errors) > 0) { foreach ($errors AS $error) { echo "• $error <br />"; } echo "<p><a href='#' onclick=\"history.go(-1);return false;\">Go back to fix errors</a></p>"; } else { //IF NO ERRORS INSERT INTO DATABASE $sql = "INSERT INTO e_users (`name`,`user`,`email`,`about_me`,`photo`,`ip`,`pass`,`date_joined`,`banned`,`admin`,`a_activated`) VALUES('" . $name . "','" . $user . "','" . $email . "','" . $about_me . "','" . $photo . "','" . $ip . "','" . md5($pass) . "',now(),'','','') "; $rel = mysqli_query($conn, $sql) or die(mysqli_error($conn)); } } //SITE VALIDATION END /* Test if form was not submitted if form was not submitted show signup form otherwise don't show the signup form */ if (!$submit) { //SHOW SIGNUP FORM START echo "<div id='register'>"; echo "Already an E-articles member <a href=''>Login Here</a>"; echo "<form method='POST' name='r' enctype='multipart/form-data' action='" . $_SERVER['PHP_SELF'] . "'>"; echo "<p><label for='name'>Name:</label><input type='text' id='name' class='input' value='" . $_REQUEST['name'] . "' name='name' size='40' /></p>"; echo "<p><label for='user'>Username:</label><input type='text' id='user' class='input' value='" . $_REQUEST['user'] . "' name='user' size='40' /></p>"; echo "<p><label for='email'>E-mail:</label><input type='text' id='email' class='input' value='" . $_REQUEST['email'] . "' name='email' size='40' /></p>"; echo "<p><label for='pass'>Password:</label><input type='password' id='pass' class='input' name='pass' size='40' /></p>"; echo "<p><label for='pass_cof'>Confirm Password:</label><input type='password' id='pass_cof' class='input' name='pass_cof' size='40' /></p>"; echo "<p><input type='hidden' name='MAX_FILE_SIZE' value='900000' /></p>"; echo "<p><label for='photo'>Upload A Photo:</label><input type='file' id='photo' class='input' value='" . $_REQUEST['photo'] . "' name='photo' size='40' /></p>"; echo "<p><label for='about'>About You:</label><textarea id='about' class='input' name='about' rows='4' cols='40' >" . $_REQUEST['name'] . "</textarea></p>"; echo "<p><input type='submit' name='signup' value='Create Account' /></p>"; echo "</form>"; echo "</div>"; //SHOW SIGNUP FORM END } ?> Thanks in advance
April 13, 201016 yr Author Simply put your upload code right after your 'IF NO ERRORS INSERT INTO DB'. I'd personally also be adding in something along the lines of: if(!is_writable(YOUR_IMAGE_DIR_UPLOAD_PATH)) die('Fatal Error: Cannot upload to the specified directory, please CHMOD it to 777.'); Would also check the file size of the upload too. Also, am not sure this is, but using the image/jpeg etc. format doesn't always work for me on all hosts. I always add in the normal extensions too, like: $allowed_filetypes = array('.jpg','.gif','.png', '.JPEG', '.JPG', '.GIF', '.PNG'); Have no idea why this is, but could be a PHP setting. Yea i did add the upload code there but the prob is its below the errors so if i use something like this above the upload code if(!in_array($_FILES['photo']['type'],$filetypes)){ echo "".$file_array["type"].""; $errors[] = "You may only upload a photo with a file type of jpeg or a file type of gif!"; } It wont work any suggestions to fix this will be most helpful thank u
April 13, 201016 yr Author I always get the file extension of the upload: $ext = substr($_FILES['myFile']['name'], strpos($_FILES['myFile']['name'],'.'), strlen($_FILES['myFile']['name'])-1); and then check: if ( (in_array($ext, $allowed_filetypes)) ) { Yea but the include include ("upload.php"); will be included below if ( (in_array($ext, $allowed_filetypes)) ) { Inwhich it wont check for the filetype if my upload file is included below the above line
April 13, 201016 yr Author okay, if you have to include you upload code at the top, try something along the lines of: //your included file here if (form submitted) { //set a second var here $uploadErrors = array(); //do upload checking if (NOT is valid type) { $uploadErrors[] = 'wrong type'; } elseif (file is too big) { $uploadErrors[] = 'too big'; } //if no errors in the array continue to check the form for errors if (NOT upload errors) { //all your code from above to check for input errors } else { //display upload erros } if (no upload errors AND no input errors) { //upload the actual file, do db stuff, email etc. (assume you have some sort of upload wrapper function in your upload.php } } //code to display your form EDIT: actually, just read your post back, exactly what code is in upload.php ? Heres the code in upload.php $file_dir = "photos/"; foreach($_FILES as $file_name => $file_array) { if (is_uploaded_file($file_array["tmp_name"])) { move_uploaded_file($file_array["tmp_name"], "$file_dir/".$file_array["name"]) or die ("Couldn't copy"); } }
Create an account or sign in to comment