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

Ligonsker's avatar

How to putFileAs with backwards slash?

Hello,

How can I make laravel save the file with all backwards slashes?

I use the following code:

Storage::disk('disk')->putFileAs(
            "\some_folder\$year\",
            $request->file('document'),
            "document.$extension"
        );

But it saves the path like so:

\some_folder \ 2023/document.pdf

but I want the last slash to also be backwards:

\some_folder \ 2023\document.pdf

How can I do that?

Thanks

0 likes
6 replies
JeromeFitzpatrick's avatar

Hey @ligonsker It doesn't seem possible unless you override the method as the code that adds that slash appears to be the below line where the slash is hard coded.

vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php

        $result = $this->put(
            $path = trim($path.'/'.$name, '/'), $stream, $options
        );
1 like
Snapey's avatar

stick with forward slashes and it should all work out ok

Storage::disk('disk')->putFileAs(
            "/some_folder/$year/",
            $request->file('document'),
            "document.$extension"
        );
1 like
kokoshneta's avatar

Why would you want to use backslashes? They’re pretty much only used in file paths on Windows (see this StackExchange answer for a brief history of why), and Windows can also parse forward slashes just fine, so you don’t need to use backslashes on Windows.

Everywhere else (file paths in all other OSes as well as URIs) uses forward slashes, and in many cases will not parse backslashes as directory separators.

If your code is only ever going to be run on a Windows server and nowhere else, and you’re only using the saved paths to generate file paths (not URIs for links, images, etc.), then you should be all right using backslashes – but as soon as you have to either run the code on different architecture or use the path names as part of a URI, then you’ll need to use forward slashes anyway to make it work.

So basically, just always use forward slashes, never backslashes.

1 like
Ligonsker's avatar

@snapey @kokoshneta thanks guys. But I think the data team that uses the path in this case specifically need backwards slash because of some weird software they're using. Not sure but I'll ask. Otherwise I would use forward slashes as you suggested. It's just that they asked for it specifically in this case

Snapey's avatar

@Ligonsker save the file with all forward slashes, and then if you need the path created in some specific way, to pass on to another system, then create that string separately

1 like
kiwi0134's avatar

The file will be saved just fine with both backwards and forward slashes, as it is just a directory separator. It does not make a difference to other people wanting to access the file. The file will be in the same place on the filesystem either way.

1 like

Please or to participate in this conversation.