YuraLons's avatar

How do I change the name of a file when uploading it?

How to make sure that using this function the documents are uploaded to the folder with the home ID? And how to change the file name from this one (E0FIfjmuAbTV9Nhol2Q31lRNBbJZpL1ZHhZ3VK4R.pdf), to (house id - contract - date)

if ($request->hasFile('passport'))
        {
            foreach($request->file('passport') as $passport){
                $str_passport = $passport->store('passport','public');
                $homes->passport()->create(['document' => $str_passport]);
            }
        }
0 likes
7 replies
tykus's avatar

Use storeAs method to set a custom name - signature is storeAs($path, $name, $options = [])

neilstee's avatar

@yuralons

if ($request->hasFile('passport'))
        {
            foreach($request->file('passport') as $passport){
                $str_passport = $passport->storeAs('passport',  $house->id . ' - ' . $house->contract . ' - ' $house->date, 'public');
                $homes->passport()->create(['document' => $str_passport]);
            }
        }

But of course, you need to change $house->id, $house->contract, and $house->date with the real values but you should get the idea.

YuraLons's avatar

fopen(D:\local\untitled\storage\app/public\passport/6 - - ): Failed to open stream: Permission denied

What rights does he need?

neilstee's avatar

@yuralons read and write permission on the storage directory

sudo chmod -R 664 storage
YuraLons's avatar

i'm us Windows 10 ) It's my local machine

neilstee's avatar

@yuralons then can you right-click on that storage folder and allow "everyone" to have access to it?

I'm not familiar with changing permission on Windows but since the real question here is how to name a file upload I think that's a different question.

YuraLons's avatar

My files seem to be loading correctly, but they are not defined as an extension, that is, it looks like this now (id - [] - -) and the link itself leads to the 404 page, for the location of the file download. I can add an extra field to the title table, right? so that I can fill in the file name myself and upload it to the site?

Please or to participate in this conversation.