Laravel / Laravel-Excel missing forward slash in asset url - laravel

I am uploading excels in Laravel for processing with Laravel-Excel. I have the following issue:
When I do this:
$file = $request->file('file')->store('Questionnaires', ['disk' => 'public']);
$file = asset($file);
dd($file);
I get something as expected, like:
http://project.test/Questionnaires/filename.xlsx
However, when I pass $file that into Laravel-Excel thus:
$collection = Excel::toCollection(new QuestionnairesImport, $file);
I get this error:
File not found at path: http:/project.test/Questionnaires/filename.xlsx
This is missing a forward slash in the http:/ ie http://
What's going on?

OK, I solved this and am saving the answer here for my oown record and in case it is useful to someone else.
Disclaimer: I still don't know with who the error lay, so consider this a work around/ bug fix/ terrible idea - as you like.
I modified two core files in this way:
In the function readStream() in vendor/league/flysystem/src/adapter/local.php:
// original lines, removed:
//$location = $this->applyPathPrefix($path);
//$stream = fopen($location, 'rb');
// my addition (1 line):
$stream = fopen(asset($path), 'rb');
In the function readStream() in vendor/league/flysystem/src/filesystem.php:
// original line, removed:
//$this->assertPresent($path);
// my addition (1 line):
$path = str_replace('http:/a','http://a', $path);
In the first function it was failing because it couldn't open the path - a local prefix was being added in front of the web address.
In the second function, it couldn't assert the file as present because one of the forward slashes had been removed (still not quite sure how...). So I just did a string replace to put it back in ('a' is the first letter of my project name in APP_URL in .ENV - you need to change it to the first letter of your project for this to work.
This feels a horribly dirty way to do it, but it works. Hopefully I'll come across a better solution.

Related

Move a line from controller to new file and call it in laravel

ThisController in my local PC's Laravel, there is a line like this.
$result = shell_exec('C:\Users\mypc\AppData\Local\Programs\Python\Python37-32\python.exe test.py '. $text);
but, this is local PC path. so if I push to web server, I need to change to
$result = shell_exec('usr\bin\Python test.py '. $text);
So, I want to cut this line and paste into a new file, and I want to call this line from the new file via ThisContoroller. So, I can make the file to gitignore and I don't get conflict.
But I don't know how to do it. Do you have any suggestion? Thank you.
Regarding to ceejayoz's comment, yes, I could do with .env file.
With this thread's answer.

Codeigniter force_download() No output

Fast to explain, but I can't get it to work:
In this simple code, the function force_download simply doesn't make any output.
$this->load->helper('download');
$data = file_get_contents("upload/".$filename);
$name = $no_file;
force_download($name, $data);
Here I just get a white screen, but the file content is show (well you know, the strange codified content :) I think it is simple enough, I just want the file downloaded with no other effect, am I doing something wrong?
I think this is you should do..
$this->load->helper('download');
$path = file_get_contents(base_url()."yourpath/".$filename); // get file name
$name = "test.pdf"; // new name for your file
force_download($name, $path); // start download`
Hope this helps.
EDIT:
Make sure you have a file extension on the filename you supply for the first argument to force_download().
CodeIgniter uses this to set the MIME type, and it doesn't seem to work without.
ERROR: Name or Service not known.
Step1: Opening /etc/resolv.conf (my file was empty)
Step2: Add
nameserver 8.8.8.8
nameserver 8.8.4.4
options rotate
options timeout:3
This may help.

Laravel File Not Found At Path - Storage::move

I've run across an interesting predicament with the Laravel Storage::move function. In my controller I have the following lines of code:
$path = $files->storeAs('/public/temp/agentBilling', $name);
$file = Storage::url($path);
Now, with the script that follows right after this, it can recognize and locate $file using this combination: public_path() . $file;
Which looks like: E:/Apache/htdocs/CASAT/public/storage/temp/agentBilling/htz...jpeg
But, a little bit lower in my script here:
Storage::move(public_path() . $file, '/public/temp/agentBilling'.$pro.'.jpeg');
It says File not found at path:... even though that path (E:/Apache/htdocs/CASAT/public/storage/temp/agentBilling/htz...jpeg) is the same as above....
I've looked and there is a file at the path with the given name. I'm not sure what else to try. I'm just trying to rename the file at this point to see if it works.
Thanks!

renaming a file before uploading to digital ocean spaces using laravel

To upload a file I use
Storage::disk('spaces')->putFile('uploads', $request->file, 'public');
The file is saved successfully on digital ocean spaces. But I want to rename it to something like this user_1_some_random_string.jpg. And then save it.
How can I do it?
The move method may be used to rename or move an existing file to a new location:
Storage::move('hodor/oldfile-name.jpg', 'hodor/newfile-name.jpg');
Also:
If you would not like a file name to be automatically assigned to your stored file, you may use the storeAs method, which receives the path, the file name, and the (optional) disk as its arguments:
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
More: https://laravel.com/docs/5.6/filesystem
try use rand()
$ext = $request->file('file')->getClientOriginalExtension();
$name = rand(11111,99999).'.'.$ext;
Storage::disk('spaces')->putFile('uploads', $name, 'public');
This is pretty old but I found an answer for anyone still looking.
You need to use the method putFileAs, as far as I can see
the first parameter is the bucket/location. I tested this and it will create a new folder if you use 'uploads/testz' it created the 'testz' in the uploads folder on spaces.
the second parameter is the request file object, in my case $request->file('file')
the third parameter is the filename that you WANT to store the file as. I tested and if you 'testz/<yourspecialfilename.extension>' it will create the same folder as in parameter 1, which suggests to me that the method concats param 1 and 3.
So the full snippet in my controller is
public function create(Request $request){ Storage::disk('spaces')->putFileAs('uploads/testz', $request->file('file'), 'mychosenfilename.mydesiredextension');
return redirect()->back();}

Laravel uploading a file to the right directory

I upload the files alright. Only it turns out that, in my case, the starting directory is Public, and that results that the files are accesible by everybody when they should be in the directories that are protected by previous authentication, that is, under App.
So, when I do this:
$file = Input::file('fichero');
$destinationPath = 'uploads/folder';
$file->move($destinationPath);
// Create model
$model = new Model;
$model->filename = $file->getClientOriginalName();
$model->destinationPath = $destinationPath;
The file is saved in here: localh.../myweb/public/
and that is outside the App parent folder where all my application files are.
I can imagine that the fact that is going to that directory is because it is taking the default in the configuration file, which is localhost/laravel/public/index.php/
so, writing something like
$file->move(base_path().'/uploadfolder/subdirectory/', $newname);
will start from what is fixed as base_path, but the base_path needs to be something like localhost/mywebname/public.
So, how do you write the path so that it goes to directories under App and not under Public?
I hope I am not asking a dumb question, but I have been trying for days and I have read the 3 related queries in here.
thank you
You may use:
app_path() // Path to the 'app' folder
app_path('controllers/admin') // Path to the 'app/controllers/admin' folder
This will return you local file system path, you should put your uploaded images in public/... folder.

Resources