# FTP server recieve files without content

**URL:** <https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919>\
**Category:** Networking, Protocols, and Devices\
**Created:** [February 20, 2014, 5:40pm UTC](https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919 "2014-02-20T17:40:10Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 20, 2014, 5:40pm UTC](https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919/1 "2014-02-20T17:40:10Z")

</div>

Hi everyone  
I am trying to send a text , picture and video separately by using an Arduino Mega 2560. the server is my PC with a Filezilla server. using the following code which I found in the Arduino site I am trying to download or upload files.  
But in the first step when I tried to send a text file with 1 kB length, I just could to see an empty text file in server.  
Would you explain me how can I solve this problem?  
sincerely

```cpp
#include <SD.h>
#include <SPI.h>
#include <WiFi.h>
// comment out next line to write to SD from FTP server
#define FTPWRITE

// this must be unique
byte mac[] = {0x78, 0xC4, 0xE, 0x01, 0x9C, 0xE3};

// change to your network settings
IPAddress ip ###########
IPAddress  gateway#########
IPAddress subnet############
char ssid[]= "########";
char pass[]= "########";
// change to your server
IPAddress server(########);

WiFiClient client;
WiFiClient dclient;

char outBuf[128];
char outCount;

// change fileName to your file (8.3 format!)
char fileName[13] = "TEXT3.TXT";
void setup()
{
  Serial.begin(9600);

  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  if(SD.begin(4) == 0)
  {
    Serial.println(F("SD init fail"));          
  }

  WiFi.begin(ssid,pass); 
  digitalWrite(10,HIGH);
  delay(2000);
  Serial.println(F("Ready. Press f or r"));
}
void loop()
{
  byte inChar;

  inChar = Serial.read();

  if(inChar == 'f')
  {
    if(doFTP()) Serial.println(F("FTP OK"));
    else Serial.println(F("FTP FAIL"));
  }
  if(inChar == 'r')
  {
    readSD();    
  }
}

File fh;

byte doFTP()
{
#ifdef FTPWRITE
 fh = SD.open(fileName,FILE_READ);
  Serial.println(fh);
  //fh = SD.open("Text3.txt",FILE_READ);
#else
  SD.remove(fileName);
  fh = SD.open(fileName,FILE_WRITE);
#endif
  if(!fh)
  {
    Serial.println(F("SD open fail"));
    return 0;    
  }
#ifndef FTPWRITE  
  if(!fh.seek(0))
  {
    Serial.println(F("Rewind fail"));
    fh.close();
    return 0;    
  }
#endif

  Serial.println(F("SD opened"));

  if (client.connect(server,21)) {
    Serial.println(F("Command connected"));
  } 
  else {
    fh.close();
    Serial.println(F("Command connection failed"));
    return 0;
  }

  if(!eRcv()) return 0;

  client.println(F("USER #####"));

  if(!eRcv()) return 0;

  client.println(F("PASS #######"));

  if(!eRcv()) return 0;

  client.println(F("SYST"));

  if(!eRcv()) return 0;

  client.println(F("PASV"));

  if(!eRcv()) return 0;

  char *tStr = strtok(outBuf,"(,");
  int array_pasv[6];
  for ( int i = 0; i < 6; i++) {
    tStr = strtok(NULL,"(,");
    array_pasv[i] = atoi(tStr);
    if(tStr == NULL)
    {
      Serial.println(F("Bad PASV Answer"));    
    }
  }

  unsigned int hiPort,loPort;
  hiPort = array_pasv[4] << 8;
  loPort = array_pasv[5] & 255;

  Serial.print(F("Data port: "));
  hiPort = hiPort | loPort;
  Serial.println(hiPort);
  
  if (dclient.connect(server,hiPort)) {
    Serial.println(F("Data connected"));
  } 
  else {
    Serial.println(F("Data connection failed"));
    client.stop();
    fh.close();
    return 0;
  }
#ifdef FTPWRITE 
  client.print(F("STOR "));
  client.println(fileName);
#else
  client.print(F("RETR "));
  client.println(fileName);
#endif

  if(!eRcv())
  {
    dclient.stop();
    return 0;
  }

#ifdef FTPWRITE
  Serial.println(F("Writing"));
// _______________________________________________________________________________________________________________________________________________________________________

  byte clientBuf[64];
  int clientCount = 0;

  while(fh.available())
  {
    clientBuf[clientCount] = fh.read();
    clientCount++;

    if(clientCount > 63)
    {
      dclient.write(clientBuf,64);
      clientCount = 0;
    }
  }

  if(clientCount > 0) dclient.write(clientBuf,clientCount);

#else
  while(dclient.connected())
  {
    while(dclient.available())
    {
      char c = dclient.read();
      fh.write(c);      
      Serial.write(c); 
    }
  }
#endif

  dclient.stop();
  Serial.println(F("Data disconnected"));

  if(!eRcv()) return 0;

  client.println(F("QUIT"));

  if(!eRcv()) return 0;

  client.stop();
  Serial.println(F("Command disconnected"));

  fh.close();
  Serial.println(F("SD closed"));
  return 1;
}
byte eRcv()
{
  byte respCode;
  byte thisByte;

  while(!client.available()) delay(1);

  respCode = client.peek();

  outCount = 0;

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);

    if(outCount < 127)
    {
      outBuf[outCount] = thisByte;
      outCount++;      
      outBuf[outCount] = 0;
    }
  }

  if(respCode >= '4')
  {
    efail();
    return 0;  
  }

  return 1;
}

void efail()
{
  byte thisByte = 0;

  client.println(F("QUIT"));

  while(!client.available()) delay(1);

  while(client.available())
  {  
    thisByte = client.read();    
    Serial.write(thisByte);
  }

  client.stop();
  Serial.println(F("Command disconnected"));
  fh.close();
  Serial.println(F("SD closed"));
}

void readSD()
{
  fh = SD.open(fileName,FILE_READ);

  if(!fh)
  {
    Serial.println(F("SD open fail"));
    return;    
  }

  while(fh.available())
  {
    Serial.write(fh.read());
  }

  fh.close();
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 20, 2014, 7:05pm UTC](https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919/2 "2014-02-20T19:05:04Z")

</div>

I was fighting with this yesterday, if I turned the Firewall off on my WinXP PC (I am using the WinXP FTP Server), this code worked for me.

Good Luck...

---

<div class="post-metadata">

**Author:** ![SurferTim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/surfertim/32/1072341_2.png) [@SurferTim](https://forum.arduino.cc/u/SurferTim)\
**Post date:** [February 20, 2014, 8:13pm UTC](https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919/3 "2014-02-20T20:13:21Z")

</div>

Insure the user you are logging in with has write permissions in that directory. I use Linux with that code and it works fine. It should. I wrote that sketch. 🙂

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 21, 2014, 11:09am UTC](https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919/4 "2014-02-21T11:09:18Z")

</div>

I have Win 8.1 64x on my system.  
First of all I tried to turn firewall off But nothing happened then I checked fillzilla permission I got the same result .  
Now I am trying to debug the code by putting some Serial.print .  
I have problem in the following part

```cpp
  byte clientBuf[64];
  int clientCount = 0;

  while(fh.available())
  {
    clientBuf[clientCount] = fh.read();
    clientCount++;
    Serial.println(clientCount);

    if(clientCount > 63)
    {
      dclient.write(clientBuf,64);
      clientCount = 0;
    }

```

Expecting result is 64 counting but I just have 42 counting .  
when I change 64 to 32 I have the same counting.

---

<div class="post-metadata">

**Author:** ![SurferTim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/surfertim/32/1072341_2.png) [@SurferTim](https://forum.arduino.cc/u/SurferTim)\
**Post date:** [February 21, 2014, 11:13am UTC](https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919/5 "2014-02-21T11:13:39Z")

</div>

Insure you have the last line here. It sends any amount smaller than 64.

```cpp
   while(fh.available())
  {
    clientBuf[clientCount] = fh.read();
    clientCount++;

    if(clientCount > 63)
    {
      dclient.write(clientBuf,64);
      clientCount = 0;
    }
  }

// this sends any bytes remaining in the clientBuf
  if(clientCount > 0) dclient.write(clientBuf,clientCount);

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 21, 2014, 11:57am UTC](https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919/6 "2014-02-21T11:57:37Z")

</div>

Yes I have it.  
This is the result

> Ready. Press f or r  
> 1  
> SD opened  
> Command connected  
> 220-FileZilla Server version 0.9.43 beta  
> 220-written by Tim Kosse ([tim.kosse@filezilla-project.org](mailto:tim.kosse@filezilla-project.org))  
> 220 Please visit [FileZilla® download | SourceForge.net](http://sourceforge.net/projects/filezilla/)  
> 331 Password required for ##  
> 230 Logged on  
> 215 UNIX emulated by FileZilla  
> 227 Entering Passive Mode (#,#,#,#,#,#,#)  
> Data port: 53413  
> Data connected  
> 150 Opening data channel for file upload to server of "/TEXT3.TXT"  
> Writing  
> 1  
> 2  
> 3  
> 4  
> 5  
> 6  
> 7  
> 8  
> 9  
> 10  
> 11  
> 12  
> 13  
> 14  
> 15  
> 16  
> 17  
> 18  
> 19  
> 20  
> 21  
> 22  
> 23  
> 24  
> 25  
> 26  
> 27  
> 28  
> 29  
> 30  
> 31  
> 32  
> 33  
> 34  
> 35  
> 36  
> 37  
> 38  
> 39  
> 40  
> 41  
> Data disconnected

---

<div class="post-metadata">

**Author:** ![SurferTim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/surfertim/32/1072341_2.png) [@SurferTim](https://forum.arduino.cc/u/SurferTim)\
**Post date:** [February 21, 2014, 12:03pm UTC](https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919/7 "2014-02-21T12:03:06Z")

</div>

It appears it is working ok. If the file is still empty on the server, insure the file permissions are set correctly.

If you tried with Filezilla and got the same empty file, then it is probably a permission challenge on the server.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 21, 2014, 12:33pm UTC](https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919/8 "2014-02-21T12:33:16Z")

</div>

I changed the sharing part in properties of server folder.but again it is empty

 ![Untitled.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/b/2/8/b28f9ec1489c01b36080bea154450f9f07d4dfb6.jpeg)

---

<div class="post-metadata">

**Author:** ![SurferTim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/surfertim/32/1072341_2.png) [@SurferTim](https://forum.arduino.cc/u/SurferTim)\
**Post date:** [February 21, 2014, 12:40pm UTC](https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919/9 "2014-02-21T12:40:04Z")

</div>

Are you using the Windows FTP server? Maybe is a setting in it. Read this and insure you have your FTP server settings changed to allow writes.  
[http://support.microsoft.com/kb/323384](http://support.microsoft.com/kb/323384)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 21, 2014, 4:44pm UTC](https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919/10 "2014-02-21T16:44:36Z")

</div>

I tried to create an FTP server windows but in last step could't to be connect to the server so then Because of made doubt on access point and server,first I checked access point configuration but it was OK,then tried to send a file from another PC as client to my PC as server finally could to receive file,by the way now I think my server has not problem !!.  
I formatted the SD card,the counted number increased from 42 to 64 and so on..  
But again I have problem in receiving file and it is empty.  
I am trying with WiFi but your original code is based on Ethernet,This would not be a problem?

---

<div class="post-metadata">

**Author:** ![SurferTim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/surfertim/32/1072341_2.png) [@SurferTim](https://forum.arduino.cc/u/SurferTim)\
**Post date:** [February 21, 2014, 5:09pm UTC](https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919/11 "2014-02-21T17:09:02Z")

</div>

I see. You are using a wifi shield. I have one here. Give me a chance to play with it. It may be a couple hours before I finish the project I have going. Then I will experiment.

---

<div class="post-metadata">

**Author:** ![SurferTim](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/surfertim/32/1072341_2.png) [@SurferTim](https://forum.arduino.cc/u/SurferTim)\
**Post date:** [February 22, 2014, 11:54am UTC](https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919/12 "2014-02-22T11:54:00Z")

</div>

Sorry. The wifi shield doesn't work with the FTP client. ☹

It does ok until the data connection is closed, but after that the wifi shield command connection (socket) never receives the status of the send, and I'm not sure the FTP server gets the "QUIT" message. That is what causes the server to write the data and close the file.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 22, 2014, 2:39pm UTC](https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919/13 "2014-02-22T14:39:30Z")

</div>

No problem.I will try to solve my problem in the other way.  
Thanks for your consideration and time.  
Regards

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 10:47am UTC](https://forum.arduino.cc/t/ftp-server-recieve-files-without-content/213919/14 "2021-05-06T10:47:04Z")

</div>

Closed on May 6, 2021, 10:47am UTC


