gazareth
gazareth

Reputation: 1154

PHP - gracefully handle timeout with copy()

Part of my application copies files to a network share. Periodically, the network share times out when PHP does its copy() bit and the application dies with a fatal error (exceeded maximum execution time).

Is there a way to have it "give up" on the copy BEFORE it hits the maximum execution time so that it can throw an exception or give a nicer message to the user (and not clutter my error logs!)?

Upvotes: 1

Views: 583

Answers (2)

Rob W
Rob W

Reputation: 9142

Instead of working around the timeout, perhaps figure out why the shares are timing out (or if the script is timing out) instead of writing files that will end up being corrupt, wasting space and potentially causing problems later. That being said, after you have figured it out, it might be useful to use a better tool than PHP's copy such as executing rsync, xcopy, or robocopy.

Upvotes: 1

gontrollez
gontrollez

Reputation: 6548

You can fork, and execute the copy in the child process. In the main process, keep checking the time and kill the child process if limit is exceeded.

Upvotes: 0

Related Questions