automatic processing of ftp-uploaded files; upload done or not?

Posted on 2007-11-22
Last Modified: 2008-03-04

I've got a system that processes images and movies for further use.
To make it easyer for some users, we are making a system that will allow them to upload files to an ftp-server. From there the system will automaticly process the files.

Since it is an automated process, it will be run quite often, to allow quick processing of the files.

However, a file should not be processed when the upload of the file is not complete.

How can I detect in PHP that a certain file was uploaded completely?
Filetypes that can be used are jpg, avi, mpeg, wmv, html, etc.
Question by:DoppyNL
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2

Expert Comment

ID: 20334460

Try this...

Just call the function to validate the upload...


	// Set some variables
	$filename = $_FILES["file"]["name"];
	$original_size = $_FILES["file"]["size"];
	// Move the upload file from tmp to wherever
	move_uploaded_file($temp_path, $new_path);
	// The function to check the size
	function checkSize($filename, $new_path, $original_size) {
		// Make sure the file exists
		if (file_exists($new_path . $filename)) {
			// Get the size of the uploaded file on the server
			$filesize = filesize($new_path . $filename);
			// Check that the filesizes match
			if ($filesize == $original_size) {
			// File sizes match
			return true;
		} else {
			// Filesizes don't match
			return false;

Open in new window


Author Comment

ID: 20334483
The file is being uploaded to an FTP-server. NOT via HTTP-POST!
a php-script will then read a specific dir and find files in that dir to process.

Chances are the file is still being uploaded via the ftp-server when the php-script tries to process it; since both these processes are seperate.


Expert Comment

ID: 20334563

Ok, I see...

Taken from

    // Set the filehandle
	$fh = fopen ($file_name, "r");
    // Perform the upload
	$ret = ftp_nb_fput ($ftp, $file_name, $fh, FTP_BINARY);
	// Check the status while uploading
    while ($ret == FTP_MOREDATA) {
		// Print the status to the screen
		print ftell ($fh)."\n";
        // Keep uploading...
		$ret = ftp_nb_continue($ftp);
	// If the returned status is not finished
    if ($ret != FTP_FINISHED) {
		// Print an error message to screen
		print ("error uploading\n");
		// Stop script execution
	// Close the file handle

Open in new window

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.


Author Comment

ID: 20334592
Nice function, but I'm sorry to tell that is not what I meant.

I'll try to be more specific.
What happens is as follows:

Event 1:
- User logs in on FTP-server (manually)
- Uploads 1 or more files
- Closes FTP-connection

Event 2:
- PHP-script is run (automaticly; SEPERATE from the user)
- PHP-script checks for new files on ftp-server
- PHP-script processes new files.

Since both Event 1 and Event 2 are initiated completely seperate it is possible they are run simultaneously. In that event it is possible the user is stil waiting for his upload to be finished and the file is still incomplete. In that case I don't want Event 2 to process the file and leave it for the next time it is run.

Expert Comment

ID: 20334710
I'm sorry,

Is the Event 1 actually carried out on an FTP client or via a php ftp function?

Author Comment

ID: 20334720
Event 1 is done by a user using a normal ftp-client on windows.
Event 2 is a cronjob

Expert Comment

ID: 20334871

I think you need to make a ftp upload function in php.

Why don't you try that and then use
LVL 20

Accepted Solution

steelseth12 earned 125 total points
ID: 20335424
Hi DoppyNL
What you need to do is loop through the directory once and create an array of the filenames and the modification time of the files using the filemtime() function

Then loop through again or loop through the array you created and use the filemtime() function again to see if the modification time has change. If it has changed then it means that the file is still being uploaded.

Please let me know if you need more help.

Author Comment

ID: 20337950
Do I need to add a considerable delay between the tests? I can imagine that the file doesn't have to change for a second, simply because the users connection is slow.
A delay of a minute would probably be a optimal delay?

Starting to work on that now, may take a little time.
LVL 20

Assisted Solution

steelseth12 earned 125 total points
ID: 20337976
Yes it would be a good idea to put some delay not because of slow connections but mostly because the connection of the ftp might break up and the user would need to reconnect and resume the upload.
A 1 minute delay would work fine.

Author Comment

ID: 21039883
Sorry for the late reply, I completely forgot about this question.

There is now a cronjob running every minute that checks for files on the ftp-server. When a file is new it is stored in a database the file is there. The second time the process runs it will check if the file has changed in the meantime, if not the file is processed, if it is changed the cronjob skips it for a later attempt.

tnx for the help!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question