mafortis
mafortis

Reputation: 7128

Laravel stores temporary files instead

I have code below and instead of saving files it saves temporary files in location

Code

public function upload(Request $request)
{
    //validate the file
    if (!$request->hasFile('file_to_import')) {
        return new JsonResponse([
            'status'  => 'fail',
            'message' => trans('json.needSelectFile'),
        ]);
    }

    $file = $request->file('file_to_import');

    if (strtolower($file->getClientOriginalExtension()) != 'csv') {
        return new JsonResponse([
            'status'  => 'fail',
            'message' => trans('json.needValidCSV'),
        ]);
    }

    $disk = Storage::disk('local');

    if (!$disk->exists('feeds/translations')) {
        $disk->makeDirectory('feeds/translations');
    }

    $uploaded_date = now();
    $name_without_extension = str_replace('.' . $file->getClientOriginalExtension(), '', $file->getClientOriginalName());

    $new_filename = $name_without_extension . ' - ' . $uploaded_date . '.' . $file->getClientOriginalExtension();
    $location = storage_path('app/feeds/translations/', $new_filename);
    $file->move($location);

    return new JsonResponse([
        'status'   => 'success',
        'filename' => $new_filename,
    ]);
}

file i uploaded

two

what saved in my app location

one

What I need

Is to save real file translator_translations - 2019-04-15 05:52:50.csv and not tmp file.

any idea?

Upvotes: 0

Views: 16712

Answers (2)

Mayank Dudakiya
Mayank Dudakiya

Reputation: 3869

You need to pass filename into the move() function instead of any other otherwise it will take temporary name.

$file->move($destinationPath,$filename);

Notes :

You should use proper filename. Filename should not contain any special character like spaces, comma and colon (:).

Upvotes: 1

Ganesh Khartode
Ganesh Khartode

Reputation: 71

You Wrong Approach Use move function supply two parameters old directory to new directory You remove this line $file->move($location); and use $file->storeAs( $location, $new_filename );

Upvotes: 0

Related Questions