Jump to content

Recommended Posts

It has been a while since I have posted. I need a little help.

I have composed a form script that will upload files. The uploads work great. All required fields but my $_FILE input required !empty() is not working right. In the form I have named the field "file" and in my required !empty($_POST['file']) is the only part that makes my statement false. I really need this field to be required. It is the main portion of the field. Here is my code:

<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
session_start();
//check for  cookie or required fields
	if( isset($_POST['submit'])) {
   		if(( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] )) &&
   		(!empty($_POST['event_title'])&& !empty($_POST['email']) && !empty($_POST['state']) && !empty($_POST['year'])&& !empty($_POST['month'])
   		&& !empty($_POST['day']) && !empty($_POST['file']))){
	// Insert form processing code start here for email, database and thank you
    @copy($_FILES[file][tmp_name],"/home/arscnaor/public_html/activities/uploads/".$_FILES[file][name]);
	$event = $_POST['event_title'];
	$email = $_POST['email'];
	$location = $_POST['state'];
	$month = $_POST['month'];
	$day = $_POST['day'];
	$year = $_POST['year'];
	$date = $year . "-" . $month . "-" . $day;
    $filename = $_FILES['file']['name'];
	$datetime = date("F j, Y, g:i a");
//email code information here
//This emails to the website committee
	mail($to, $subject, $message, $headers);
/*Insert Database Code here*/
//database code is here works good
/*Insert Database Code here*/
//Thank You echoCode 
echo "Thank you for using our Flyer Upload Form.
	<a href=\"http://www.arscna.org/activities/uploads/$filename\" target=\"_blank\">$filename</a><br />
	You may click your back button and then refresh the page if you wish to add<br />
	another flyer or Click <a href=\"http://www.arscna.org/flyers/\">HERE</a> to return to the Flyers Page.";

unset($_SESSION['security_code']);}
	else {
		//This echoes if all required fields are not filled out
		echo "All form fields are Required to send this form.<br />";
		echo "Please click your browser back button and make sure that all fields are filled.";
	}
}
else {
?>
<html>
<form action="newindex.php" enctype="multipart/form-data" method="post" style="margin:0 0 50px 100px;border:solid 1px red;width:500px;">
Upload the Flyer:<input type="file" name="file" size="30" style="background-color:yellow;" >
<input type="submit" name="submit" value="Submit" />
<input type="reset" name="reset" value="Reset" />
</form>
</html>
<?php
}
?>

How do I require a $_FILE() or my type="file" input become required? Can I use the name="file" as a $_POST['file']?

Thanks for any help

Link to comment
https://forums.phpfreaks.com/topic/171363-solved-required-form-field/
Share on other sites

You need to check within the $_FILES array, not the $_POST array.

 

$error_message_stack = 0;
if(isset($_FILES[file][tmp_name])){
    // you know you have a file
}else{
     $error_message_stack = 1;
     // output an error message, repopulate the form, and skip further processing
}
if($error_message_stack == 0){
    // do whatever you want to do with the file
}

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.