Laravel Uploaded file path return error - php

i been following the upload method using this tutorial and i got some errors that i cant brain anymore.
Here are the example, i upload file name "fileNumber1.pdf", the laravel system will save the file using Storage facade.
$filePath[] = $fileData->storeAs('uploadedFile', $fileName);
So the original path will be "storage/app/uploadedFile/fileNumber1.pdf".
To view the file, the tutorial suggest that to use
echo asset('storage/file.txt');
So from my understanding the echo should be like
echo asset('storage/uploadedFile/fileNumber1.pdf');
Since i use local development the return is
http://localhost:8000/storage/uploadedFile/fileNumber1.pdf
Then i go to the link given, it return
Sorry, the page you are looking for could not be found.
So how is the proper way to receive or view the file?

This url http://localhost:8000/ points to pubilc folder
So store your file inside uploadedFile folder inside public folder then use like
http://localhost:8000/uploadedFile/fileNumber1.pdf
otherwise use storage_path('uploadedFile/fileNumber1.pdf'); instead asset('storage/uploadedFile/fileNumber1.pdf');

If you would have followed the tutorial link then you should have run the below command which creates a symlink, If you haven't, do run that artisan command in your application folder.
php artisan storage:link
So now the below commad will give you desired result.
{{ asset('storage/uploadedFile/fileNumber1.pdf') }}
or
{{ storage_path('uploadedFile/fileNumber1.pdf') }}
You don't have to point the complete path of your file because asset() will defaultly point you to public folder and then you have to add the required path of your file location.

sorry for all the rude if you receive.
The mistake is mine.
The solution was simple.
Original is
$filePath[] = $fileData->storeAs('uploadedFile', $fileName);
The solution is
$filePath[] = $fileData->storeAs('public/uploadedFile', $fileName);
Just add "public" before the file path before uploading.
Thanks all for the effort.

Related

Copy file from any given path in Laravel

