Jump to content

Check if folder exists, if not, create it


TheBrandon

Recommended Posts

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?

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.