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
Solved

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

Posted on 2007-11-22
11
573 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Displaying text in text field when clicking on ajax search result 10 39
Ajax and PHP 4 31
Ajax and PHP 9 30
Reference key in foreach loop 4 21
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

861 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