I want to copy a file from outside the root directory of my Laravel application using an artisan command which i made myself. The user will call the command like so:
php artisan crawl:pdf {filepath}
Then the filepath will be processed and on this part:
File::copy($path, storage_path('pdfs') . '/' . $fileName . ".pdf");
it breaks every time i use a path outside of my root directory it says "Failed to open stream: No such file or directory" as error msg".
I've already googled about copying files in plain php using copy() and in Laravel using Storage::copy(). Also reading through articles that all describe creating a disk which can route outside of laravel-root-directory.
All of that is not the solution to my problem. The user should be able to use any path.
C:\dir1\pdf3.pdf ;
E:\dir456\pdf2222.pdf`
...
from his own filesystem to copy files to the laravel-application.
Is their a way todo it ? Thx for all the help in advance!
EDIT 1:
Before anyone asks if i have write and read permission: yes i do. if i use plain php i can write and read everywhere on my working drive. I also checked the user laravel is running on with echo whoami and it gives the same user as my plain php application which works fine.
EDIT 2:
Worked in comments regarding using File-Facad-copy
Ok i found a solution and now it is working as i want it.
But first why is this happening. Laravel is expecting to always work on files using disks. It is required.
the solution is On-demand Disks!!!
This way it is possible to take a path given by a user and create a temporary disk which only works in this instance through facades.
Important is to make sure that the path given is always a directory! Like so:
$diskpath = dirname($path);
$disk = Storage::build([
'driver' => 'local',
'root' => "$diskpath",
]);
In my case I only want to copy one specific file which makes it mandatory to get the file information for correct naming and copying. Like so:
$pathinf = pathinfo($path)
$fileName = time() . '_' . $pathinf['filename']
Next we need to safe it to the new location. Like so:
Storage::disk('pdfs')->putFileAs('/', $path, $fileName . ".pdf");
Notes:
'pdfs' disk is a disk i configured in my filesystems.php config-file
I always asume that the user is giving the full path from the file like C:\...\test.pdf or something similar so watch out for the dirname() function. It will always cut the last part of the path and delete it.
Thanks to apokryfos for helping solving this through his comments and hints.

Laravel 8 Storage move file not found

I am trying to move a file from a temporary path to the correct path. However no matter what I tried it always sends back this file not found error:
League\Flysystem\FileNotFoundException
File not found at path: var/www/html/storage/app/public/covers/tmp/608c07a082451-1619789728/81-536x354.jpeg
I am using sail thus there are 'var/www/html/' in the path, even though there is no problem with this path in the container since it is possible to reach this path from the frontend fetch API and that is how the temp file was saved.
Here is the code in the controller:
//...
$temp_file = TempFile::where('folder', $cover_folder)->first();
if ($temp_file) {
// retrieve file path and target path
$filepath = storage_path("app/public/covers/tmp/{$temp_file->folder}/{$temp_file->filename}");
$target_path = storage_path("app/public/covers/{$course->id}/");
Storage::move($filepath, $target_path);
$course->cover_img = "{$target_path}/{$temp_file->filename}";
$course->save();
// remove temp directory and temp file record
rmdir(storage_path("app/public/covers/tmp/{$temp_file->folder}/"));
$temp_file->delete();
}
The error traceback highlighted on this line:
Storage::move($filepath, $target_path);
Also tried to use Storage::disk('public')->move(), or public_storage, or Storage::path(), or only pure string, all did not work.
Symlink is set from public/storage to storage by running php artisan storage:link.
I have searched as much as I could find on laracast, stack overflow, or github, but could not reach a solution. Does anyone have any idea on this issue?
Really really appreciate it.
dont use storage_path function because method Storage::move( have prefix path [app_path]/storage/app/
Storage::move("public/covers/tmp/{$temp_file->folder}/{$temp_file->filename}", "public/covers/{$course->id}/");

How to unlink file from children dir of parent dir? PHP

I want to unlink an image file from children dir of a parent dir.
I developing this project in a director folder under my website (mysite/project) but when i complete the project i will move to another hosting package.
Whic code i need use for stable running in all directories?
I get this error:
Warning: unlink(../../images/urunler/deneme-urun.jpg): No such file or directory in /home/admin/public_html/eticaret/admin/includes/updateproduct.php on line 120
My folder structure:
click here to see my folder structure
$delete = unlink("../../images/products/".$img);
Before running unlink() command you can check the existence of the file to get rid of such error
$path = "../../images/products/".$img;
if (file_exists(path)) {
unlink($path)
}
Better is using the full directory path (absolute path). An example, let your project structure is
and your unlinking code is in script.php, then your can get full absolute path the following way
$path = __DIR__ . "/../../images/products/{$img}";
if (file_exists(path)) {
unlink($path)
}
Thanks for all answers.
I have a fault on this topic.
Image name is xxx.jpg in my database but xxx.jpeg in the folder.
but there is an interesting situation: if you have an image as .jpeg format, you can open on browser as .jpg format.
I've tried a lot of methods to solve the above error, but I've found that.
I using chrome browser now.
So i solved my problem yet :)
as the script is run from "public_html/eticaret/admin/includes/" you need to .. traverse back 3 directories to get to public_html which is a common ancestor folder for both image and script
according to your directory image your images are in public_html/myproject/images/products/ and according to error detailed in your comment you are trying to delete from images/urunler/ I assume urunler is the actual project name.
if the folder containg your images is urunler try $delete = unlink("../../../images/urunler/".$img);
if the container folder is products try $delete = unlink("../../../images/products/".$img);
if that fails try using an absolute path instead of a relative path.

How to download all files from directory using codeigniter?

I am using codeigniter framework for a website. I need to download all the files from a particular directory. I can do this task using PHP alone but not working in codeigniter may be due to some path issue. I searched on many sites but not able to get correct solution. Also, followed official documentation here : codeigniter zip documentation , but without any success. I need working code , which can be tested directly.
code which I have now is :
$path = '/path/to/your/directory/';
$this->zip->read_dir($path);
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');
After a lot of tries, finally i found the exact problem. Actually, the CodeIgniter code works perfectly. The problem is setting the path. Here I am zipping a folder named noimg inside assets folder inside CodeIgniter project folder.
function zip()
{
$this->load->library('zip');
$path = FCPATH.'/assets/noimg';
$this->zip->read_dir($path,FALSE);
// Download the file to your desktop. Name it "my_backup.zip"
$this->zip->download('my_backup.zip');
}
Don't forget to put 'FCPATH.' before /assets very important. Then only the path can be identified. If you have any problem please comment below. The above code work perfectly for me

How to load images in twig from root directory folder in Symfony 2?

I'm new in Symfony and my problem is simple, I just can't load images from a certain directory and display it to twig. I have this upload folder located in the root directory of my Symfony project (outside of web folder). This folder is just a link to other VM through Linux file system and which has the original upload folder. I've tried to check if it exists using php's
file_exists('/var/www/html/<projname>/upload/somefolder/image.png');
and it shows that the image exists but when I'm trying to display the images in twig, it always shows 404 error not found and the image is not displaying. I've already did this:
<img src="/var/www/html/<projname>/upload/somefolder/image.png" />
and
<img src="/upload/somefolder/image.png" />
How does symfony handle this situation?
If you want to get the path of your root directory you can try below code. Here is a best example provided for file uploading.
$this->get('kernel')->getRootDir() . '/../web' . $this->getRequest()->getBasePath();
Might be this can also help you.
I would suggest using asset() (see https://symfony.com/doc/current/templating.html#templating-assets), but this also requires you to put your folder under projects web/-directory so it could be accessed by symfony and webserver (in combination).
Since you're using a linux, it's simple as that:
cd /var/www/html/<projname>/
ln -s upload web/upload
Now you can simply reference to your assets like:
{{ asset('upload/somefolder/image.png') }}

Categories