So here is my stream copy function that tries to detect errors, but it still seems to not work (and I thought that fwrite should have returned the correct number of bytes)
function stream_copy ($ in, $ out, $ limit = null, $ offset = null) {
$ bufsize = 32 * 1024;
if (isset ($ offset)) fseek ($ in, $ offset, SEEK_CUR);
while (! feof ($ in)) {
if (isset ($ limit)) {
if (! $ limit) break;
$ data = fread ($ in, min ($ limit, $ bufsize));
} else
$ data = fread ($ in, $ bufsize);
$ datalen = strlen ($ data);
$ written = fwrite ($ out, $ data);
if ($ written! = $ datalen) {
return false; // write failed
}
if (isset ($ limit)) $ limit - = $ datalen;
}
return true;
} In the above function, I still get "true" even if an error is displayed.
So, I will just try to connect to the PHP error handler. Did not test the following, but I assume that it should work
function stream_copy_to_stream_testerr ($ source, $ dest) {
$ args = func_get_args ();
global $ __ stream_copy_to_stream_fine;
$ __ stream_copy_to_stream_fine = true;
set_error_handler ('__ stream_copy_to_stream_testerr');
call_user_func_array ('stream_copy_to_stream', $ args);
restore_error_handler ();
return $ __ stream_copy_to_stream_fine;
}
function __stream_copy_to_stream_testerr () {
$ GLOBALS ['__ stream_copy_to_stream_fine'] = false;
return true;
} Awful, but the only solution I see.
source share