ivanvermeyen / laravel-google-drive-demo

Laravel & Google Drive Storage - Demo project with Laravel 5.4
403 stars 155 forks source link

Rename File #72

Closed desyashasyi closed 4 years ago

desyashasyi commented 4 years ago

I would like to know is there a way to rename a file?

ivanvermeyen commented 4 years ago

Hey,

I think the same way you would rename a directory, but then with a file's path:

https://github.com/ivanvermeyen/laravel-google-drive-demo/blob/a545a75b15ac5b16d896a60854c46b5f0bce5300/routes/web.php#L248-L267

desyashasyi commented 4 years ago

Thank you very much ivan. I was success to renam the file.

I have another issue. For example I uploaded file to a sub directory created before upload. Then I rename the file, but the renamed file move to the root (not in same directori

Root folder (define in File system) | _Folder for upload file (created before upload)

This is my code: ` $uploadedFile = $request->file('sk_first_supervisor');
$hashName = $uploadedFile->hashName();

        //PUT IN DIR
        $dir = '/';
        $recursive = false; // Get subdirectories also?
        $contents = collect(Storage::disk('spv_assign')->listContents($dir, $recursive));
        $dir = $contents->where('type', '=', 'dir')
            ->where('filename', '=', strtoupper($request->first_supervisor))
            ->first(); // There could be duplicate directory names!

        if ( ! $dir) {
            Storage::disk('spv_assign')->makeDirectory(strtoupper($request->first_supervisor));
            //return 'Directory does not exist!';
        }

        $dir = '/';
        $recursive = false; // Get subdirectories also?
        $contents = collect(Storage::disk('spv_assign')->listContents($dir, $recursive));
        $dir = $contents->where('type', '=', 'dir')
            ->where('filename', '=', strtoupper($request->first_supervisor))
            ->first(); // There could be duplicate directory names!

        Storage::disk('spv_assign')->put($dir['path'],$uploadedFile);

        //RENAME FILE
        $dir = '/';
        $recursive = true; // Get subdirectories also?
        $contents = collect(Storage::disk('spv_assign')->listContents($dir, $recursive));
        $file = $contents
            ->where('type', '=', 'file')
            ->where('filename', '=', pathinfo($hashName, PATHINFO_FILENAME))
            ->where('extension', '=', pathinfo($hashName.'.pdf', PATHINFO_EXTENSION))
            ->first(); // there can be duplicate file names!
        //return $file; // array with file info

        if ( ! $file) {
            return 'File does not exist!';
        }else{
           Storage::disk('spv_assign')->move($file['path'], $file_first_supervisor);
        }`

How to solve it?

ivanvermeyen commented 4 years ago

Hi,

You probably need to prepend the directory path to the new filename, like when you upload a file to a directory.

I'm not completely sure, but that directory path might be in the $file info array from the original file.

So the new filename would be something like $directoryPath.'/'.$filename.