Link to home
Start Free TrialLog in
Avatar of DoppyNL
DoppyNL

asked on

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

Hi!

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.
Avatar of simonkin
simonkin

Hi,

Try this...

Just call the function to validate the upload...

Code:


<?php
	
	// 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

Avatar of DoppyNL

ASKER

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.

Hi,

Ok, I see...

Taken from http://us.php.net/ftp_nb_fput


<?php
 
    // 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
		exit(1);
    }
    
	// Close the file handle
	fclose($fh);
 
?>

Open in new window

Avatar of DoppyNL

ASKER

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.
I'm sorry,

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

ASKER

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

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

Why don't you try that and then use https://www.experts-exchange.com/M_4284069.html
ASKER CERTIFIED SOLUTION
Avatar of steelseth12
steelseth12
Flag of Cyprus image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DoppyNL

ASKER

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DoppyNL

ASKER

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!