Here is the piece of code:
public function uploadPhoto(){
$filename = '../storage/temp/image.jpg';
file_put_contents($filename,file_get_contents('http://example.com/image.jpg'));
$photoService->uploadPhoto($filename);
echo("If file exists: ".file_exists($filename));
unlink($filename);
}
I am trying to do the following things:
- Get a photo from a URL and save it in a temp folder in my server. This works fine. The image file is created and echoes
If file exists: 1
whenecho("If file exists: ".file_exists('../storage/temp/image.jpg'));
. - Pass that file to another function that hanldes uploading the file to Amazon s3 bucket. The file gets stored in my s3 bucket.
- Delete the photo stored in the temp folder. This doesn't work! I get an error saying:
unlink(../storage/temp/image.jpg): Resource temporarily unavailable
If I use rename($filename,'../storage/temp/renimage.jpg');
instead of unlink($filename);
i get an error:
rename(../storage/temp/image.jpg,../storage/temp/renimage.jpg): The process cannot access the file because it is being used by another process. (code: 32)
If I remove the function call $photoService->uploadPhoto($filename);
, everything works perfectly fine.
If the file is being used by another process, how do I unlink it after the process has been completed and the file is no longer being used by any process? I do not want to use timers.
Please help! Thanks in advance.
$_SERVER['DOCUMENT_ROOT']
in your filepath rather than../
– Lutzclearstatcache()
beforeecho("If file exists: ".file_exists($filename));
php.net/manual/en/function.clearstatcache.php and maybe$photoService->uploadPhoto
does the unlink before you. – Feignedimage.jpg
,maybe diff. requests try to work on the same file. – Feignedunlink
but beforefile_exists
and who knows whatuploadPhoto
does. ;-) And i pasted the link, so the OP can read about it, to get it. – FeigneduploadPhoto
does is creating an AWS client and uploading the photo to my s3 bucket. @Feigned – Selle