-1

I have file on my local and I want to upload that file on ftp server on specific folder but I am not able to do it, Can anyone give some light on it. Below is my code.

$ftp = ftp_connect("server path",22);    

ftp_login($ftp,username,password);    

$ret = ftp_nb_put($ftp,
    "/incoming/in/",
    "http://localhost:8888/cardManagement/newfile.dat", 
    FTP_BINARY, 
    FTP_AUTORESUME);

print_r($ret);

Note- It's SFTP

  • Any errors? As far as I know, if the FTP server isn't set up to be locked in a default folder, you might be trying to write in a folder /incoming on the root path of the server. – Janno Jun 05 '17 at 08:00
  • Maybe the second example can help you : http://php.net/manual/en/function.ftp-nb-put.php – Taha Paksu Jun 05 '17 at 08:02
  • @janno it just loading the page – nitesh Sharma Jun 05 '17 at 08:03
  • 1) Any reason why you are using `ftp_nb_put` and not the basic `ftp_put`? 2) *"I am not able to do it"* + *"but its not working"* - What does that mean? We need a problem description. 3) Why are you using `http://localhost:8888/` to refer to a *"local"* file? 4) Your original code with a full path to a remote file was correct, why did you remove the filename? – Martin Prikryl Jun 05 '17 at 08:10
  • @MartinPrikryl 1.No reason I just found the example so I used that if you have better approach then feel free to share 2.It means the code I have its not working I want to upload one file from local to ftp server folder 3.By this path I am Able to access but I can change that path. 4.Boz there will not be any file exist at first so we have to upload there . is it neccessary that there should be some file there and we can only replace ? – nitesh Sharma Jun 05 '17 at 08:34
  • 1) So try with `ftp_put` (not that it helps on its own, as you have other problems, it's just one step). 2) You have just repeated *"its not working"*, so what does it do??? 3) That's not a path, that's a URL. Use a path to the file. 4) The `$remote_file` argument of `ftp_put` or `ftp_nb_put` has to be a path to file to write to. I.e. `"/incoming/in/newfile.dat"`. The file does not have to exists upfront, obviously. – Martin Prikryl Jun 05 '17 at 08:43
  • If it's "SFTP", why do you use FTP functions, "ftp" tag and say "ftp" in your question? – Martin Prikryl Jun 05 '17 at 13:32
  • @MartinPrikryl Please recommend me the code so that I can upload file – nitesh Sharma Jun 06 '17 at 10:44
  • Did you try to Google *"sftp php upload"*? https://stackoverflow.com/q/9572314/850848 – Martin Prikryl Jun 06 '17 at 10:46
  • @MartinPrikryl not yet let me try. Thank you keep helping me. Let me try this. – nitesh Sharma Jun 06 '17 at 12:56

1 Answers1

-1
$strServer = "##";
 $strServerPort = "22";
 $strServerUsername = "##";
 $strServerPassword = "##";

 //connect to server


 $sftp = new Net_SFTP($strServer);
 if (!$sftp->login($strServerUsername, $strServerPassword)) {
     exit('Login Failed');
 }

 // puts a three-byte file named filename.remote on the SFTP server
 $sftp->put('/incoming/'.$fileName, $fileName);
 // puts an x-byte file named filename.remote on the SFTP server,
 // where x is the size of filename.local
 $sftp->put('/incoming/in/'.$fileName, $fileName, NET_SFTP_LOCAL_FILE);
  • Do not copy other's answer, close your question as a duplicate. And even if you had a good reason to copy it (and you do not), you have to give a credit. – Martin Prikryl Jun 07 '17 at 06:17
  • Yeah I am ready to give credit. I just post this solution here so can help someone else. There is no thought to get any credit. – nitesh Sharma Jun 07 '17 at 07:11
  • On Stack Overflow, we do not copy over answers to duplicate questions. We mark the question as duplicate, to point people to the primary question that already have good answers - So please, accept the duplicate. – Martin Prikryl Jun 07 '17 at 07:19