how to count files from input field in codeigniter? - codeigniter

I am trying to collect number of selected files. My html is like--
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="file" class="img" name="userfile[]" >
<input type="file" id="file" class="img" name="userfile[]" >
<button type="submit" name="add">Add</button>
</form>
In my controller i used
$cp= count($_FILES['userfile']['name']); echo $cp;
which print value 2.. even if i don't select any file.
how do i get the correct value from my selected file field??
N.B: i can select 0 to 2 number of files

First do the:
$this->upload->do_multi_upload("files");
Then your filecount is:
count($_FILES['userfile']['name']);

Use array_filter()
$cp= count(array_filter($_FILES['userfile']['name']));
echo $cp;
If you not selected any file it keeps null in $_FILES['userfile']['name'] so array_filter() removes blank elements from array and you can get exact count of selected files.

Related

How to set default value in thymeleaf th:field

I have a form and I want to set default value in the field below but it's not working.
<span>ID User:</span>
<input type="text" th:value="${session.name}" th:field="*{userid}" th:errorclass="field-error" />
</div>
<div>
<span class="name-in">ID Room:</span>
<input type="text" th:value="${id}" th:field="*{room}" th:errorclass="field-error" />
</div>
I read some topic about this problem and I try to change as given below
th:attr="value = ${session.name}"
But It's still not working. Field ID User is empty. I don't know how to solve this problem.
Although your question contain less information, but i think you want to put default value for all field. If you like to do so change
`<input type="text" th:value="${session.name}" th:field="*{userid}" th:errorclass="field-error" />`
to
<input type="text" name="userid" value="as your wish" th:errorclass="field-error" />
Instead of changing the html, you should instead set the value of *{userid} in your controller. That way you can keep your html the same:
// Controller
modelObject.setUserId(session.name);
// HTML
<input type="text" th:field="*{userid}" th:errorclass="field-error" />

Form validation on radio buttons Codeigniter

I am trying to add validation for radio buttons on a html form in Codeigniter. The form is prepopulated with data from a database, including a list of uploaded documents (this field may be empty if the item has no associated docs). The user can upload a new document, adding it to the existing docs or adding a new doc deleting the current ones.
So I have a text field containing the org doc names and a file upload field for new:
<label for="orgdocs">Documents</label>
<input type="text" id="orgdocs" readonly="readonly" value="<?php echo $fetched_row['pdocs']; ?>" />
<input type="file" id="newdocs" name="newdocs[]" multiple="multiple" />
and radio buttons: (ignore the bad attempt at Spanish names)
<label for="mas"><b>Añada mas</b></label>
<input type="radio" name="docsacion" style="margin-right: 0" <?php if (isset($docsacion) && $docsacion=="mas") echo "checked";?> value="mas" title="Add another document to existing docs"><br />
<label for="otra"><b>Borrar y añada otra</b></label>
<input type="radio" name="docsacion" style="margin-right: 0" <?php if (isset($docsacion) && $docsacion=="otra") echo "checked";?> value="otra" title="Remove all current docs and add new">
I just want to add validation. IF a new document has been selected(newdocs is not empty), dosacion is required.
I have tried:
if(isset($_FILES['newdocs']['name']) && (!empty($_FILES['newdocs']['name'])))
{$this->form_validation->set_rules('docascion','Documentation upload', 'required');}
but this gives the error even if the newdocs field is empty and I´ve no idea why!?
Try this
if(!empty($_FILES['newdocs']['name'])){
$this->form_validation->set_rules('docascion','Documentation upload', 'required');
}

How to upload multiple files at once

I'm trying to upload multiple files using Dropzone.js, but it acts like it's upload one file per one upload.
For example: I select 3 files, click on the OK button, Dropzone shows OK status (successful uploaded), but server side get 3 separate request with one file per each request.
I need to get all files as an array of files, can anybody help with this case?
Code:
<form id="my-awesome-dropzone" class="dropzone custom_bc" action="'.$_SERVER['PHP_SELF'].'">
<input type="hidden" name="action" value="file">
<div class="fallback"><input type="file" name="file" multiple></div>
<input type="hidden" name="object_id" value="'.$_REQUEST['object_id'].'">
<input type="hidden" name="est_id" value="'.$_REQUEST['est_id'].'">
<div class="dz-preview"></div>
</form>
I have also tryed to put [] in the name of <file>, but it doesn't help
<input type="file" name="file[]" multiple>
You need to set uploadMultiple to true.
var myDropzone = new Dropzone(
"#my-awesome-dropzone",
{
url: document.URL, // Set the url
paramName: "file",
uploadMultiple: true
}
);
When setting uploadMultiple to true, Dropzone automatucally concatenates [] to your file name. So you don't need to do that.

Trying to validate Filefield using wtforms

I am trying to capture the filename of an uploaded file via a wtforms FileField
in my validator
def checkfile(form,field):
print form
print field
the 'print forms' statement shows: forms.ticket.TicketForm object at 0x1d2a350
the 'print fields' statement shows: input id="files" name="files" type="file"
if i try to access field.file or field.files i get the error: 'FileField' object has no attribute 'file(s)'
field.data is empty
so how can i access the filename to run a validator?
relevant portion of my class:
class MyForm(wtforms.Form):
files = wtforms.FileField('Files',[checkfile])
which is rendered in my template as:
<form enctype="multipart/form-data" class="form-horizontal" name="add_ticket" action="/ticket/add" method="post">
<input type="hidden" name="_xsrf" value="xxxxxxxxxx"/>
<input class="input-medium" id="files" name="files" type="file">
</form>

How can I validate the files size of uploaded files before upload?

So I need a way of validating the sum total of multiple files being uploaded. I know this needs to be done on the client's side but I am not sure how to implement it. Here is the form I amusing:
<form action="upload.php" method="POST" enctype="multipart/form-data" >
<input type="file" name="image[]" multiple="multiple">
<input type="submit" value="upload">
</form>
You can do it in HTML5, something like:
var file = document.getElementById('fileToUpload').files[0];
// now you can get the size via:
var fileSize = file.size;

Resources