PDA

View Full Version : browse file required field


davidmerson
03-27-2008, 06:31 PM
hi, im hoping someone will be able to help me with this problem.

i have got the code to make a text field on an html form a required field, but i was wondering how to make a "browse file"box a required field as well? the code i have is shown below





<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="2" cellspacing="5" class="box">
<tr>
<td>poster name:</td>
<td><input type="text" SIZE="30" name="poster_name" /></td>
</tr>
<tr>
<td>poster Description:</td>
<td><textarea name="poster_description" cols="60" rows="4"></textarea></td>
</tr>
<tr>
<td width="200">poster Dimensions:</td>
<td><input type="text" name="poster_dimensions" /></td>
</tr>
<tr>
<td width="200">poster Price unstretched:</td>
<td><input type="text" name="poster_price" /></td>
</tr>
<tr>
<td width="200">poster Price stretched:</td>
<td><input type="text" name="poster_sprice" /></td>
</tr>
<tr>
<td width="200">poster Price stretched and framed:</td>
<td><input type="text" name="poster_sfprice" /></td>
</tr>
<tr>
<td width="200">poster Price high quality paper:</td>
<td><input type="text" name="poster_pprice" /></td>
</tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input name="userfile" type="file" id="userfile">
</td>
<tr>

<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload all "></td>
</tr>
</table>
</form>


</body>
</html>

<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}

include 'config.php';
include 'opendb.php';




$errmsg = "";
if (!isset($_POST['poster_name']) || empty($_POST['poster_name'])) $errmsg .= "<p>poster name not entered";
if (!isset($_POST['poster_description']) || empty($_POST['poster_description'])) $errmsg .= "<p>poster description not entered";
if (!isset($_POST['poster_dimensions']) || empty($_POST['poster_dimensions'])) $errmsg .= "<p>poster dimensions not entered";
if (!isset($_POST['poster_price']) || empty($_POST['poster_price'])) $errmsg .= "<p>unstretched canvas price not entered";
if (!isset($_POST['poster_sprice']) || empty($_POST['poster_sprice'])) $errmsg .= "<p>stretched canvas price not entered";
if (!isset($_POST['poster_sfprice']) || empty($_POST['poster_sfprice'])) $errmsg .= "<p>stretched and framed canvas price not entered";
if (!isset($_POST['poster_pprice']) || empty($_POST['poster_pprice'])) $errmsg .= "<p>high quality paper price not entered";

if (!$errmsg == "") {
echo "<td>" . $errmsg ."</td><br /><br />";
echo "Please re-enter the information";
exit;
} else {


$query = "INSERT INTO film_posters (name, size, type, content, poster_name, poster_description, poster_dimensions, poster_price, poster_sprice, poster_sfprice, poster_pprice) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content','$_POST[poster_name]','$_POST[poster_description]','$_POST[poster_dimensions]','$_POST[poster_price]','$_POST[poster_sprice]','$_POST[poster_sfprice]','$_POST[poster_pprice]')";

mysql_query($query) or die('Error, query failed');
include 'closedb.php';

echo "<br>File $fileName uploaded<br>";
}
}
?>

any help on this issue would be really appreciated. thanks

David

Fumigator
03-27-2008, 09:43 PM
Simply check the $_FILES['userfile']['name'] variable to see if it's not set or empty, like you are doing with the other fields. You may also want to validate the actual file uploaded to make sure it's the type of file you expect or a valid file at all.

Just an FYI on the empty() function-- it will return "true" if the value is 0, so if (for example) you ever have a situation where any of your four price fields can legitimately be 0, you'll need to change your validation to allow 0.

Fumigator
03-27-2008, 09:46 PM
Let me change my answer a bit. You do not have a call to the move_uploaded_file() function that I can see, which you must have in order for the uploaded file to be retained. Your validation of the uploaded file should be central around this function-- it will return error messages if the file is invalid.

Also you should be checking $_FILES['userfile']['error'] for errors as well.

More info:

http://us.php.net/manual/en/features.file-upload.php