Solved

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

Posted on 2007-11-22
11
570 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I'm sorry,

Is the Event 1 actually carried out on an FTP client or via a php ftp function?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 6

Author Comment

by:DoppyNL
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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.

771 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

10 Experts available now in Live!

Get 1:1 Help Now