Upload a tar file to a ftp server

Hi,

I need some help from you guys.
I need to create a php script to upload a tar file to a ftp server.
The tar file is located at www.site.com/file.tar and I want it uploaded to ftp.mysite.com to directory public_html/files/

This is extremely urgent.
bogdan300Asked:
Who is Participating?
 
Marcus BointonConnect With a Mentor Commented:
This is an adapted version of the same code that also does the download:

<?php
//Download the source file
//Note that current directory must be writable
$tar = file_get_contents('http://www.site.com/file.tar');
$fp = fopen('file.tar', 'wb');
fwrite($fp, $tar);
fclose($fp);

//Upload
$conn_id = ftp_connect('ftp.mysite.com');
// login with username and password
$login_result = ftp_login($conn_id, 'username', 'pass');
// check connection
if ((!$conn_id) || (!$login_result)) {
      echo "FTP connection has failed!";
      echo "Attempted to connect to $ftp_server";
      exit;
} else {
      echo "Connected to $ftp_server, for user $ftp_user_name";
}

// upload the file
$upload = ftp_put($conn_id, 'file.tar', 'file.tar', FTP_BINARY);

// check upload status
if (!$upload) {
      echo "FTP upload has failed!";
} else {
      echo "Uploaded";
}

// close the FTP stream
ftp_close($conn_id);
?>
0
 
Jaime OlivaresSoftware ArchitectCommented:
This is a typical upload routine:

// set up basic connection
$conn_id = ftp_connect($ftp_server);


// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection
if ((!$conn_id) || (!$login_result)) {
        echo "FTP connection has failed!";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name";
        exit;
    } else {
          echo '<br><br>';
        echo "Connected to $ftp_server, for user $ftp_user_name";
    }
     
$ftp_dir = ftp_pwd($conn_id);
$destination_file = $ftp_dir.$sourcefile_name;
echo "<br>$destination_file";

// upload the file
$upload = ftp_put($conn_id, $destination_file, $sourcefile, FTP_BINARY);

// check upload status
if (!$upload) {
        echo "<br><br>FTP upload has failed!";
    } else {
        echo "<br><br>Uploaded $sourcefile_fix to $ftp_server as $destination_file";
    }

// close the FTP stream
ftp_close($conn_id);

You will need to have downloaded your file previously with filename described by $sourcefile
0
 
bogdan300Author Commented:
Yes I seen this example on php.net, but I am not able to make it upload.
Please give me more detailed help with an example.
0
All Courses

From novice to tech pro — start learning today.