Jump to content

Recommended Posts

Hi peeps, I'm building a PHP application that needs to be able to write to zip file entries. This works fine most of the time and I just use "ZipArchive::addFromString(...)" to write my data. However, problems occur when the server starts writing to the same zip entry simultaneously. It appears that "ZipArchive::addFromString(...)" is not thread safe because I end up with a garbled combination of the simultaneous writes.

 

So, what I need is a way of synchronizing PHP server threads. I've tried using flock on the zip file but this is slow and doesn't work on a Windows server. What I really need is a way of doing a global mutex per user (each user has a unique zip file). Anyone got any ideas?

 

Thanks,

Kal.

Link to comment
https://forums.phpfreaks.com/topic/85960-solved-php-server-thread-synchronisation/
Share on other sites

(not tried any of this with zippin)

Flock is technically the way to go, wouldn't know if it does'nt work with windows.

 

However, use a similar way as to flock, before a process attempts to get a handle on a file, check for a 'locking' file, if there isn't one, then create one, do your stuff with the tar, then delete the locking file you made.

 

Thanks tinker, it sounds like that would work. Unfortunately I think it might be a bit slow, this is part of a virtual file system (each user has a virtual drive represented by a zip file on the server) and so the method used to synchronize writes needs to be as simple as possible without any excess disk access if possible.

could use a db instead, if not I can't see another way to how your going to specify ownership across the board. At some point your going to have to bite the bullet. And how relative is slow? For instance is size more of an issue or speed, because if it's speed then why not use pre-sized binary files, with some kind of FAT. This would allow reads at the same time as a single write, but in the FAT you could allocate a flag for 'being written to'?

it would if more than one person has access to the files. And if I remember correctly the session vars arn't truly written until the page finishes, therefore they won't be updated until after the zip stuff had finished stuffing the turkey, in the case of a single user.

 

Have you thought of writing some shell scripts or c progs and calling them via php, you'd still have to use similar style locks, but hey, thats the way it's done.

In case anyone else encounters this problem I've also just discovered that the flock() function only works if your running php in cgi mode (ie: php.exe). If you use flock from php.dll (only loaded once by the apache process) flock will always return true because there is only one process (apache). So in other words flock is broken, don't use it.

I've found a locking solution that works in php (on both linux and windows)...

 

/*
Wait for the lock...
*/
do{
clearstatcache();
}while(!is_writable($filename_sync));

/*
Issue the lock...
*/
chmod($filename_sync, 0400);
clearstatcache();


/*
Do your sensitive bit of code (writing to files or whatever)...
*/


/*
Issue the unlock...
*/
chmod($filename_sync, 0600);
clearstatcache();

 

Unfortunately this doesn't work for zip file access because the new ZIPARCHIVE system in PHP 5.2.0 is fundamentally not thread safe. The zip file changes are cached in memory until $zip->close() is called at which point the changes are flushed to disk. This wouldn't normally be a problem, however $zip->close() is a non-blocking call which means that multiple $zip->close() calls can be honored at the same time even if you protect it with a lock like the one described above.

 

So, if anyone is thinking of trying it, don't use php zip file writes on a multi-threaded OS.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.