yii shows error file not found at given location - export-to-excel

i am trying to export excel file using eexcelview extention in yii
i am having following code with me
public function behaviors(){
return array(
'eexcelview'=>array(
'class'=>'extensions.eexcelview.EExcelBehavior'
),
);
}
and one more
public function actionTest()
{
// Load data
$model = Customer::model()->findAll();
// Export it
$this->toExcel($model, array(
'custid',
'custname'
),
'Test File',
array(
'creator' => 'Zen',
),
'Excel2007' // This is the default value, so you can omit it. You can export to CSV, PDF or HTML too
);
}
}
as given on toexcel extension documentation
i am having eecxcelview folder and EExcelBehavior file at extensions/eexcelview/EExcelBehavior.php location
but i am having following error while executing the code
Alias "extensions.eexcelview.EExcelBehavior" is invalid. Make sure it points to an existing PHP file and the file is readable.
please help me as I am new to yii and I am using yii 1.0 .

Try to referencing extension folder as "ext". Try the following:
'eexcelview'=>array(
'class'=>'ext.eexcelview.EExcelBehavior'
),

Related

DOMPDF: How to customize the name of the single pdf, relating to a single client, with the information present in the object

I am trying to create the pdf, using the domPDF library, for each single client present in the table of my application. So far everything ok!
When I go to save the pdf I want the name of the pdf document to be saved as:
name_client_surname_client.pdf,
so if for example I have a client named Alex Haiold, the document must be saved as Alex_Haiold.pdf
To do this, since I am passing the single client in the controller, as shown below, I tried to write
return $pdf->download('client->surname client->name.pdf');
but it is saved as
client-_surname client-_name.pdf (then printing client-_surname client-_name).
Here my code:
public function generatePDF(Client $client)
{
$data = [
'title' => 'Welcome to LaravelTuts.com',
'date' => date('m/d/Y'),
'client' => $client
];
//dd($data);
$pdf = PDF::loadView('client.clientPDF', $data);
return $pdf->download('client->surname client->name.pdf');
}
Can anyone kindly help me?
$pdf = PDF::loadView('client.clientPDF', $data);
return $pdf->download($client->surname . ' ' . client->name . '.pdf');
On the first line, we are just making a view file. The file is resources/views/client.clientPDF.blade.php and we are feeding data to the view file by passing the variable $data to it.
After making the view, our next step is to download the pdf file. For doing this, we need to call the download method of the instance.
On the first parameter, we are passing the filename with extension(pdf).

How can I set encoding for export csv with Laravel Excel in Laravel

I using Laravel Excel for export CSV file in Laravel.
How can I set the encoding for export csv file.
I have tried several ways:
Change config in excel.php
'use_bom' => false,
Use mb_convert_encoding to convert content to before export.
$exportData = mb_convert_encoding($exportData, "SJIS", "UTF-8");
$pblClassExport = new \App\Exports\PblClassExport($exportData, 'test.csv');
But it's not working. The encoding of csv file auto change by file content.
you need to configure your PblClassExport.php headers
in PblClassExport.php
/**
* Optional headers
*/
private $headers = [
'Content-Type' => 'text/csv',
'Content-Encoding'=> 'SHIFT-JIS' // somthing like this ?
];
i have't done this but i think it will work
ref link
https://docs.laravel-excel.com/3.1/exports/exportables.html#exportables
Update
you can encode line by line
public function bindValue(Cell $cell, $value)
{
$value = mb_convert_encoding($value, "SJIS");
return parent::bindValue($cell, $value);
}
ref link https://www.gitmemory.com/issue/Maatwebsite/Laravel-Excel/1886/552849170
If you you use Laravel Excel 3.1, you can use WithCustomCsvSettings interface. Then you add your encoding setting in the getCsvSettings method like the following.
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
class InvoicesExport implements WithCustomCsvSettings
{
public function getCsvSettings(): array
{
return [
'output_encoding' => 'SJIS',
];
}
}
Plese refer to the docs for more details.
I resolved it. Let's me share my solution here.
Laravel Excel not support it by default.But we can do it by simple way.
Get csv content before download: \Excel::raw
Convert to another encoding: mb_convert_encoding
https://docs.laravel-excel.com/3.1/exports/collection.html#storing-raw-contents
Download csv.
$exportedObject= new \App\Exports\ClassExport($exportDataArray, $fileName);
$csvContent = \Excel::raw($exportedObject, $exportedObject->writerType);
$csvContent = mb_convert_encoding($csvContent, 'SJIS', 'auto');
// In my case, I upload my csv to S3.
$storageInstance = \Storage::disk('s3_import_csvs');
$putFileOnStorage = $storageInstance->put($fileName, $csvContent);
Note: Solution povided by OP on question section.

Laravel batch file upload

