I have error "upload_invalid_filetype" everytime I upload file in xls and csv format. It just work fine for file with xlsx, docx and pdf. I know that it has something to do with Mimes.php. I tried everything I found in the internet to get it work but still no luck. Here I put the mime content so maybe you can find what I missed.
'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel','application/zip'),
'xls' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/excel', 'application/vnd.ms-excel', 'application/msexcel','application/zip'),
if your xls and csv is the problem then try this in mime.php
'xls' => array('application/vnd.ms-excel', 'application/msexcel', 'application/x-msexcel', 'application/x-ms-excel', 'application/x-excel', 'application/x-dos_ms_excel', 'application/xls', 'application/x-xls', 'application/excel', 'application/download', 'application/vnd.ms-office', 'application/msword'),
'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain'),
also while uploading try this
$randomString = substr(str_shuffle(str_repeat($x = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ceil(10 / strlen($x)))), 1, 10);
$config['upload_path'] = $YOUR_PATH;
$config['file_name'] = $randomString;
$config['allowed_types'] = 'csv|CSV|xls|XLS';
$config['overwrite'] = 0;
$config['max_size'] = 2048;
$this->load->library('upload', $config);
$this->upload->initialize($config); // Make sure it has been initialized
also try to loaded the library in the construct, or right after the upload part
$this->load->library('excel');
this will only upload the xls and csv file, you have to read the file after upload you will have to follow the documentation of excel library
Related
I'm trying to generate and then download a csv file in Laravel 9.
The generation is ok at the correcte location (public/files/OUT/) but i'm getting the content in the response and no download occurs.
here is my code :
$path = storage_path('app\public\files\OUT\\');
$filename='dataTemplate';
$f = fopen($path.$filename.'.csv', 'wb');
// => PUT DATAS IN CSV => no problem
fclose($f);
// file is generated successfully at path location
$headers = [
'Content-Type' => 'text/csv',
];
return response()->download($path.'dataTemplate.csv','dataTemplate.csv',$headers);
Thanks in advance
I am trying to send a mail to multiple recipients with an attachment of file URL , but while hitting the api in postman it's throwing an error unable to open file for reading [ file link ] , but while I am copying file link and opens in browser it's opening perfectly .
I have checked the file permission also and referred to some of the answers on Stackoverflow but nothing helped me, please help me as soon as possible.
$file_name = 'TimeActivityReport' . "_" . time() . '.pdf';
$storage_path = 'public/TimeActivityReport';
// $storage_path = public_path();
$filePath = $storage_path . '/' . $file_name;
// return $filePath;
$exl = Excel::store(new TimeActivityReportExport($all_total_values,$data,$date_totals), $filePath);
if($exl)
{
$fileurl = asset('storage/TimeActivityReport').'/'.$file_name;
// return $fileurl;
}
// return $fileurl;
return Mail::send([], $emails, function($message)use($fileurl,$emails) {
$message->to($emails,'hello')
->subject('test')
->attach($fileurl,[
'as' => 'checkname.pdf',
'mime' => 'application/pdf'
])
->setBody('check');
});
Try this I tested it on my end and it returned the file
Storage::get('./public/TimeActivityReport/'.$file_name);
You can also test if the file exists using:
Storage::disk('local')->exists('public/TimeActivityReport/'.$file_name);
To attach try:
$fileurl = Storage::path('public/TimeActivityReport/'.$file_name);
resource laravel docs
I am using guzzle to downlaod file from url and save it into my storage.
So I have a code look like this
$response = $this->client->request('GET', $model->url, [
'stream' => true
]);
$body = $response->getBody();
while (!$body->eof()) {
Storage::append($this->filePath, $body->read(1024));;
}
but when I open the folder where my file is located, I see that the file size is changing and sometimes it is zero. So in the end I am getting a invalid file.
How can I solve this problem?
Installation Process
I followed this tutorial to install aws Package in Laravel 5.3
My Code is below
$s3 = \App::make('aws')->createClient('s3');
$s3->putObject(array(
'Bucket' => 'Bucket_Name',
'Key' => 'AWS_ACCESS_KEY_ID',
'SourceFile' => 'http://domainname/sample.txt',
));
I am trying a txt file with around 50 bytes contents and got below error.
A sha256 checksum could not be calculated for the provided upload
body, because it was not seekable. To prevent this error you can
either 1) include the ContentMD5 or ContentSHA256 parameters with your
request, 2) use a seekable stream for the body, or 3) wrap the
non-seekable stream in a GuzzleHttp\Psr7\CachingStream object. You
should be careful though and remember that the CachingStream utilizes
PHP temp streams. This means that the stream will be temporarily
stored on the local disk.
Am I missing something?
SourceFile must be a local file path. The Body parameter allows stream, so you should be able to do a request with guzzle and pass the body to it.
$client = new GuzzleHttp\Client();
$response = $client->get('http://domainname/sample.txt');
$s3->putObject([
'Bucket' => 'Bucket_Name',
'Key' => 'AWS_ACCESS_KEY_ID',
'Body' => $response->getBody(),
]);
I'm almost done with a multipart file upload but not quite. The API I am using requires two parts: a meta description (Json) and a file (File).
Here some of the Code:
File.open(file_path) do |image|
request = Net::HTTP::Post::Multipart.new(
url.path,
'metadata' => metadata_as_json_string,
'attachment' => UploadIO.new(image, "image/jpeg", "image.jpg")
)
The trouble I am having is with the 'metadata' part (metadata_as_json_string). Without it everything works fine, but the API requires meta information as json. It works if I save the json content in a file and use it as metadata-part. But my content is not coming from a file.
Any ideas how to provide the metadata without previously saving it in a file?
Thank you
I did find a solution myself by using StringIO:
metadata_file = StringIO.new(metadata_as_json_string)
File.open(file_path) do |image|
request = Net::HTTP::Post::Multipart.new(
url.path,
'metadata' => UploadIO.new(metadata_file, "application/json"),
'attachment' => UploadIO.new(image, "image/jpeg", "image.jpg")
)
Anyway thank you for your time