2 Replies - 236 Views - Last Post: 04 January 2013 - 09:38 AM Rate Topic: -----

#1 Nuko32  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 08-May 12

FTP Transfer not working (Using system() with BASH)

Posted 04 January 2013 - 08:23 AM

I was working on a simple application that zips a folder and upload it in my website. Here's the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Global variables

char zip[60] = "cd ~/Library/\nzip -r STORAGE_DATA.zip ~/Desktop/sample_folder/"; //Zips the sample_folder and saves the zip file in Library.

char ftp_transf[150] = "ftp -n myhost.com <<END_SCRIPT\nquote binary\nquote USER myusername\nquote PASS mypassword\nput ~/Library/STORAGE_DATA.zip STORAGE_DATA.zip"; //FTPs the file STORAGE_DATA.zip in myhost.com (using myusername and mypassword)

int main(int argc, const char * argv[])
{
    system(zip); //Executes first shell script
    system(ftp_transf); //Executes second shell script

    return 0;
}



Hope the code is readable and you have understood it. Basically I'm using Terminal commands to zip a folder and upload it in the web. (Please don't tell me how to use C/C++ libraries for FTP, because I need no external libraries)

Ok, the code works fine and the file is uploaded. The problem is...

When I download the file back from the site and try to open it, the file is corrupted.

Yep, that is the problem... I really cannot understand why! I use binary mode (which is also used by default in FTP), so please... I need some help! Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: FTP Transfer not working (Using system() with BASH)

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: FTP Transfer not working (Using system() with BASH)

Posted 04 January 2013 - 08:55 AM

Well since noting you are doing actually has anything to do with C, I suggest you try your commands at the command line and insure things are working. Run your first command "cd ~/Library/\nzip -r STORAGE_DATA.zip ~/Desktop/sample_folder/" and then check the resulting zip file to insure it is correct. Then run your second command and insure it is functioning properly.


Also in your C file you may be having a problem with the "~" not being properly expanded you may want to try using the full path.

Jim

This post has been edited by jimblumberg: 04 January 2013 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#3 Gorian  Icon User is offline

  • ninja DIC
  • member icon

Reputation: 120
  • View blog
  • Posts: 1,681
  • Joined: 28-June 08

Re: FTP Transfer not working (Using system() with BASH)

Posted 04 January 2013 - 09:38 AM

hmm... I second jimblumberg. Have you tried this in a command line to make sure it works?

first off, I'd ask if you need to add quote before the USER and PASS commands. I never have, but could be wrong. Second, I'd end that command string with \nEND_SCRIPT. Since you are constructing a hereto doc for the FTP transfer, best practice (and at least, to make it work in the command line), you need to end it with the HERETO string, in this case END_SCRIPT

For more information on HERETO docs, I have a tutorial, it's for telnet with HERETO docs, but it's the same principal.

Maybe try running the scripts from your program like this for debugging :

    system(bash -x zip); //Executes first shell script
    system(bash -x ftp_transf); //Executes second shell script


This post has been edited by Gorian: 05 January 2013 - 05:16 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1