Problem to Upload on a FTP using SWFUpload

Hello,

In my application I want to integrate SWFUpload to let the users Upload files on a FTP server.

In fact, I want to do the same thing that The SimpleDemo on the SWFUpload site.

In my code I have successfully integrated the SWFUpload library and it works correctly for my little uploads. The problem happens when I try to upload files which have approximately a size bigger than 5 MB.

The problem is that my progressbar reach immediately 100%, and, only after that the upload process begins. An other problem that I have is that while a file is transferring I can’t cancel and remove the upload.

From where come this problem ?

Here is my do_upload function code :


public function do_upload()
{
...
...
...

//We retrieve the file to upload
$file_tmp_path = $_FILES['Filedata']['tmp_name'];
$file_name = $_FILES['Filedata']['name'];
$file_type = $_FILES['Filedata']['type'];

//Establish a ftp connection
$conn = ftp_connect($ftp_host);
//Error
if(!$conn)
{
log_message('error', "Connexion error message");
}
else
{
//Identification
ftp_login($conn, $ftp_user, $ftp_password);

//Upload
$ret = ftp_nb_put($conn, $ftp_path, $file_tmp_path, FTP_BINARY);

while($ret == FTP_MOREDATA)
{
//upload continues
$ret = ftp_nb_continue($conn);
echo $ret;
ob_flush();
flush();
}
//upload ended
echo $ret;
ob_flush();
flush();

//Download error
if ($ret != FTP_FINISHED)
{
log_message("error", "download error message.");
}
else
{
//Download OK

}

//FTP connection close
if(!ftp_close($conn))
{
log_message('error', "FTP close connection failed.");
}
}
}
}

Can the problem happen because I want to upload my files on a FTP server ?

ps : I use the same handlers and the progressbar that the SimpleDemo.

gyphie's picture

Server side work

Your server does not start processing the file until it has been uploaded completely. So it will be normal for the file to upload to the server (reaching 100%). Then SWFUpload waits while the server does its work (in this case uploading the file to an FTP server). Then the server will respond saying it has finished (but returning an HTTP status code of 200) and SWFUpload fires uploadSuccess.

This is all normal behavior.

Some issues you will have:

1. You should return some text from your upload script so uploadSuccess fires properly.
2. In testing if the upload script took longer than 30 seconds to process the connection would end without errors and SWFUpload would appear to hang.

#2 can be difficult to fix. You need to either force the upload script to return and process the FTP upload in a separate process or you need to force the server to send some text to SWFUpload before the 30 second timeout (every 15 seconds for example). This will make sure SWFUpload recognizes the connect as still being alive and it won't just disconnect.

Thanks for your answer. I

Thanks for your answer.

I have resolved my problem and now the UploadSuccess event is fired correctly.

However, My progressbar doesn't work correctly. I don't know how I can upload my file to a FTP server while the progress continues normally (with a real progress, and not directly reach the 100%).

A strange thing is that my progressbar works correctly only if I upload my file on the server where the site is (whith the PHP move_uploaded_file() command for example).

Do you have an idea on how I can solve this progressbar problem ?

gyphie's picture

Not supported

SWFUpload does not support uploading via FTP. You're code looks like you are uploading to the web server and then the web server is uploading to an FTP server. SWFUpload is only involved in uploading to the web server. The progress bar reflects this. As the upload script sends the file via FTP SWFUpload is not involved. It is simply waiting (at 100%) for the web server to finish whatever it is doing.