Which one is a best file up-loader extension for Yii1.x framework? - yii1.x

I am using coco file up-loader extension in my Yii1.x framework but its not working properly.
1: Like if I want to delete a uploaded file name from status area its not working.
2: I am not able to make a limit to upload files if I want to edit a file.
3: Its have also UI issues.
I am using coco like this.
widget('ext.coco.CocoWidget'
,array(
'id'=>'cocowidget1',
'onCompleted'=>'function(id,filename,jsoninfo){ }',
'onCancelled'=>'function(id,filename){ alert("cancelled"); }',
'onMessage'=>'function(m){ alert(m); }',
'allowedExtensions'=>array('jpeg','jpg','gif','png'), // server-side mime-type validated
'sizeLimit'=>2000000, // limit in server-side and in client-side
'uploadDir' => 'assets/', // coco will #mkdir it
// this arguments are used to send a notification
// on a specific class when a new file is uploaded,
'receptorClassName'=>'application.models.MyModel',
'methodName'=>'onFileUploaded',
'userdata'=>$model->primaryKey,
// controls how many files must be uploaded
'maxUploads'=>3, // defaults to -1 (unlimited)
'maxUploadsReachMessage'=>'No more files allowed', // if empty, no message is shown
// controls how many files the can select (not upload, for uploads see also: maxUploads)
'multipleFileSelection'=>true, // true or false, defaults: true
));
?>

This issue got when we are going to update a gallery images, But now we resolved that issue using below point.
1: 'onCompleted'=>'function(id,filename,jsoninfo){ }', we can call a custom AJAX call where we can delete our files.
2: If we deleted any uploaded items, then we can set here a hidden fields where we can set counters for uploaded files and on select another files to upload we able to show alert for how many files you can upload.

Related

How can I restrict accepted media types in the media field on Strapi

As far as I see there is only one type of media field which can hold every type of media (image, video, pdf etc.) Is there a way to restrict the media type, so that the field only accepts images and no other filetypes? Because when I allow the field to hold multiple files the array will hold images, video and files. I search for a solution how I can restrict the data types for this field.
You can deal with that by updating the upload function of the upload plugin.
Please for that you will have to check how the extensions folder work.
📚Documentation here: https://strapi.io/documentation/3.0.0-beta.x/concepts/concepts.html#extensions
And after you will have to find the path of the file you want to update.
It will be this one https://github.com/strapi/strapi/blob/master/packages/strapi-plugin-upload/controllers/Upload.js
So you will have to create an empty file at the same path in the extension folder.
It will be ./extensions/upload/folder/Upload.js
module.exports = {
};
In this file you will have to create the function you want to update/override.
It will be this function https://github.com/strapi/strapi/blob/master/packages/strapi-plugin-upload/controllers/Upload.js#L12
So you have to copy this function and past it in your extensions file.
When it's done you can modify the function as you want.
Here in the function https://github.com/strapi/strapi/blob/master/packages/strapi-plugin-upload/controllers/Upload.js#L27 you can see the var files contain all your files, you can simply add your code to test the type of your files.

Laravel PDF generation with Graph and send it with Email

