Upload files into SFTP server using PHP

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Upload files into SFTP server using PHP



This is my first time using sftp, and I'm stuck "This was pedictable"...I have a csv file dowloanded via web browser into my Downloads Directory using a "Webservice" permetting such thing, and I wanna upload this file into a sftp server.
I've tried many codes that I've found here or on other websites but none of them worked. Here is two examples of codes I tried to run:



FISRT EXAMPLE:


<?php
$ftp_server = 'example.server.net'; // Address of FTP server.
$ftp_user_name = 'username'; // Username
$ftp_user_pass = 'password'; // Password

$destination_file = "serverpath"; //where you want to throw the file on the
webserver (relative to your login dir)

$conn_id = ftp_connect($ftp_server,22) or die("<span style='color:#FF0000'>
<h2>Couldn't connect to $ftp_server</h2></span>"); //set up basic connection

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("
<span style='color:#FF0000'><h2>You do not have access to this ftp server!
</h2></span>"); //login with username and password, or give invalid user
message
if ((!$conn_id) || (!$login_result)) { // check connection
// wont ever hit this, b/c of the die call on ftp_login
echo "<span style='color:#FF0000'><h2>FTP connection has failed! <br />";
echo "Attempted to connect to $ftp_server for user $ftp_user_name</h2>
</span>";
exit;
} else {
echo "Connected to $ftp_server, for user $ftp_user_name <br />";
}

$newnamefile= 'in.txt';
$filenameonlocal= 'in.txt';
//important
$upload = ftp_put($conn_id, $destination_file.$newnamefile, $filenameonlocal,
FTP_BINARY); // upload the file

//le contenu a inserer dans le fichier Log
//modifier $contenu vers log selon besoin
$contenu= 'Export done'.PHP_EOL;


if (!$upload) { // check upload status
$contenu= 'ERROR SUR LE DEPOT DU FICHIER: '.$filenameonlocal.'Nouveau Nom :
'.$newnamefile.PHP_EOL;
$h = fopen("logerror.txt", "a");
fwrite($h, $contenu);
} else {
echo "<span style='color:#339900'><h2>Uploading $newnamefile Completed
Successfully!</h2></span><br /><br />";
$h = fopen("log.txt", "a");
fwrite($h, $contenu);
}


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

?>



SECOND EXAMPLE:


<?php
include('./Net/SFTP.php');
set_time_limit(28800);
…………………
…………………
$SFTPServer='example.server.net';
$SFTPUser='username';
$SFTPPassword='password';
$SFTPDirectory='/usr/share/nginx/html';
$folder='/SFTP';
$lastNameFile='';
$LastPathFileNameTemp='';
$SysSeprator='';

//Send to the sftp
if($issFTP && $isError==false){
$sftp = new Net_SFTP($SFTPServer);
if (!$sftp->login($SFTPUser, $SFTPPassword)) {
$LOG["SFTP"] = "ERROR : LOGIN FAILED";
$isError=1;
}
set_time_limit(300); //reset time limit to another 5 minutes. added 20131121
to cause script timeout if sftp put hangs for an individual file

if($isError==false){
if($folder!=""){
if(!($sftp->stat($SFTPDirectory.$folder))){
if(!($sftp->mkdir($SFTPDirectory.$folder))){
$LOG["SFTP"] = "ERROR : Create folder :
".$SFTPDirectory.$folder;
$isError=1;
}
}
}
}

if($isError==false){
if($sftp->put(
$SFTPDirectory.$folder.$SysSeprator.$lastNameFile,
$LastPathFileNameTemp,NET_SFTP_LOCAL_FILE)){
$LOG["SFTP_FILE_SIZE"] = $sftp->size($SFTPDirectory.$folder.
$SysSeprator.$lastNameFile);
if($LOG["SFTP_FILE_SIZE"]==filesize($LastPathFileNameTemp)){
$LOG["SFTP"] = "OK";
}else{
$LOG["SFTP"] = "ERROR (SIZE)";
$isError=1;
}

}else{
$LOG["SFTP"] = "ERROR";
$isError=1;
}
}
}
?>





FTP and SFTP are not the same protocol... SFTP uses SSH so your first example is invalid.
– Devon
7 mins ago





Great, first illumination...So the second one is a more logical code to use, but it somehow doesn't work. Any ideas why?
– BaDumTss
6 mins ago





You'll have to give a better explanation other than it doesn't work. What doesn't work? Where does it fail?
– Devon
2 mins ago









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

C# - How to create a semi transparent or blurred backcolor on windows form

Swipe gestures in WKWebView

How to populate data on nav-tab in partial View in MVC?