I have a .txt file with countries and their codes and I want to get the contents from it and insert into the database.
But when why try to open the file using the php function fopen() it throws maximum execution time error
here's the code:
web.php:
Route::get('/countries', 'PageController#insertCountries');
PageController:
public function insertCountries()
{
$file = fopen(asset('databases/countries.txt'), 'r');
return 'ok';
}
The size of the file is 6KB. I am using Laravel 5.4
EDIT: the file is in mu public folder in folder databases
If you want to open local file, use File facade to work directly with filesystem, also you shouldn't use asset() helper. So, do something like this instead:
$file = File::get('/full/path/to/the/file/countries.txt');
Related
I am new to CodeIgniter and was working on downloading a file. However, I want to download a file that resides on my local machine, I am able to locate the file by providing the path, also the file gets downloaded with a File type, However, I want my file to be in .csv format
Here goes my Controller's download function:
public function download()
{
$state_id=$this->input->post('state'); // gets me the state-id from viw's dropdown
$this->load->helper('download'); // Load download helper
$file="C:\\Users\usernew\\Desktop\\New folder\\".$state_id.".csv";
$filename=$state_id.'.csv';
if (file_exists($file))
{
$data = file_get_contents($file); //check file exists
force_download($fileName,$data);
}
else{
echo"not working!";
}
}
Where am I going wrong?
As the force_download() function accepts the file name to be set and the data to insert into that file, as you have the data in that file already, you just need to download it. so, You should use the for_download() function like this:
force_download($file,NULL);
This will solve your problem :)
I am trying to move files from my FTP server to a local directory. First I need to find the correct file from the FTP server, as there can be a few hundreds:
//Search for the file.
$fileName= array_filter(Storage::disk('ftp')->files(), function ($file)
{
return preg_match('/('.date("Y-m-d" ,time()).').*.XLSX/', $file);
});
Above finds the correct file. If I dd($fileName), I get this:
My File Name.XLSX
I then try to move that file, to my public disk:
$ftp_file = Storage::disk('ftp')->get($fileName);
$local_file = Storage::disk('public')->move($ftp_file, "moved_file.xlsx");
However above code doesn't work. I get below error:
preg_match() expects parameter 2 to be string, array given
Which I have identified being on below function:
$ftp_file = Storage::disk('ftp')->get($fileName);
What am I doing wrong? How can I move the file, which I am able to find on my FTP server, to my local disk?
Thank you in advance.
As #Loek pointed out, $fileName was an array, and therefore to access it I needed to use:
$ftp_file = Storage::disk('ftp')->get($fileName[0]);
I have used local storage for file storing. I have created table of file, in which title, path, card_id rows stored.
$request->file('file')->storeAs('public/upload',$files->getClientOriginalName());
i.e:passport.png stored at /home/dhruv/MyProject/storage/app/public/upload/passport.png
Above works fine but I'm getting error while downloading file.
in controller...
public function getDownload($file_id)
{
$file = File::find($file_id);
//print_r($file);
return response()->download($file->path);
/* return response()->download(
$file->path,
$file->name,
[],
'inline'//attachment
);*/
}
I tried many downloading methods but any didn't work fine.
I am getting this error.
Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException:
The file
"/home/dhruv/MyProject/storage/public/upload/passport.png"
does not exist in file
/home/dhruv/MyProject/vendor/symfony/http-foundation/File/File.php on
line 37
I dont know why it is searching at /home/dhruv/MyProject/vendor/symfony/http-foundation/File/File.php since i have inserted correct path.
So suggest any method for downloading ..
return response()->download(Storage::get($file->path));
Import this before:
use Illuminate\Support\Facades\Storage;
Laravel Storage
Or try this:
return response()->download(storage_path($file->path));
I am trying to import a csv file with Laravel 5.5 from the local file location. For some reason it can't find the file on my computer however the path is correct.
$fileLocation = $request->file('file')->store('csv');
$importFile = File::file(storage_path('app/' . $fileLocation));
You can use the Storage facade for this. Here is an example:
if (Storage::disk($disk)->exists($filename)) {
return Storage::disk($disk)->get($filename);
}
throw new FileNotFoundException(sprintf('File not found: %s', $filename), 404);
If you don't wanna use Storage facade, you can use below code to get the file
$importFile = Illuminate\Support\Facades\File::get(storage_path('app/' . $fileLocation));
but when you store a file with csv as store argument, it will save in storage/csv folder, and you just need call storage_path($fileLocation).
be sure that your storage path is correct and for more information read here
I Am using the standard CI zip library to read a directory and create a zip file for downloading however when I am testing this in windows I get:
and in OS X the zip unpacks a cpgz file which then unpacks to a zip file - infinitely
My CI function:
public function downloadPackage($unique) {
$this->load->library('zip');
$text = $this->syndication_m->getTextForContent($unique);
$path = '/var/www/html/uploads/'.$unique.'/';
if(file_exists($path."copy-".$unique.".txt")) {
unlink($path."copy-".$unique.".txt");
$fp = fopen($path."copy-".$unique.".txt","wb");
fwrite($fp,$text);
fclose($fp);
}
$this->zip->read_dir($path);
$this->zip->download('dl-'.$unique.'.zip');
}
Can anyone help me with a fix or suggest what to do here? Thanks
EDIT
public function downloadPackage($unique) {
$this->load->library('zip');
$path = '/var/www/html/uploads/'.$unique.'/';
$text = $this->syndication_m->getTextForContent($unique);
$this->zip->read_dir($path, TRUE);
$this->zip->add_data('copy-'.$unique.'.txt', $text->synd_text);
$this->zip->download('dl-'.$unique.'.zip');
}
I have just come across a very similar issue where the downloaded zip file got corrupted and this is what solved my problem:
I call the ob_end_clean() function just before the $this->zip->download() line.
From what i know and what codeigniter's documentation is saying:
$path = '/var/www/html/uploads/'.$unique.'/';
$this->zip->read_dir($path)
Permits you to compress a folder (and its contents) that already exists somewhere on your server. Supply a file path to the directory and the zip class will recursively read it and recreate it as a Zip archive. All files contained within the supplied path will be encoded, as will any sub-folders contained within it.
Basically you are creating a zip with everything starting from /var and ending with the files in the $unique folder, something is bound to be wrong ...
I recommend setting the second parameter to false, as the documentation specifies:
If you want the tree preceding the target folder to be ignored you can pass FALSE (boolean) in the second parameter.
$this->zip->read_dir($path, FALSE)
This will create a ZIP with the folder "$unique" inside, then all sub-folders stored correctly inside that, but will not include the folders /var/www/html/uploads.
I also suggest/recommend, for better control of data and i believe fewer resources, to use:
$this->zip->add_data() instead of fopen() and $this->zip->read_dir()
Example:
public function downloadPackage($unique) {
$this->load->library('zip');
$text = $this->syndication_m->getTextForContent($unique);
$this->zip->add_data('copy-'.$unique.'.txt', $text);
$this->zip->download('dl-'.$unique.'.zip');
}
NOTE: Do not display any data in the controller in which you call this function since it sends various server headers that cause the download to happen and the file to be treated as binary.
--------------- EDIT ---------------
Unless you want to add to the zip whatever files are in the $unuqie directory, there is no use for $this->zip->read_dir().
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Testing extends CI_Controller {
public function downloadPackage($unique = '') {
$this->load->library('zip');
$unique = '4ts5yegq;';
$text = 'I am zorro ...';
$this->zip->add_data('copy-'.$unique.'.txt', $text);
$this->zip->download('dl-'.$unique.'.zip');
}
}
In place of writing file using fopen try to use "$this->zip->add_data($text);" and make sure directory(sub directories) $path pointing to does not have any soft link(shortcuts) files.
Apart from this try, not to include empty folders in zip file sometimes it creates these kind of issues.
I had similar issue. whenever i was creating new zip file and unzipping .zip file, new file was creating as .cpgz. this was happening due to my zip file was not creating properly. Means it was creating as corrupt zip file.
Meanwhile i was getting Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) error. Once i solved this Fetal error, my zip and unzip issue got resolved.
I resolved below error
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried
to allocate 262144 bytes)
Add into index.php
ini_set('memory_limit', '-1');
deletes the topmost output buffer and all of its contents using the ob_end_clean() function. Add ob_end_clean(); function just before db download function call.