Solved

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

Posted on 2007-11-22
11
571 Views
Last Modified: 2008-03-04
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.
0
Comment
Question by:DoppyNL
  • 5
  • 4
  • 2
11 Comments
 
LVL 4

Expert Comment

by:simonkin
ID: 20334460
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

0
 
LVL 6

Author Comment

by:DoppyNL
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.

0
 
LVL 4

Expert Comment

by:simonkin
ID: 20334563
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

0
 
LVL 6

Author Comment

by:DoppyNL
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.
0
 
LVL 4

Expert Comment

by:simonkin
ID: 20334710
I'm sorry,

Is the Event 1 actually carried out on an FTP client or via a php ftp function?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Author Comment

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

Expert Comment

by:simonkin
ID: 20334871
Oh....

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

Why don't you try that and then use http://www.experts-exchange.com/M_4284069.html
0
 
LVL 20

Accepted Solution

by:
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

http://www.php.net/manual/en/function.filemtime.php

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.
0
 
LVL 6

Author Comment

by:DoppyNL
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.
0
 
LVL 20

Assisted Solution

by:steelseth12
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.
0
 
LVL 6

Author Comment

by:DoppyNL
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!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now