Reputation: 73
Hi i am trying to restrict the file uploading if the file size exceeds the particular size but my problem is if the file size exceeds the maximum size i didn't get any information about the file in the php server my code in the HTML is as follows please help me why i didn't get the information.
<?php echo $form->create('Add', array('id'=>'addFile', 'enctype'=>'multipart/form-data', 'url'=>'/content/upload?path='.$contentItem['url_path']),array(
'inputDefaults' => array('label' => false))); ?>
<div class="_100 formText">
<label>Section : </label>
<input type="input" value="<?php echo (!empty($contentItem['display_text'])) ? $contentItem['display_text'] : $contentItem['parent_disp_txt'] ;?>" name="data[Add][section]" readonly="readonly"/>
</div>
<div class="clear" style="height: 5px;"></div>
<div class="_100 formText">
<label>File Name : </label>
<input type="input" value="" name="data[Add][file]" class="filePath"/>
</div>
<div class="clear" style="height: 5px;"></div>
<div class="_99 formText">
<label>File Description : </label>
<textarea rows="3" cols="" name="data[Add][description]" class="description" style="resize:none;"></textarea>
</div>
<div class="clear" style="height: 15px;"></div>
<div class="_99 formText">
<label>Select File to Upload: </label>
<input type="file" name="data[Add][upload]" class="filesToUpload" id="file"/>
</div>
<?php echo $html->tags['formend'];?>
Upvotes: 2
Views: 23481
Reputation: 2437
if(isset($_FILES['uploaded_file'])) {
$errors = array();
$maxsize = 2097152;
$acceptable = array(
'application/pdf',
'image/jpeg',
'image/jpg',
'image/gif',
'image/png'
);
if(($_FILES['uploaded_file']['size'] >= $maxsize) || ($_FILES["uploaded_file"]["size"] == 0)) {
$errors[] = 'File too large. File must be less than 2 megabytes.';
}
if(!in_array($_FILES['uploaded_file']['type'], $acceptable) && (!empty($_FILES["uploaded_file"]["type"]))) {
$errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
}
if(count($errors) === 0) {
move_uploaded_file($_FILES['uploaded_file']['tmpname'], '/store/to/location.file');
} else {
foreach($errors as $error) {
echo '<script>alert("'.$error.'");</script>';
}
die(); //Ensure no more processing is done
}
}
Try this code.
Upvotes: 4