GKMelbo's avatar

Uploaded file damaged after moving and renaming

Hi,

I'm currently able to upload files to my application, but when I try to open the file I get an error saying the file is damaged. I think the reason is because I rename the file and haven't got the extension right. First, I have an input field in my form where the user can choose a name for the file $fileName = $request->fileName;

Then I've specified the path to store the file $path = $buildingID.'/'.$category.'/';

After that I request the file:

$uploadedFile = $request->file('fileInput')->getClientOriginalName();
$fileExtension = $request->file('fileInput')->getClientOriginalExtension();

And then move it and rename it like so:

Storage::disk('dokumentarkiv')->put($path.$uploadedFile, $uploadedFile);

Storage::disk('dokumentarkiv')->move($path.$uploadedFile, $path.$fileName.'.'.$fileExtension);

But I can't figure out what I'm doing wrong. Any help is greatly appreciated!

0 likes
5 replies
ejdelmonico's avatar

It would seem that maybe your method of getClientOriginalExtension() is the problem because you stated the extension may not be correct. One thing you could do is dd($fileExtension): to make sure it is what you expected. If not, then the method is the problem.

GKMelbo's avatar

@ejdelmonico If I dd($fileExtension); it returns the correct extension but without . first. So it returns pdf and not .pdf not sure if that is correct? That's why I add . when I rename the file like: Storage::disk('dokumentarkiv')->move($path.$uploadedFile, $path.$fileName.'.'.$fileExtension); I think that might be the problem, but I have no clue how to fix it.

ejdelmonico's avatar

I think the issue is that a dot is the concat operator. I would revise your getClientOriginalExtension() method to add the dot in its output. You can not just put a dot in front of variable and expect it to show up.

Alternately, you could try this:

$fullFileName = $path.$fileName . "." . $fileExtension;

Storage::disk('dokumentarkiv')->move($path.$uploadedFile, $fullFileName);
GKMelbo's avatar

My apologies, I think I explained too poorly. The file shows up in FTP as test.png but I'm not able to open it.

GKMelbo's avatar

@ejdelmonico What I find odd is that if I do the following:

$fileName = $request->fileName;

      $uploadedFile = $request->file('fileInput')->getClientOriginalName();
      $fileExtension = $request->file('fileInput')->getClientOriginalExtension();
      $fileSize = $request->file('fileInput')->getSize();
      $mimeType = $request->file('fileInput')->getMimeType();
      $path = $buildingID.'/'.$category.'/';

      Storage::disk('dokumentarkiv')->put($path.$uploadedFile, $uploadedFile);

      Storage::disk('dokumentarkiv')->move($path.$uploadedFile, $path.$fileName.'.'.$fileExtension);

I get the error when I try to open the file, but if I use

$fileName = $request->fileName;
        $path = $buildingID.'/'.$category.'/';
        $file = $request->file('fileInput');
        $fileExtension = $request->file('fileInput')->getClientOriginalExtension();

$file->move(storage_path('dokumentarkiv/').$path, $fileName.'.'.$fileExtension);

It works without problems. Any idea what I'm doing wrong with Storage?

Please or to participate in this conversation.