Page 1 of 1

[SOLVED] Large files uploading with FTP

Posted: Sat Jan 14, 2012 10:58 pm
by mlatelcom
Hello everyone,
After an exhausting search for more than 5 hours and reading almost every php forum on internet; I finally decided to post here to see if I can get some help. I have a php script which is basically a form to upload a single file. But unfortunatelly I can't get it to work for large files. The purpose of this is because some friends want to share files with me sometimes and I want to use this form since emails don't allow to send files of about 2GB or 4GB size. the script uses ftp connections since http does not support such a files sizes. I've changed some things in my php.ini but with no success. It does work with small files. At this point I don't know what to do.
Here is my actual php config(only changed memory limit and post max size):

Code: Select all

max_execution_time = 30
max_input_time = 60
memory_limit = 3072M
post_max_size = 5120M
default_socket_timeout = 60
and Here is the script:
INDEX.PHP

Code: Select all

   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>some title here</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
.center{
margin-left:30%;
margin-right:30%;
width:40%;
padding:0 0 23px 25px;
background-color:#b0e0e6;
}
</style>
</head>

<body>
<div class="center" ><form enctype="multipart/form-data" action="upload.php" method="post">
  <p><br>
    <br>
<h1>Upload File</h1></p>
  <br />
  <label>Select User:</label>
  <select name="usr">
	<option value="<?php echo 'user1'?>">user1</option>
	<option value="<?php echo 'user2'?>">user2</option>
	<option value="<?php echo 'user3'?>">user3</option>
  </select>
  <br />
  <br />
  <label>Password:</label>
  <input type="password" name="pwd">
  <p>
  <input name="userfile" type="file" />
  </p>
  <p><br>
    <input type="submit" value="Submit" />
    <br>
  </p>
</form></div>
</body>
</html>
UPLOAD.PHP

Code: Select all

<?php

set_time_limit(9000000);
$ftp_server = "ftp.myserver.com";
// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");

// try to login
//if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
if (@ftp_login($conn_id, $_POST['usr'], $_POST['pwd'])) {
echo '<div style="margin-left:30%;margin-right:30%;width:40%;padding:0 0 23px 25px;background-color:#b0e0e6;" ><h3>Connected to server. Please wait...</h3></div>';

} else {
echo "Connection was unsuccessful";
}
$remote_file = $_FILES['userfile']['name'];;
//v important this one as you have to use the tmp_file created for the actual upload
$file = $_FILES['userfile']['tmp_name'];
if (ftp_put($conn_id, $remote_file, $file, FTP_BINARY)) {
echo '<h1 style="text-align:center;">Successfully Uploaded File</h1>';
} else {
echo "Error uploading the file";
}

// close the connection
ftp_close($conn_id);
?>
Just wrote the whole code so anyone can use it for uploading small files.

Re: Large files uploading with FTP

Posted: Sun Jan 15, 2012 5:51 am
by twinedev
It doesn't matter that you have the script using FTP to put the file where you want it, you still have the issue that the file still needs to get to your server for the script to even do anything with it. So you are still held by the limitations of the browser doing a POST to the server with the file as part of it, where you will either get a problem of it timing out (no way you are going to have people uploading a 2-4gig file in under 60 seconds), or once it does upload, it determines it is too large and kills it.

You need something that will make the FTP connection FROM THE CLIENTS SIDE (php is sever side execution). This is going to be either they run an actual FTP client (Filezilla is a good free one) or you embed on in the page which is either Flash or Java.

-Greg

Re: Large files uploading with FTP

Posted: Mon Jan 16, 2012 3:05 pm
by mlatelcom
I understand. if I'm using a POST method I'll have the browser limitations. I have just started learning PHP; but I got your point. I've been using filezilla for years under linux; but I just want something easier for my friends to share things with me. most of them don't want to learn how to use an FTP client and some don't even try. actually the reason why I'm doing this is because I already found a java app with drag and drop capablilities, very good by the way. I tested it with a 3.5GB file and it works like a charm; but I was talking to a friend who wanted to send me a wedding video and she gave up when she got the message to install java in order to run the app in her browser. she just said "the computer is asking me to install something...this is to hard for me". so you can figure out why I rather to have a hard time learning myself instead. I will go for a flash app since she has flash player installed(she watch videos on youtube). thanks a lot for your help and have a great one.