I tried to find it on StackOverflow and also tried to google it, but could not find any relevant answer.
I want to send monthly reports to the user of Laravel application with a PDF that contain a graph/chart.
This is what that is already done
Created a route, lets say
Route::get('/print/', 'PrintController#report');
In printController created a report function that is getting all the necessary data from the DB and returning a view with user data
return view::make('monthly_report', $user_data);
In monthly_report view, get all the user data, show the view and create a chart with the data. The chart is created with Charts.js. it is in a canvas.
Send the generated chart as image to the server with Ajax. For example
var canvas = document.getElementById("myChart"), // Get your canvas
imgdata = canvas.toDataURL();
file_name = "<?php echo $chart_file_name; ?>"; //created with userId and date
//send it to server
$.ajax({
method: "POST",
url: "save",
data: {
data: imgdata,
file_name: file_name,
_token: token,
}
});
Created a save route
Route::post('print/save', 'PrintController#saveChart');
In print controller, saveChart function, save the chart
$data = base64_decode($data);
//save chart on server
file_put_contents("Charts/".$fileName, $data);
Then create a PDF report by using another view monthly_report2, that is also in saveChart function. The view monthly_report2 does not use any JavaScript and use the chart image that was generated by monthly_report, in number 6.
$pdf = \PDF::loadView('monthly_report2', $cll_data);
file_put_contents("reports/".$pdf_name, $pdf->output());
It save the generated PDF on server. So far so good.
To send these PDF reports to the users by e-mail, I will created a schedule/crone job that will be run on a specific date, monthly and will send the e-mails with PDF reports as attachments.
I skip some details for clarity, please ask, if you need more information.
Thank you for reading so far, now I have two questions
The way I am doing is good or it can be improve?
We want all this process automatically (generating reports and sending by email). To generate the PDF's, monthly_report view must be loaded? so that it generate the Chart and send it to the server. Can we schedule it also, so that it generate the pdf reports automatically?, If no, is there any other way to do it?
Kind of a big question, but I'll try to answer
I think it's good. I'm not a big fan of using JavaScript to create charts, but that's me. You obviously know what you're doing and PDF generation is in my experience a "If it works, please don't break it" functionality.
I think this might be more difficult. Since you're using JavaScript to create charts, you need some kind of engine (NodeJS comes to mind) to parse the JavaScript and actually create your charts without opening a browser and doing it manually. (This is why I don't like using JavaScript to create charts). You could take a look at tutorials like this one to get an idea of how to render your charts serverside.
After that, you can take a look at the Laravel task scheduler (provided you're on Laravel 5, a community package exists for Laravel 4). You can schedule existing and custom-made commands to be executed. In pseudo-code, a PDF generation command could look like this:
public function createAndSendCharts() {
// 1. Get necessary user data
// 2. Create your charts
// 3. Save your charts
// 4. Create email with charts
// 5. Send your email
}
You could then add that function to your Task Scheduler
$schedule->command('send:charts')
->monthly();
Hope this was of some help. All in all, you're doing fine, but the choice for ChartJS has some consequences if you want to automate the whole process. Nothing really special, tons of tutorials exist for this situation :)

Symfony 2: File constrains on a AJAX file upload

I have created normal Symfony form. However I require additional files to be uploaded (photos, certificates) which I handle by AJAX and are not retrieved via Symfony form.
Instead file upload is handled in separate controllers actions via file bag.
I would like to limit the maximum file size (only for this action).
If the upload was handled by classic form I would use the file constraint
but this way I do not now how to apply it.
I check mime-type and successful upload by methods on UploadedFile, however the documentation for method for retrieving the size is in a way discouraging (UploadedFile#method_getClientSize).
Is there a way how to use FileValidator/File constraint without the form?
I have got inspired by actual implementation of FileValidator:
$file = $request->files->get('file');
$path = $file->getPathname();
$size = round(filesize($path) / 1000000, 2);
$limit = (int) $constraint->maxSize;
if ($size > $limit) {
//Add violation
}
This solution works and is probably better then use getClientSize().

Why are images uploaded through the admin panel of my custom module, having the file name of Array?

Here is my BrandController.php
https://gist.github.com/a958926883b9e7cc68f7#file-brandcontroller-php-L53
I've gone through all my files of my custom module, and compared them to the one given from the custom module maker, and I couldn't find much differences.
Are you attempting to upload multiple files? If you're using multiple fileupload elements with the same name you'll get an array of items.
So when the following line is called,
//this way the name is saved in DB
$data['filename'] = $_FILES['filename']['name'];
It will have the value
["name"]=>array(2) {
[0]=>string(9)"file0.txt"
[1]=>string(9)"file1.txt"
}
you'll need to update the code to loop through each $_FILES['filename']['name'] and upload and save the files separately.
You may unknowingly uploaded multiple files. If you that is not your intention, you may check your in your HTML and check the name attribute of the tag. It must not be an array (like this).
<input type="file" name="my_files[]" />
If you only see Array() in your database, it means you are indeed uploading a multiple files. You can process them by using loops.
If you are really sure that you are uploading 1 image, you may follow #Palanikumar's suggestion. Use a print_r() and display the $_FILES and paste it here. IF you don't want to use that, You can use
json_encode($the-data-you-are-going-to-insert-to-the-database);
If you don't know where to put the print_r() function, you may put it after line 56 of this file.
https://gist.github.com/desbest/a958926883b9e7cc68f7#file-brandcontroller-php-L53
if(isset($_FILES['filename']['name']) && $_FILES['filename']['name'] != '') {
print_r($_FILES);
die;
If saveAction() is being called inside an ajax function you need to log the ajax response. Assuming you are using jquery..
$ajaxResponse = $.POST({...});
console.log($ajaxResponse.responseText);
Then, you you can view it inside a browser's console. If nothing appears, you may use a non-async request
$ajaxResponse = $.POST({
// your options,
// your another option,
async: FALSE
});
Usually file upload will return in array format. So that each uploaded file will have the information like name, type, size, temporary name, error. You can get the file information using print function (print_r($_FILES)). So if you want to display name of the file you have to use something like this $_FILES['filename']['name']
Use print function and debugging tool then save file information using loops.
For more info please check here.
You aren't setting the enctype of the form so the image will never be sent. updated the code to
$form = new Varien_Data_Form(array( 'enctype' => 'multipart/form-data'));

drupal_execute populating image field

How to populate image field value with drupal_execute.
for ex my content type (test) has two additional fields
1. photo (image filed),
2. phid (text field)
for phid $form_state['values']['field_phid'][0]['value'] ='14'; . how to populate photo which is image field type
If the file is already uploaded to Drupal and has a file ID (fid) then you can just do
$form_state['values']['field_image_filed'][0]['fid'] = 17; //where 17 is the Drupal file ID of the file you want input
If the file isn't already uploaded it's a lot trickier. You'll first need to programmatically create the file. I can't walk you through it off-hand but a good place to look for a template as to how it should be done is the file_service_save() function in the Services module's file_service.inc:
http://drupalcode.org/viewvc/drupal/contributions/modules/services/services/file_service/file_service.inc?revision=1.1.2.7.2.3&view=markup&pathrev=DRUPAL-6--2-2
To be clear: I'm not saying you'll use file_service_save() to accomplish the upload, but that that code shows you what needs to be done. It will show you how to save the file to the server using file_save_data(), record the file to the Drupal "files" table, then call hook_file_insert to notify other modules that a file's been saved.
i found the solution as below . i dont know pros and cons but it works fine for me.
$image = "*******/test.jpg";
$field = content_fields('field_img', 'img_test');
$validators = array_merge(filefield_widget_upload_validators($field), imagefield_widget_upload_validators($field));
$files_path = filefield_widget_file_path($field);
$form_state['values']['field_img'][]= field_file_save_file($image, $validators, $files_path, FILE_EXISTS_REPLACE);

Resources