In laravel 5.7 app I need to write csv file and download in in browsers download function using method :
\Response::download(
I try to upload it to tmp directory and checking value I see
$sys_get_temp_dir= sys_get_temp_dir();
line above has /tmp value
Next I save it as :
$dest_csv_file= $sys_get_temp_dir . '/box_rooms_'.time().'.csv';
$header = ["Id",... ];
$fp = fopen($dest_csv_file, "w");
fputcsv($fp, $header);
foreach ($storageSpacesCollection as $line) {
fputcsv($fp, $line);
}
fclose($fp);
But searching on my local ubuntu 18(on server I also have ubuntu) I found generated file as
/tmp/systemd-private-6a9ea6844b9c4c94883d23e4fb3e2215-apache2.service-shqCeo/tmp/box_rooms_1576324869.csv
That was very strange, as I do not know how read it from tmp path. Can it be done?
I think you need to pass Content-Type properly
$filePath= public_path(). "/upload/example.doc";
$headersContent = array('Content-Type: application/pdf',);
return Response::download($filePath, 'example.doc', $headersContent)
Related
I use this code to generate binary file. How to store it in local storage?
$path = 'events/';
$filename = 'visitors_list_' . date('d-m-y') . '.xlsx';
return Excel::import(new VisitorsExport($request), $path.$filename);
I use library
The method is Excel::store().
According to this
Beware your return keyword will end the execution
My code to upload image like this :
$file = $file->move($path, $fileName);
The code works
But I want to change it using Storage::put like this reference :
https://laravel.com/docs/5.6/filesystem#storing-files
I try like this :
Storage::put($fileName, $path);
It does not works
I'm confused, where I must put $file on the code
How can I solve this problem?
Update :
$file = file of image
$path = storage_path('/app/public/product/')
$fileName = chelsea.jpg
So I want to save the file with name chelsea.jpg on the /app/public/product/
Easy Method
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
This will automatically store the files in your default configuration.
This is another example
Storage::put($fileName, $path);
Hope this helps
I have a Laravel 5.3 app that has a form which users can upload multiple files using multiple file fields. The form work in that the files can be uploaded and moed to the destinationPath as I expect but I can't seem to change each of the files 'filename' values. It keeps saving the filename value as the php**.tmp.
Here is the foreach in my controller;
$files = $request->files;
foreach($files as $file){
$destinationPath = 'images/forms'; // upload path
$filename = $file->getClientOriginalName(); // get image name
$file->move($destinationPath, $filename); // uploading file to given path
$file->filename = $filename;
}
If I dd($filename) and dd($file->filename) within the foreach I do get the value (original name) I am looking for but if I dd($files) outside that foreach, the filename is set as the temp php value.
What am I missing? Thanks.
EDIT
The file object looks like this;
-test: false
-originalName: "sample_header_1280.png"
-mimeType: "image/png"
-size: 51038
-error: 0
path: "C:\xampp\tmp"
filename: "php7240.tmp"
basename: "php7240.tmp"
pathname: "C:\xampp\tmp\php7240.tmp"
extension: "tmp"
realPath: "C:\xampp\tmp\php7240.tmp"
I am trying to save the originalName to the db but it seems to default to saving the filename.
Turns out using a foreach for Input::file is not he approach here. If uploading multiple files from the same field - then you'd use a foreach to loop, move and save.
To upload files from multiple file inputs on the same form all you need to do is treat each input individually - as you might with any other form.
In my example I did this in my controller;
$data['image1'] = Input::file('image1')->getClientOriginalName();
Input::file('image1')->move($destinationPath, $data['image1']);
$data['image2'] = Input::file('image2')->getClientOriginalName();
Input::file('image2')->move($destinationPath, $data['image2']);
Not sure this is the best approach (there's always another way) but it worked for me.
I'm trying to make a image upload in lavarel 4. The upload passes by
The file "C:\xampp\tmp\phpD1F3.tmp" does not exist
This is my upload code :
$file = Input::file('image');
// Get extension
$extension =$file->getClientOriginalExtension();
// Generate a file name
$fileName = 'pool' . Str::quickRandom();
$fileNameExtension = $fileName . '.' . $extension;
// Process upload
Input::file('image')->move(public_path() . '/uploads/images/app/pools/original/', $fileNameExtension);
`
I don't know why i get this error , please can anyone help me to solve this?
You are probably making some operations on "uploaded" file after moving it to server by move() function. Please make sure there is no operation made on temporary file after calling Input::file('image')->move() function.
Do you have any sample codes or functions to check if an image name is existing already in the folder before uploading?
I've tried using file_exists() but it doesn't work, here is my sample code:
$path = FCPATH . "images2/";
$filename=$_FILE['userfile'];
$full_path = $path .$filename;
if(file_exists($filename))
{
///display error message///
}
Here is the simplest way to check if a file exist:
if(is_file($filename){
return true; //the file exist
}else{
return false; //the file does not exist
}
I'm assuming you are not getting the correct result with file_exists() because you don't include the full path (even tho you define it).
Try using the following: file_exists($full_path)
Also consider using some CI helper functions for handling files like images, or uploads. They are there to make this 'easier'.
File helper:
http://ellislab.com/codeigniter/user-guide/helpers/file_helper.html