Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Uploading Large Files: HTTP and FTP

Status
Not open for further replies.

Glowball

Programmer
Oct 6, 2001
373
US
I've searched through a lot of posts here and on other forums and also read through the PHP docs. I have the same concerns that a lot of the other posts have but I don't think my FTP questions have been answered...

I need to upload large files (50MB max) from client machines not running PHP (usually Windows) to a *nix server I do not have telnet access to. I can successfully upload files to it using command line from my Windows machine.

Am I correct in assuming that I can NOT use FTP functions to move these files? Reading other posts makes me believe this is not possible without PHP on the client machines.

Is extending the timeouts the only thing I can do to allow a 50MB file to move to the server using HTTP?

Thanks for any and all input! I'm just looking for the answers to sum up the information I've found out there.
 
ColdFusion has a similar tag and the manual for CFFTP says that you use FTP functions to move files between the ColdFusion server and an FTP server. Moving files from a client browser to the ColdFusion server requires the standard upload functions.

Is the same thing true for PHP?

I wish I would have read the ColdFusion manual about 3 hours ago...
 
I figured it out! Turns out ColdFusion and PHP handle things differently. The trick is to change directories on the server first.

I added this as an FAQ but I'll post it here too so it's easy to find.
Code:
-------------------------------------------
ftp_example.php
-------------------------------------------
<?php
if (isset($_POST['upload']) && $_POST['upload'] == 1) {
    // set up the variables
    $ftp_server = "server_name_or_IP_address";
    $ftp_user = "username";
    $ftp_password = "password";
    $destination_directory = "some/directory/from/default/ftp/directory";
    $destination_file = "ftp_upload_test.gif";
    
    // set up basic connection
    $conn_id = ftp_connect($ftp_server);
    
    if (!$conn_id) {
        echo "Could not connect!";
        exit;
        }
    else echo "Connected...<br>\n";
    
    // login with username and password
    $login_result = ftp_login($conn_id, $ftp_user, $ftp_password);
    
    if (!$login_result) {
        echo "Could not log in!";
        exit;
        }
    else echo "Logged in...<br>\n";
    
    // set to passive mode
    $set_passive = ftp_pasv($conn_id, true);
    
    if (!$set_passive) {
        echo "Could not set to passive mode!";
        exit;
        }
    else echo "Connection is set to passive mode...<br>\n";
    
    // change directories
    $change_dir = ftp_chdir($conn_id, $destination_directory);
    
    if (!$change_dir) {
        echo "Failed to change directory!";
        exit;
        }
    else echo "Changed directory...<br>\n";
    
    // upload the file
    $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 
    
    // check upload status
    if (!$upload)
        echo "FTP upload has failed!";
    else
        echo "Uploaded $source_file to $ftp_server as $destination_file";
    
    // close the FTP stream 
    ftp_close($conn_id); 
    
    echo "\n<hr>\n";
    }
?>

<form action="ftp_example.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="upload" value="1">
<b>GIF File to upload: </b><input type="file" name="source_file"><br>
<input type="submit" value=" Upload ">
</form>
 
I've added some more checking so referece faq181-86 for the best and most up-to-date script.
 
It would be nice if we could edit our posts... the real FAQ is faq434-5750 and I have no idea where that last one came from. Sorry about that!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top