Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dvdfreitas's avatar

Access files in the storage folder

Hi everyone,

I'm building an app where the user can upload a python file I want to execute.

The uploaded file cannot be public. I have already built the uploaded part. The files are in the storage/app/code folder.

My problem is that, unless I give the absolute path for the file, I cannot access it. I don't want to use the absolute path because the app will be installed in different machines.

If I use storage_path('code/' . $file) it opens the '/storage/code/' and doesn't work.

Can someone, please, help me?

Thanks, David

0 likes
3 replies
LaryAI's avatar
Level 58

To access files in the storage folder, you can use the storage_path() function provided by Laravel. This function returns the absolute path to the storage directory. You can then append the relative path to your file to this path to get the full path to the file.

Here's an example:

// Assuming $file contains the name of the uploaded file
$filePath = storage_path('app/code/' . $file);

// Check if the file exists
if (file_exists($filePath)) {
    // Do something with the file
} else {
    // File not found
}

In this example, we're using the storage_path() function to get the absolute path to the storage directory. We're then appending the relative path to our file (app/code/ followed by the file name) to this path to get the full path to the file.

We're then checking if the file exists using the file_exists() function. If the file exists, we can do something with it. If not, we can handle the error appropriately.

Note that we're using the app directory within the storage directory. This is because files uploaded by your application should be stored in the storage/app directory. If you want to store files in a different directory within the storage directory, you can replace app with the name of your directory.

Snapey's avatar
Snapey
Best Answer
Level 122

You can upload the file into any folder and access it with base_path() . path/to/your/file;

You might use storage/app/code just as easily as any other folder, so I'm expecting that your code should work.

storage_path() may not work if you have made changes to it in config/filesystems.php

1 like

Please or to participate in this conversation.