Bug report on webkit.org
Below is 2 form.
First one has only a simple texte input.
Second one has a file input.
On Service Worker, a header X-FROM-SW is added with the user timestamp.
On Server side, a simple JSON is returned containing this header, or false if not found.
On Chrome and Firefox, the from-sw is correctly added.
On Safari, it's added only on the simple text form.
It's not working on Uploaded form, where it doesn't go to Service Worker fetch event.
test.js file
sw.js file
As suggested in the third comment here,
the last form read the file, transform it into a dataUrl and send it through regular POST data.
Then, on server side, the followin PHP code is used ASAP in the code in order to recreate the same behavior than regular file:
if (isset($_POST['_files'])) { foreach ($_POST['_files'] as $name => $file) { list($type, $data) = explode(';', $file['data']); $type = substr($type, strlen('data:')); list(, $data) = explode(',', $data); $data = base64_decode($data); $tmpName = tempnam('/tmp', 'phpFile_'); $handle = fopen($tmpName, 'w'); fwrite($handle, $data); fclose($handle); $_FILES[$name] = [ 'name' => $file['name'], 'type' => $type, 'tmp_name' => $tmpName, 'error' => 0, 'size' => filesize($tmpName), ]; } unset($_POST['_files']); }
Then, when you want to move the file to another location, you'll have to use this code:
if (isset($_FILES['file'], $_FILES['file']['tmp_name'])) { if (is_uploaded_file($_FILES['file']['tmp_name'])) { @move_uploaded_file($_FILES['file']['tmp_name'], __DIR__.'/upload/'.$_FILES['file']['name']); } else { @rename($_FILES['file']['tmp_name'], __DIR__.'/upload/'.$_FILES['file']['name']); } }