Hello all. New guy here, so please go easy on me.
I am using plupload to upload files on a work website. The upload itself works flawlessly, but then I run into trouble. The website is hosted on a linux server with several other boxes mounted, whereof one file server and another server where the files are processed (let's call this the process-server). I.e. there are three separate file systems involved.
First copy: When the file upload is finished, the file(s) are copied from the linux server tmp-folder to the file server. This NEVER fails.
Second copy: Once on the file server, I proceed to copy the file from the file server to a watched folder the process-server. This failed about 40% of the time. I.e. it WORKS more often than not, but not 100% of the time. The only error I ever get is "No such file or directory in...". I do know for a fact, however, that this is wrong. The directory is there. I have done extensive logging if this procedure and watched it happen in front of my eyes. The directory NEVER exists when the upload starts, but is created before I get to the part where this error occurs.
Considering the possibility that the file is picked up from the watched folder before it is finished copying, I decided to FIRST copy the file to a NON-watched folder on the processor-server and then renaming it from the non-watched folder to the watched folder, since copying a file between file systems DOES take time. However, once on the correct file system, the rename:ing of the file is instant as the file is not physically moved. It's just file file system pointer that is changed.
This improved the success rate greatly, but not to 100%. Now I suffer this problem perhaps 10% of the time.
Below is the relevant code. What I do not understand is how this works most of the time but SOMETIMES fails. There is no consistency, this error occurs on large files and small files alike. This is all done on our intranet, so "slow internet connection" is not an issue. As you can see, the files are given a new name when copied to the file server. $filename contains a valid filename, without spaces, special characters and other weird characters.
Is it possible that the copy command returns true before the file is finished copying and allows the code continue to run? I thought I would not enter an if-statement block until the file was successfully copied and done. Is it thus possible that the rename executes and tries to rename the file before the copy is finished?
if (copy("{$filePath}.part", $fileServerPath.$filename)) { // File copied from webserver to file server. This NEVER fails.
if (!copy($fileServerPath.$filename,$processServerPathTmp.$filename)) { // File copied from file server to process server tmp. This NEVER fails.
error_log("Filehandler: Could not copy $filename from file server to process server tmp");
} else {
if (!rename($processServerPathTmp.$filename,$processServerPath.$filename)) { // File is moved from process server tmp to process server watched folder. This fails about 10%-20% of the time.
error_log("Filehandler: Could not move $filename from process server tmp to process server");
}
}
return true;
}