128

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.

1
  • are you using Laravel 3 or Laravel 4?
    – Laurence
    Feb 12, 2013 at 16:26

8 Answers 8

319

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');
2
  • 7
    Just to add that public_path() is actually pretty useless- it just assumes all your public files are in a directory called "public"- even if they aren't. For example, I always have to change the default "public" directory to "public_html" (in line with my server). Laravel doesn't recognise this. So don't rely on this to make any intelligent determination of what your public directory actually is. I'm just using $_SERVER['DOCUMENT_ROOT']; unless anyone can suggest why I shouldn't?
    – Inigo
    Oct 21, 2017 at 17:40
  • 12
    @inigo it is not useless, you are just not using it as it was intended. If you want to change the directory of public, you need to tell Laravel about it: stackoverflow.com/questions/31758901/…
    – Justin
    May 2, 2018 at 21:23
23

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.

2
  • 1
    is base_path defined in laravel .. i used it i got this error Call to undefined function base_path() Feb 12, 2013 at 16:48
  • Yes - it is defined in helpers.php. I tested in on my L4 app - it works. Make sure you are running the latest L4 build - it might not have been in the earlier versions.
    – Laurence
    Feb 12, 2013 at 16:51
14

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.

9

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

1

I think what you are looking for is asset(''); You can use asset('storage'), after creating your symbolic link.

1
  • asset_path("") did it for me.
    – Studocwho
    Nov 6, 2022 at 3:54
1

Use asset() function; for example, you have image.png in the assets folder in the public dir ie: public/assets/image.png You can access it by:

$image = asset('assets/image.png');
0
asset('public')

OR

url('public')
0

To get a public directory in laravel controller:

you can use

$custom_url = "sdefrghj";
$filename="assets/".$custom_url.'-question-list.csv';

hope you get it :-)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.