Jump to content

Recommended Posts

Hello,

I have been trying to find the definitive script for using PHP in a form on an html page that will upload (using SFTP) a large file to a remote location... I have found some help, and had some limited success using ftp, but need to use SFTP.

 

Most comments I find all talk about the differences between SFTP and FTP, but most are from more then a year ago, and I can't find a clear answer...

 

To add complications to it, I am new to programming (I know you hate these kinds of questions I am sure)...

 

Is there an easy way to send a large file to a remote location using SFTP with PHP or for that matter, any other scripting method?

 

Sorry to be a bother, and thanks in advance for your advice...

   

Link to comment
https://forums.phpfreaks.com/topic/98469-sftp-ftp-with-php/
Share on other sites

are you asking for FTP over SSH is sometimes referred to as secure FTP or securing FTP, such as with SSL/TLS (FTPS).

 

 

*UNTESTED

<?php
$ftp_server = "xxx.xxx.xxx.xxx";
$ftp_port = 21;
$ftp_timout = 90;

$ftp_user_name = "root";
$ftp_user_pass = "secureFTP";

$remote_file = "helloworld.txt";
$file = "helloworld.txt";

// set up basic ssl connection
$conn_id = ftp_ssl_connect($ftp_server, $ftp_port, $ftp_timout);

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

//Returns the current directory name
echo ftp_pwd($conn_id);

// upload a $file to $remote_file (includeing the name)
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo "successfully uploaded $file\n";
} else {
echo "There was a problem while uploading $file\n";
}

// close the ssl connection
ftp_close($conn_id);
?> 

Link to comment
https://forums.phpfreaks.com/topic/98469-sftp-ftp-with-php/#findComment-504038
Share on other sites

Sorry Not sure... I was told the only way to connect to the server where I need to have people upload their contest submissions, was with a MAC using AFP or with a PC using SFTP (The server is MAC based "xserver")... I have tried many different FTP programs, and had only some success on PC.  I thought if I could find a script to do it all in the background, we would not have to let each of the people submitting entries, to have to download an FTP program that may or may not work...

The code you have posted looks similar to what I have been playing with for a couple of days, with a couple of changes, so I guess I will try it...

 

Thanks again for your insight,

 

 

Link to comment
https://forums.phpfreaks.com/topic/98469-sftp-ftp-with-php/#findComment-504075
Share on other sites

if you connecting to a FTP sever on a MAC or PC you can use the standard FTP protocal

its not the only way to connect to them, the xserve will probably have OSX installed this will have samba installed this allow PC users to connect to the fileserver so you can have you PHP connect via that

 

as for connecting to via FTP

i would suggest checking the PC & Mac servers FTP setup by installing a client and connecting to them and uploading a file or two, once your sure the servers FTP setup is correct then

 

create a simple FTP script in PHP

<?php
$ftp_server = "xxx.xxx.xxx.xxx";
$ftp_user_name = "user";
$ftp_user_pass = "pass";
$destination_file = "file.txt";
$source_file = "file.txt";

// 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 "Connected to $ftp_server, for user $ftp_user_name";
    }

// 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); 
?>

Link to comment
https://forums.phpfreaks.com/topic/98469-sftp-ftp-with-php/#findComment-504084
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.