Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pag66's avatar
Level 5

Unable to open file for reading - Swift_IoException

Hello everyone, I'm trying to send a email using attach as is show in the documentation, with s3 files attached works fine, but if a change to local storage I get the error :

Swift_IoException
Unable to open file for reading

to use local follow the recomendation to make the symbolic link and the local files display but it doesn't attach using this command:

ln -sr public/storage storage/app/public

Here is my controller

public function sendEmailLab($paciente,$carpeta,$archivo){
       $parametros = Parametro::first();
       $pac = Paciente::where('id_pac','=',$paciente)->first();
       $path = Storage::url("almacenamiento/$carpeta/$archivo");
       Mail::send('email.laboratorio',['title'=>$parametros->laboratorio_cabecera,
           'content'=>$parametros->laboratorio_detalle], function($message) use ($parametros, $path,$pac) {
               $message->to($pac->correo_pac);
               $message->subject($parametros->laboratorio_cabecera);
               $message->attach($path);
           });
        return back();;
   }

The view is just calling the method

<td>
                            <a href="{{ url('carpetas',array($pac->id_pac,$directorio->nombre, $archivo->nombre,'mail'))   }}" class="btn btn-default"><span class="glyphicon glyphicon-envelope" aria-hidden="true"></span></a>
                          </td>

Please give a tip

Greetings

0 likes
8 replies
Shocm's avatar

Looks like a permissions issue. Have you checked the permissions of the file and make sure the Laravel application has what it needs to read it?

pag66's avatar
Level 5

as you say has only read permission in the public/store link, how I could give more permissions ?

Shocm's avatar

Not knowing what user you are running the app as and assuming you are on a Unix based system you can test to see if it's a permissions issue by changing it with chmod 777 path/to/file .. If this works you will want to dial those permissions back to what you actually need but first lets make sure that is the issue. Also, please note the current permissions because you are going to want to set it back to those permissions when you are done.

pag66's avatar
Level 5

I change the permissions but it's still give me the same message,

Swift_IoException in FileByteStream.php line 144:
Unable to open file for reading [/storage/almacenamiento/laboratorio_s3/H00430laboratorio_s314726645071471621866codigo_barras.pdf]
pag66's avatar
pag66
OP
Best Answer
Level 5

I made some changes in my controller to solve it, I don't know if there is a better solution but until then I put my solution here.

 public function sendEmailLab($paciente,$carpeta,$archivo){
        $parametros = Parametro::first();
        $pac = Paciente::where('id_pac','=',$paciente)->first();
        //s3-public validation
        if (config('filesystems.default') == 'public'){
            $path =realpath("storage/almacenamiento/".$carpeta."/".$archivo);  
        }else {
            $path = Storage::url("almacenamiento/$carpeta/$archivo");    
        }
        Mail::send('email.laboratorio',['title'=>$parametros->laboratorio_cabecera,
            'content'=>$parametros->laboratorio_detalle], function($message) use ($parametros, $path,$pac) {
                $message->to($pac->correo_pac);
                $message->subject($parametros->laboratorio_cabecera);
                $message->attach($path);
            });
         return back();;
    }

Thaks for your replies

1 like
iskren.dimov's avatar

For some wierd reason, SwiftMailer fails if you pass URL to it, but it works just fine with realpath. And of course, to make it more spicy, it works both ways on my setup of mine, and it works only with realpath on another...

1 like
marcoacm's avatar

I tried realpath and still get the same result. Any other ideas?

forerser's avatar

if anybody still has the error, make sure your path doesn't have the / at the beginning

$path =realpath("storage/...

or

$path =public_path("storage/...

Please or to participate in this conversation.