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!
Related
So, I've already dug through the questions here about this, but still can't seem to figure it out.
Do you know what I am doing wrong here?
$imageName = $thishire->color_image;
$path = public_path('/img/colorImages/' . $imageName);
$file = Storage::get(public_path('/img/colorImages/'. $imageName));
$file->move($path, public_path('/img/uploads'));
The 3rd line is throwing an error:
"File not found at path: Applications/MAMP/htdocs/GT Laravel/public/img/colorImages/l18Ow1uVBqhReGqW7lWjanp2vT5bOAKt8pylnmmb.jpg"
But that is the correct path, so I'm super confused.
You shouldn't do Storage::get(public_path('/img/colorImages/'. $imageName)).
Storage::get() is relative to the storage disk root.
If you use the default storage configuration, then your storage root path is storage_path('app'), said otherwise /path/to/your/project/storage/app.
This is what's happening:
Storage::get(public_path('/img/colorImages/'. $imageName));
// looking into /path/to/your/project/storage/app/path/to/your/project/public/img/colorImages
// which is obviously wrong
What you should do instead:
Store your public files in the storage/app/public directory
Use the php artisan storage:link command to create a symlink between public and storage/app/public. If you are not familiar with this concept, it'll create a small file into your public directory which is a shortcut to the storage/app/public directory. More about this here: https://laravel.com/docs/8.x/filesystem#the-public-disk
For instance, with a file named helloworld.jpg and located in storage/app/public/users/8/helloworld.jpg, you can get it by doing:
Storage::disk('public')->get('users/8/helloworld.jpg');
// or Storage::get('public/users/8/helloworld.jpg');
I know it can be confusing at first. This is a quick tip to "know where you are and what your are looking at". Use Storage::path():
dd(Storage::path(public_path('/img/colorImages/'. $imageName)));
// you'll see the problem
don't forget public meaning access storage but by created symlink
I think you should be store image in your path before getting it by using
Storage::put(public_path('/img/colorImages/'), $imageName);
and you can check documentation from here
I solved it.
$imageName = $thishire->color_image;
$old = public_path('/img/colorImages/'.$imageName);
$new = public_path('/img/uploads/'.$imageName);
rename($old, $new);
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.
Am trying to read a .docx file which is inside my project folder via PHP Word library. I have included autoloader like this :
include_once 'vendor/autoload.php'; in my controller.
CODE FOR FUNCTION IN CONTROLLER:
function test_phpword()
{
$a=base_url();
$path=$a."".'123.docx';
$source =$path;
echo date('H:i:s'), " Reading contents from `{$source}`";
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);
echo write($phpWord, basename(__FILE__, '.php'), $writers);
}
BUT GETTING ERROR LIKE BELOW:
06:18:42 Reading contents from http://localhost/myproject/123.docx
Fatal error: Uncaught Exception: Cannot find archive file. in /opt/lampp/htdocs/myproject/vendor/phpoffice/common/src/Common/XMLReader.php:51
Try this
Change
$path=$a."".'123.docx';
to
$path='123.docx';
And put 123.docx beside your php script file. make sure the two files are in the same place. Run your php script again.
If this helps and works fine you can check the file path and make proper change to your php program.
While loading the path, you have to give relative path to the doc file as shown below,
$random_name = 123.docx;
$contents = \PhpOffice\PhpWord\IOFactory::load('../uploads/'.$random_name);
I don't know whats your $base_url, but it will not work if it is absolute path like http://path/to/doc/file.
I have worked on it and tested. Hope it helps.
When I try to upload image from the form , Laravel is creating directory out of image name. Here is my basic code:
$file = Input::file("image");
$file = $file->move(public_path(). "/img/".$file->getClientOriginalName());
//file name is IPC_diagram.png
When die and dump I got this:
'/var/www/php/frbit/l4blog/public/img/IPC_diagram.png/phpvEb9zk'
Now name of image is name of new folder and image is renamed to some random php string, and placed in that folder.
What is the problem. Maybe something related to Linux specific handling of files. Also I was looking into symfony code for this, and symfony is trying to crete new folder every time file is moved but I don't understand how it is related to my code.
Thanks for help.
Do not use dot. Use comma, like this:
$name = time() . $file->getClientOriginalName();
$file->move('uploads/posts/',$name);
I was trying to get path current path in PHP. I tried looking though phpinfo();, but I haven't found any interesting values which could be used to get path to my script. There is no nice values which I used on Linux, like $_SERVER["PWD"].
Now I'm wondering how I'm supposed to find current path. Maybe some function will work... I really have no idea. Because I don't want to hardcode path to script.
getcwd() is what you are looking for.
It's not entirely clear whether you mean the current working directory, or the path to the current script. For the working directory, see #Taze's answer.
For the current script, the __FILE__ magic constant will give you the full filesystem path to the current file.
Note that these constants take "current file" literally: If you include a file and call __FILE__ there, it will show the included file's path.
The getcwd() method will return the current working directory on success.
<?php
echo getcwd() . "\n";
?>