1
votes

How do I properly upload a svg file with Laravel 5.5? The standard image validation rule does not see it as a proper image (image|mimes:jpeg,png,jpg,gif,svg)

When I remove all validation, the file get's stored as a txt file.

My upload code: $request->file('image')->store('images', 'public');

2
It is no proper way to store SVG, because it comes with mime text/xml, you can convert it into png or other format using imageMagic lib.Oleg Shakhov

2 Answers

0
votes

This Will Work

Use Like This Will Work

if ($request->hasFile('file')) {
      $file = $request->file('file');       
       $file->move($file, 'uploads/audio');
}
0
votes

from a svg string creating a file, I didn't succeed with string sending as it abuses about some wrong chars after it been transmited to Laravel. So far from Android by okhttp:

private static final MediaType MEDIA_TYPE_TXT =MediaType.parse("text/plain");
....
MultipartBody.Builder obj_ = new MultipartBody.Builder().setType(MultipartBody.FORM);
 fileImzo_ = new File(fileSafar_.getAbsoluteFile().getParentFile().getAbsolutePath(),"imzo.txt");
        String nomiImzo_ = fileImzo_.getName();
//svgImzo is a raw SVG xml string
        InputStream stream_ = new ByteArrayInputStream(svgImzo.getBytes(StandardCharsets.UTF_8));
        try {
            try (OutputStream output_ = new FileOutputStream(fileImzo_)) {
                byte[] buffer = new byte[4 * 1024]; // or other buffer size
                int read;

                while ((read = stream_.read(buffer)) != -1) {
                    output_.write(buffer, 0, read);
                }

                output_.flush();
            }
        }catch(Exception e){
            e.printStackTrace();
        } finally {
            try {
                stream_.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        obj_.addFormDataPart("fileImzo", nomiImzo_, RequestBody.create(fileImzo_, MEDIA_TYPE_TXT));
}
    RequestBody req = obj_
            .addFormDataPart("copayDodmi", "" + copayDodmi)
            .addFormDataPart("izoh", xavar.getText().toString())
            .addFormDataPart("driver_id", "" + driverId_)
            .build();
    Request request = new Request.Builder()
            .header("Authorization", "Bearer " + token)//Authorization
            .url(MehmonxonaActivity.URLI_ASSOSI + "/supurdani_paket_dodashud")
            .post(req)
            .build();
....

in Laravel controller , say we add the file path to array to update a row in the table:

....
 if ($request->hasFile('fileImzo')) {
        $imzo_filename = $this->sozuNomiFaylate('fileImzo', $request);
        if (!is_null($imzo_filename)) {
            $arr['signature_svg'] = $imzo_filename;
        }
    }
....

and the function sozuNomiFaylate():

....

private function sozuNomiFaylate(string $alias, \Illuminate\Http\Request $request)
{
    $separatorLcl = DIRECTORY_SEPARATOR;
    $image = $request->file($alias);
    $ext=$image->getClientOriginalExtension();
    if($ext==='svg' || $ext==='SVG' ||  $ext==='txt'|| $ext==='')
        $ext='svg';
    $filename = time() . '.' .$ext ;
    $path = public_path('images' . $separatorLcl . 'uploads' . $separatorLcl . $filename);
    if($ext==='svg' || $ext==='SVG' ||  $ext==='txt'|| $ext===''){
        File::put($path,$image->get());//Illuminate\Support\Facades\File
    }else
    try {
        l::k($path);
        Image::make($image)->save($path);
    } catch (\Exception $e) {
        l::k($e->getMessage());
        l::k($e->getTraceAsString());
        l::k('fayl  soxta na shud');
        return null;
    }
    return $filename;
}
....

And you are done