How to fix 'The file .... does not exist in Laravel? - laravel

I want to download file from storage folder in my Laravel project.
This is the method I'm using to download the file:
return response()->download(storage_path($path), $name);
When I click the link which redirects to this method I receive this output from the browser:
Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
The file "pathToTheFile" does not exist.
The problem is that when I go to www.mydomain.com/pathToTheFile, the browser shows me this txt file. Why doesn't it download the file from the link? Anyone can help?
The file is uploaded using Voyager Admin Package if it's important.

I resolved the problem. The method should be:
return Storage::disk('public')->download($path, $name);
Thanks for help anyway :)

1.Method:
$filePath = public_path("storage/app/public/test.xlsx");
return response()->download($filePath);
get "file does not exist" error.
2.Method:
$filePath = public_path("storage/app/public/test.xlsx");
return Storage::disk('public')->download($filePath);
get "Unable to retrieve the file_size for file at location" error.
3.Method: (Solution for me)
$filePath = storage_path('/app/public/test.xlsx');
return response()->download($filePath);

Related

Laravel - File upload doesn't seem to allow all types of files for some reason

My application denies access to upload files with certain extensions for some reason.
It prints out the error:
fopen(C:\wamp64\www\storage\app\public/wrdjxF3EMbtGCUyXjSEWH5zeAu822ACeKikLPaCT.): failed to open stream: Permission denied
The file types that have given out this error so far are: .msg and .txt.
I have no clue what causes this problem, other file types seem to work fine.
The controller function that handles the file uploads:
if($request->hasFile('document'))
{
$files = $request->file('document');
foreach($files as $file){
if($file){
$name = $file->getClientOriginalName();
$size = $file->getClientSize();
$size = number_format($size / 1048576,2);
$path = $file->store('public');
Document::create([
'report_id' => $id,
'report_type' => "App\VisitingReport",
'file_path' => $path,
'file_name' => $name,
'file_size' => $size.' MB'
]);
}
}
}
Any help would be appreciated, thanks.
I'm running this on a Windows computer using WAMP.
I am convinced that it has something to do with the permissions indeed. I'm already a step further, the file gets uploaded but this time without the extension.
can you paste here what permissions you see for other file types and .txt after uploads. Run ls -la in the place where these files are stored and permission should be on far left. something like rwxr-wr-w. Paste here so that at least it can give a clue on what could be the issue.
Otherwise, once .txt file is uploaded you can run chmod 777 some.txt and see if you are still getting permission issue. If you aren't getting permission issue anymore than it's somehow creating .txt file with different permission. But it's not a good idea to do 777 for anything :D. But at least now you know what the problem is.
If permissions are the problem then here is a wonderful answer on this topic
How to set up file permissions for Laravel?
Good luck :)
I'm not an expert just trying to help
I have faced with this error, but I don't know it works for you or not.
put this in your .env file
FILESYSTEM_DRIVER = public
and link the public folder with storage folder with this command:
php artisan storage:link
I hope it works.

How to read a .docx file via PHP Word sample code in Codeigniter

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.

Laravel image upload creating folder instead of file

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);

Creation of zip folder in php

I am trying to create a zip folder of my downloaded images.
Here is my code. I am not getting any errors, but the zip is not getting downloaded.The code is getting compiled and I am getting the output till the display part of the current directory, but after that the code seems to go wrong somewhere and I am not able to get any Zip archive.
<?php
$conn_id=ftp_connect("localhost") or die("Could not connect");
ftp_login($conn_id,"kishan","ubuntu"); //login to ftp localhost
echo "The current directory is " . ftp_pwd($conn_id); //display current directory
ftp_chdir($conn_id,'/var/www/test1'); //changing to the directory where my images are downloaded.
echo "<br/><p> Changing to directory" . ftp_pwd($conn_id);
$file_folder=".";
echo "<br/> The content of the directory is <br/>";
print_r(ftp_rawlist($conn_id,".")); // display the contents of the directory
if(extension_loaded('zip')) //check whether extension is loaded
{
$zip=new ZipArchive();
$zip_name="a.zip"; //some name to my zip file
if($zip->open($zip_name,ZIPARCHIVE::CREATE)!==TRUE)
{
$error="Sorry ZIP creation failed at this time";
}
$contents=ftp_nlist($conn_id,".");
foreach($contents as $file) //addition of files to zip one-by-one
{
$zip->addFile($file_folder.$file);
}
$zip->close(); //seal the zip
}
if(file_exists($zip_name)) //Content-dispostion of my zip file
{
header('Content-type:application/zip');
header('Content-Disposition:attachment; filename="'.$zip_name.'"');
readfile($zip_name);
unlink($zip_name);
}
?>
I just found that there is something wrong with the data that is passed to the for loop.
$contents=ftp_nlist($conn_id,".");
foreach($contents as $file) //addition of files to zip one-by-one
{
$zip->addFile($file_folder.$file);
}
I am not sure exactly where the thing is going wrong but here is the update. What I did was write markup using html and then passing the files to this php script via a POST method, as
foreach($post['files'] as $file){
$zip->addFile($file_folder.$file);} // Adding files into zip
This code is absolutely working fine, but now I need to figure out why post method is working and compressing my files whereas my normal for looping over files is not!!

How to get public directory?

I am a beginner here so pardon me for this question am using return File::put($path , $data); to create a file in public folder on Laravel. I used this piece of code from controller I need to know the value of $path how should it be.
Use public_path()
For reference:
// Path to the project's root folder
echo base_path();
// Path to the 'app' folder
echo app_path();
// Path to the 'public' folder
echo public_path();
// Path to the 'storage' folder
echo storage_path();
// Path to the 'storage/app' folder
echo storage_path('app');
You can use base_path() to get the base of your application - and then just add your public folder to that:
$path = base_path().'/public';
return File::put($path , $data)
Note: Be very careful about allowing people to upload files into your root of public_html. If they upload their own index.php file, they will take over your site.
I know this is a little late, but if someone else comes across this looking, you can now use public_path(); in Laravel 4, it has been added to the helper.php file in the support folder see here.
The best way to retrieve your public folder path from your Laravel config is the function:
$myPublicFolder = public_path();
$savePath = $mypublicPath."enter_path_to_save";
$path = $savePath."filename.ext";
return File::put($path , $data);
There is no need to have all the variables, but this is just for a demonstrative purpose.
Hope this helps,
GRnGC
I think what you are looking for is asset(''); You can use asset('storage'), after creating your symbolic link.
asset('public')
OR
url('public')

Resources