Page 1 of 1

file handling problem....

Posted: Wed Mar 28, 2007 3:58 pm
by coombesy
ok will try to explain as best i can

have users uploading files to their acc/s, and deleting from their acc/s
some files are long (eg songs etc).

to delete a file i was using

Code: Select all

while(file_exists($filename)) unlink($filename);
works ok... unless $filename is being streamed by someone!!!!
which makes sense, can't delete something thats running.

next plan was to delete $filename entry from mysql tables and leave the file in the users folder. Have an admin script that would physicaly delete any 'dead files' from the users folders at a later date.

again works ok... unless-> say a user has uploaded a song, and whilst someone else is listening to that song, the user decides to delete, then changes his mind and uploads it again, it won't upload. Cause it can't replace the orginal which is streaming.

So... what i need to do is
1) check if a file is streaming
2) cancel all streams
3) flock() the file, (i think this is the function i'll use)
4) delete it.
5) eureeeka, i won't end up throwing laptop out of a dublin window upon some unsuspecting granny!

any solutions??

Posted: Wed Mar 28, 2007 8:23 pm
by Mightywayne
Well I'm not a file master here or anything, but I can tell you that if the user removes the song, while someone is streaming it, the stream will just stop right there. If you don't want it to stop right there, it's a possibility that you could just have 'em download it, but giving the system you're describing, I doubt you want that.

Also, this might be of use to you, but you also have likely seen it. Worth a shot though.

http://w3schools.com/php/php_ref_filesystem.asp Hope I could help you somehow.

Posted: Wed Mar 28, 2007 10:05 pm
by RobertGonzalez
Can't you maintain state on the file in the database, so that anytime a user loads the file (since the paths are in the database), you can set a flag that says it is in use, allow the removal of the path from the database, then at a later date run a clean up utility that handles deletes of dead files regardless of current usage?

Posted: Thu Mar 29, 2007 6:05 am
by coombesy
thought about that one everah, been searchin for an automatic solution and was hoping to learn more about threads, streams etc etc.
think the flag idea is the most reasonable option.
If i do however find a php function i'll post it here.
thanx all.