Jump to content

Strange problem with File Upload Script - testing for existence of file


plastik77

Recommended Posts

Hi, i'm having a strange problem when processing some form data. I just want to validate that required fields have been populated, then, if an image has been uploaded, i want to ensure that a description of the image has been provided (for alt tag purposes) before processing the data. For some reason, when a file hasn't been attached, my script interprets it otherwise and prompts the user for the alt tag. I've not come across this problem before, there's a good chance i've done something idiotic but i just can't see it - any help greatly appreciated.

 

I've just used a simple test: if (isset($_FILES))....but this condition is always true, even when no file has been attached!

 

// Check whether a form has been submitted. If so, carry on
if (isset($_POST['Submit']){

// Validate the entries
$validator = new Validator();
$validator->validateGeneral($_POST['title'],'Article Title');
$validator->validateGeneral($_POST['tagline'],'Tagline');
$validator->validateNumber($_POST['section'],'Section');
$validator->validateGeneral($_POST['thearticle'],'Article');
//if a file has been attached, check for the alt tag first, before processing
if (isset($_FILES)) {
echo "Test!";
$validator->validateGeneral($_POST['description'],'Description');
}
// Check whether the validator found any problems
if ($validator->foundErrors() ){
$error = "<h5>Error! There was a problem with the following fields: <br/>".$validator->listErrors('<br/>')."</h5>"; // Show the errors, with a line between each
}
else{....all the data processing code

 

here is the html form info

<form name="form1" method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h3><?php echo $error;
echo $my_upload->show_error_string(); ?></h3>
<fieldset>
<legend>New Article</legend>
<label>Title:
<input name="title" type="text" id="title"/>
</label>
<label>Tagline:
<input name="tagline" type="text" id="tagline"/>
</label>
<label>Section:
<select name="section" id="section">
<?php	// Generate a drop-down list of sections.

$result = $connector->query('SELECT ID,name FROM cmssections ORDER BY name');
// Get an array containing the results.
// Loop for each item in that array
while ($row = $connector->fetchArray($result)){
	echo '<option value="'.$row['ID'].'">'.$row['name'].'</option>';
}
?>
</select>
</label>
<label>Article:
<textarea name="thearticle" cols="50" rows="6" id="thearticle"></textarea>
</label>
<p>Max. filesize = <?php echo $max_size; ?> bytes.</p>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_size; ?>"/>
<label>
Upload file:
<input type = "file" name="upload" />
</label>
<p>Please enter a description of the image, if attached:</p>
<label>Description:<input type = "text" name = "description" size = "25"/>
</label><br/><br/>
<input type="submit" name="Submit" value="Submit"/>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.