I have a csv file with the following structure:
id,title,sub_title,filename
1,Title 1, Sub Title 1,filename_1.mp3
2,Title 2, Sub Title 2,filename_2.mp3
3,Title 3, Sub Title 3,filename_3.mp3
(...)
I'm loading the CSV file inside a App\Console\Command (artisan command).
Assuming the files exist on the filesystem and the path is correct, for each csv line how can I upload the related file and validate it using the Validator class?
I'm using this code:
$validator = Validator::make(array('filename' => 'path_to_filename_x.mp3'), [
'filename' => 'required|file|audio:mp3,wav,ogg',
]);
if ($validator->fails()) {
echo '<pre>';
print_r($validator->errors());
echo '</pre>';
die();
}
I'm getting the error:
"The attribute must be a file."
Because I'm sending text instead of a resource (i suppose).
How can I upload these files without using a form using Laravel 5.6?
Thks!
Question: how can I upload the related file and validate it using the Validator class?
Validating the files:
Create a custom rule in laravel and there you can add logic like.
if(file_exists($path)){
//check for file type
}
Upload the files :
use $fcontent = file_get_contents($filepath) and file_put_contents($newfilepath, $fcontent)

Unit testing on laravel file downloading location

I am new to Laravel. I am writing unit testing on laravel for downloading a csv file. When I run the test case, I get assertResponseStatus as 200 and I need to open the created csv file and I am unable to find the location of downloaded file. How can I find the downloaded file.
This is the controller fuction
public function generateCsv(){
$list = $this->csvRepo->getDetails();
$heading = array(
'heading 1',
'heading 2',
'heading 3'
);
$this->csvRepo->generateCsv($heading,'csv',$list);
}
I need to know the location of downloaded file when run the test case
Assuming you are using the latest version of Laravel / PHP Unit you are able to use the following:
class ExampleFileDownload extends TestCase
{
public function fileDownloads()
{
Storage::fake('file');
// assuming we wanted to test like this:
$response = $this->json('POST', '/test', [
'file' => UploadedFile::fake()->image('testing.jpg')
]);
// Assert the file was stored – I believe this is the line you are looking for
Storage::disk('file')->assertExists('testing.jpg');
// Assert a file does not exist...
Storage::disk('file')->assertMissing('missing.jpg');
}
}
Let me know how you get on :)

Wordpress media_sideload_image - Download http://placekitten.com/100/100?

media_sideload_image
WordPress have a function called media_sideload_image. It is used to upload an image and attach it to the media library.
I accepts image urls like this:
h**p://s.wordpress.org/style/images/wp-header-logo.png
Rewritten URLs
Some URLs on the web are rewritten, for example:
http://placekitten.com/100/100
Error message:
"Sorry, this file type is not permitted for security reasons."
The file type is a correct JPG-file but the file extension is missing.
Adding extra MIME types don't work, in my case
I tried this function but it does not help me, because it's the file extension that is not set.
add_filter('upload_mimes', 'add_custom_upload_mimes');
function add_custom_upload_mimes($existing_mimes){
$existing_mimes['jpeg'] = 'image/jpeg';
return $existing_mimes;
}
Question
How do I upload the URL h**p://placekitten.com/100/100 with media_sideload_image or alike to attach the image to the media library?
I read your question yesterday, when i need this solution.
I find a answer after 24 hours.
Here is Full solution
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$image_url = "http://domain.com/blog/23092839823";
$image_tmp = download_url($image_url);
if( is_wp_error( $image_tmp ) ){
echo "<br> Image Download Fail:";
}else {
$image_size = filesize($image_tmp);
$image_name = basename($image_url) . ".jpg"; // .jpg optional
//Download complete now upload in your project
$file = array(
'name' => $image_name, // ex: wp-header-logo.png
'type' => 'image/jpg',
'tmp_name' => $image_tmp,
'error' => 0,
'size' => $image_size
);
//This image/file will show on media page...
$thumb_id = media_handle_sideload( $file, $post_id, $desc);
set_post_thumbnail($post_id, $thumb_id); //optional
echo "<br> Image Save ";
}
Digging into core, it looks like you need the unfiltered_upload capability in order to upload files without an extension:
if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
return $upload_error_handler( $file, __( 'Sorry, this file type is not permitted for security reasons.' ));
According to the Roles and Capabilities documentation:
This capability is not available to any role by default (including Super Admins). The capability needs to be enabled by defining the following constant:
define( 'ALLOW_UNFILTERED_UPLOADS', true );
With this constant defined, all roles on a single site install will be given the unfiltered_upload capability, but only Super Admins will be given the capability on a Multisite install.
Today I have faced the same problem, and come up with a bit dirty yet successful method to work around. As it turns out, media_sideload_image only checks for the .jpg (or any image) extension in the url, so if you add it to the end of your link, it shoud work.
So you only need to add something to the end of the url that won't change the output, for example:
http://placekitten.com/100/100?name=image.jpg
I can't say it works all the time, but it works here (TESTED). :)

Resources