TheBrandon Posted November 16, 2010 Share Posted November 16, 2010 Hello all, I am working on an upload script for a client. The script generates a random 32 bit string (to use as an ID). I want it to check if that folder exists and, if not, create it and put the files in it. Here is the script I have so far: <?php /******************************************* /* contact_us_process.php /* Author: Brandon Pence (brandonpence@gmail.com) /* /* Desc: This page process and checks the submitted form. /* /* variable meaning /* -------- ------- /* $FieldCheck Instance of the FieldCheck class. /* /******************************************/ session_start(); require ("./inc/include.inc.php"); //include Field Check Class include("classes/FieldCheck.php"); $FieldCheck = new FieldCheck; if(file_exists("uploads/$id")){ echo 'Folder Exists!'; }else{ echo 'Folder does not exist. Create folder.'; } //mkdir("uploads/$id",0777,true); $target_path = "uploads/$id/"; $i = 0; while($i < 3){ $target_path = $target_path . basename( $_FILES['uploadedfile']['name'][$i]); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name'][$i]). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!"; } $i++; } if (!$FieldCheck->checkName($your_name)){ $errors['your_name'] = "Your name is not valid.";} if (!$FieldCheck->isEmpty($your_date)){ $errors['your_date'] = "Your date is not valid.";} if (!$FieldCheck->isEmpty($your_message)){ $errors['your_message'] = "Your message is not valid.";} foreach($_POST as $key=>$value){ $fields[$key] = $value; } //print_r($_POST); //check for error messages if (isset($errors)) { $_SESSION['errors'] = $errors; $_SESSION['fields'] = $fields; $_SESSION['error_msg'] = "Errors found! Please review your entries."; $url = "./testimonials.php?action=errors"; header ("Location: $url"); exit; }else{ // multiple recipients $to = 'email@email.com'; // note the comma // subject $subject = '[Domain Name.com] Testimonials Form Submission'; // message $message = ' Name: '.$your_name.' Date: '.$your_date.' Message: '.$your_message.' '; // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; // Additional headers $headers .= 'To: Example <email@example.com>' . "\r\n"; $headers .= 'From: Example <email@example.com.com>' . "\r\n"; $headers .= 'X-MAILER: PHP'.phpversion(); $headers .= 'Reply-To:email@example.com'; // Mail it $mail = mail($to, $subject, $message); //mail("$to", "$subject", "this is a test message, if you are reading this the email was sent successfully."); //savvy $_SESSION['error_msg'] = "Your email has been sent. Thank you for contacting us."; header( "Location: testimonials.php?action=success" ); } ?> It always outputs folder exists, even though it doesn't. My form is this: function random_string($p_length) { $char_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $char_list .= "abcdefghijklmnopqrstuvwxyz"; $char_list .= "1234567890"; $random = ""; srand((double) microtime() * 1000000); for($i = 0; $i < $p_length; $i++) { $random .= substr($char_list, (rand() % (strlen($char_list))), 1); } return $random; } <form method="POST" action="contact_us_process.php" class="cmxform" enctype="multipart/form-data"> <?php if ($_GET['action'] == 'success' && isset($_SESSION['error_msg'])){ echo '<div class="success">'.$_SESSION['error_msg'].'</div>'; unset($_SESSION['error_msg']); }else{ if(isset($_SESSION['error_msg'])){ echo '<div class="error">'.$_SESSION['error_msg'].'</div>'; unset($_SESSION['error_msg']); } } ?> <fieldset style="margin:20px;width:350px;height:450px;background-color:none;"> <legend>Contact Us</legend> <ol> <li> <label for="your_name">Your Name</label> <input type="text" name="your_name" size="50" class="textbox" <?php if (isset($errors['your_name'])){echo 'class="input_red"';}?> <?php if (isset($fields['your_name'])){echo 'value="'.$fields['your_name'].'"';}?>/> <?php if (isset($errors['your_name'])){ echo '<span class="error_box"> '.$errors['your_name'].'<span class="error-pointer"> </span></span>';}else{?> <span class="hint">Tell us your name! It's nice to meet you!<span class="hint-pointer"> </span></span> <?php } ?> </li> <div class="clear"></div> <li> <label for="your_date">Your Date</label> <input type="text" name="your_date" size="50" class="textbox" <?php if (isset($errors['your_date'])){echo 'class="input_red"';}?> <?php if (isset($fields['your_date'])){echo 'value="'.$fields['your_date'].'"';}?>/> <?php if (isset($errors['your_date'])){ echo '<span class="error_box"> '.$errors['your_date'].'<span class="error-pointer"> </span></span>';}else{?> <span class="hint">Let's keep in touch!<span class="hint-pointer"> </span></span> <?php } ?> </li> <div class="clear"></div> <li> <label for="your_message">Your Message</label> <textarea rows="3" cols="40" name="your_message" class="textarea"<?php if (isset($errors['your_message'])){echo 'class="input_red"';}?>><?php if (isset($fields['your_message'])){echo $fields['your_message'].'</textarea>';}else{echo '</textarea>';}?> <?php if (isset($errors['your_message'])){ echo '<span class="error_box"> '.$errors['your_message'].'<span class="error-pointer"> </span></span>';}else{?> <span class="hint">What's on your mind?<span class="hint-pointer"> </span></span> <?php } ?> </li> <li> <input name="uploadedfile[]" type="file" /> </li> <li> <input name="uploadedfile[]" type="file" /> </li> <li> <input name="uploadedfile[]" type="file" /> </li> <?php $random_string = random_string(32); //This will return a random 10 character string?> <input type="hidden" value="<?php echo $random_string;?>" name="id"/> <input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> <div class="submit_box"> <input type="submit" value="Get in touch with us!" name="submit" class="submit"> </div> </ol> </fieldset> </form> Can anyone help me with the "if it exists, move on, if it doesnt, create folder" part? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted November 16, 2010 Share Posted November 16, 2010 I don't see where $id is defined in that first script, so as long as the uploads/ directory exists, it will return true. You need to assign the value from the $_POST array to the $id variable, unless register_globals = On (which it really shouldn't). Quote Link to comment Share on other sites More sharing options...
TheBrandon Posted November 16, 2010 Author Share Posted November 16, 2010 Gotta love it and hate it at the same time when it's something so simple... Thanks! 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.