Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flock() an image?

Tags:

php

flock

I am looking to flock() an image.

Currently I am using the following

$img = ImageCreateFromPng($img_path);
flock($img,LOCK_EX);

It seems that the GD library's file handle is not valid with flock. How can I access the image and flock the file?

like image 502
Pablo Avatar asked Dec 14 '25 11:12

Pablo


1 Answers

The function flock only works on file handles (or stream wrappers if they support locking). So, if you want to lock an image when you read it, you'd need to open it twice:

$f = fopen($imgPath, 'r');
if (!$f) {
    //Handle error (file does not exist perhaps, or no permissions?)
}
if (flock($f, LOCK_EX)) {
    $img = imagecreatefrompng($imgPath);
    //...  Do your stuff here

    flock($f, LOCK_UN);
}
fclose($f);
like image 195
ircmaxell Avatar answered Dec 17 '25 02:12

ircmaxell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!