I have this image upload script that i got from a web site (here) and i have created a script to deal with the verification of variables:
Code: Select all
$fname = ($_POST['fname']);
$email = ($_POST['email']);
$comment = ($_POST['comment']);
function check_form_text($email) {
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
return false;
}
$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false;
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}
Code: Select all
<html>
<head><title>test page</title
</head>
<body>
<form action="submit3.php" method="post" enctype="multipart/form-data">
<p><h4>File*</h4><input type="file" name="single" size="27px" /></p>
<h5>*max filesize 1mb</h5>
<h5>*only image files accepted (.gif .jpg etc.)</h5>
<p><h4>Name*</h4><input type="text" name="fname" /></p>
<p><h4>Email*</h4><input type="text" name="email" /></p>
<p><h4>Comment*</h4><textarea rows="2" cols="20" name="comment"></textarea></p>
<h5>*all fields are required</h5>
<p>
<input type="hidden" name="form_submitted" value="true" />
<input type="submit" value="Submit" />
</p>
</form>
<?PHP
if (isset($_REQUEST['form_submitted'])) {
require("email.class.php");
if (check_form_text()) {
echo '<p style="color:green">';
echo 'success!';
echo '</p>';
} else {
echo '<p style="color:red">';
echo 'Failure';
echo '</p>';
}
}
?>
</body>
</html>
Code: Select all
if (empty($fname) && empty($comment)){
return FALSE;
}
How can i comine all of them so that they all work together so that if any part fails it display an appropriate message and the rest of the script also fails, ie no file is uploaded? Also if it verifies i want it to do the following:
Code: Select all
$textfile = ($a_variable_eventually . '.txt');
$fh = fopen($textfile, 'w');
$stringData = "Name: $fname \r";
fwrite($fh, $stringData);
$stringData = "Email: $email \r";
fwrite($fh, $stringData);
$stringData = "--------------------------------------------------------- \r";
fwrite($fh, $stringData);
$stringData = "$comment \r";
fwrite($fh, $stringData);
$stringData = "--------------------------------------------------------- \r";
fwrite($fh, $stringData);
fclose($fh);