php - unlink throws error: Resource temporarily unavailable

Just had to deal with a similar Error.

It seems your $photoService is holding on to the image for some reason... Since you didn't share the code of $photoService, my suggestion would be to do something like this (assuming you don't need $photoService anymore):

[...]
echo("If file exists: ".file_exists($filename));
unset($photoService);
unlink($filename);
}

The unset() method will destroy the given variable/object, so it can't "use" (or wharever it does) any files.


Simplest solution:

gc_collect_cycles();
unlink($file);

Does it for me! Straight after uploading a file to amazon S3 it allows me to delete the file on my server.

See here: https://github.com/aws/aws-sdk-php/issues/841

The GuzzleHttp\Stream object holds onto a resource handle until its __destruct method is called. Normally, this means that resources are freed as soon as a stream falls out of scope, but sometimes, depending on the PHP version and whether a script has yet filled the garbage collector's buffer, garbage collection can be deferred. gc_collect_cycles will force the collector to run and call __destruct on all unreachable stream objects.

:)