aayush's avatar

Uploaded file is being saved as .txt while using storage::disk

I am using following code to upload csv file. But, the file is being saved as

storage/apps/files/uniqid.csv/another_long_unique_id.txt

It is saving as .txt files and also one extra folder with same unique id is generating

$extension = $request->file('import_file')->getClientOriginalExtension();

$filename = uniqid().'.'.$extension; Storage::disk('local')->put('/files/'.$filename, $request->file('import_file'));

0 likes
12 replies
lostdreamer_nl's avatar

'getClientOriginalExtension' will actually give the real extension.

Are you sure the file you are uploading ends with '.csv' and not something like 'test.csv.txt' ?

aayush's avatar

Yeah, I have checked that by printing out:

print_r($filename); //5af99307e3ee3.csv

lostdreamer_nl's avatar
Level 53

Aah, I see it now:

// FilesystemAdapter:
    public function putFile($path, $file, $options = [])
    {
        return $this->putFileAs($path, $file, $file->hashName(), $options);
        // note the 3rd param:  $file->hashName()
    }


// FileHelpers:

    public function hashName($path = null)
    {
        if ($path) {
            $path = rtrim($path, '/').'/';
        }

        $hash = $this->hashName ?: $this->hashName = Str::random(40);

        return $path.$hash.'.'.$this->guessExtension();
        // $this->guessExtension()...... this is what returns .txt  on your text file
    }

Fix:

$extension = $request->file('import_file')->getClientOriginalExtension();
$filename = uniqid().'.'.$extension; 
Storage::disk('local')->putFileAs('/files/', $request->file('import_file'), $filename);
3 likes
aayush's avatar

Yeah, that works fine and how can I retrieve that file path? I tried with

$csv_file_path = storage_path('/files/contribution_data/').$request->filename;

But it returns following error,

C:\wamp64\www\project\storage\/files/contribution_data/5af9b0c1cfa8f.csv: failed to open stream: No such file or directory

But actually there is file with same name.

36864's avatar

Remove the leading slash.

$csv_file_path = storage_path('files/contribution_data/').$request->filename;
36864's avatar

Try checking the directory through the storage facade:

$files = Storage::files('files/contribution_data');
dd($files);
aayush's avatar

print_r returns

Array
(
    [0] => files/contribution_data/5af9af79e1fd6.csv
    [1] => files/contribution_data/5af9b0a25bc81.csv
    [2] => files/contribution_data/5af9b0b0b27cf.csv
    [3] => files/contribution_data/5af9b0c1cfa8f.csv
)
36864's avatar

And if you get the file directly from the facade?

$contents = Storage::get('files/contribution_data/5af9b0c1cfa8f.csv')
dd($contents);
aayush's avatar

It provide the contents, but I need the path of the csv file so that i can parse it.

36864's avatar

Well, we've just confirmed that the file is there at the expected path and the storage facade can access it.

From here it's difficult to tell where the problem might be without looking at your code, specifically, the part of it where you're trying to fetch the file and parse it.

lostdreamer_nl's avatar

I'm thinking this is one of those nice windows bugs.. you have mixed / and \ in your path.

$path = Storage::disk('local')->putFileAs('/files/', $request->file('import_file'), $filename);
dd($path);

Does that also still have both \ and / in there? If so, try

$path = Storage::disk('local')->putFileAs('/files/', $request->file('import_file'), $filename);
$path = str_replace(['/', '\'], DIRECTORY_SEPARATOR, $path);
dd($path);

That should always return the correct slashes in the path

Please or to participate in this conversation.