Auto Fetch File from FTP Server

d and h auto fetch
It’s been 3 months since I started studying PHP, now it’s time to put what I learned into application. Just recently, I got drafted into the Junior Developer position in a company I am currently contracted to work on. As a Junior Developer, I am but a novice to the team, and thus, yet to learn from the Seniors. This is the time to gather experience and learn more about PHP programming, and by being put in a actual situation where we meet daily IT-related problems, our skills are being put to actual test. This is a scenario where we are being tested at how good we are at providing solutions by utilizing what we know in programming. This is the perfect environment for growth.

So for my first assignment, I was tasked to automate all of the inventories. A couple months earlier, with the help of a fellow developer and well-experience Data Analyst and Manager, we managed to automate majority of the inventories that goes via email by creating scripts in Outlook. These scripts automatically get the email attachments and move them into our inventory system folders. But, there were still others that require manual action, like inventories that has to be retrieved from vendor websites. It requires logging into the site, and downloading it from there. Unfortunately, our system couldn’t automatically do that. We also have vendors that have FTPs, and while our system can fetch from these FTPs, sometimes their inventories are stored in a different file format, and our system is unable to read them. Thus, they have to be manually converted to csv first before they can be fed into the local system. Now, it’s up to me to find a way to fetch them automatically. If successful, our team would be able to save time and focus on other tasks. This will also ensure that we are always up to date with our inventory.

Here I am going to document how I coded the Auto Fetch system, and how I manage to extend the functionality of our current inventory system. The PHP file I am presenting here allows auto fetching of an inventory file with no file extension from the vendor’s FTP site with a simple click of a button. After retrieving the file, it will convert it into csv and copy it into local. The PHP file can also be run by a cron by opening the file and adding ?cron=execute in the url. You may have to change the default credentials to match the FTP Server you would like to auto fetch.

While I’m not going to show the exact method as to how I did it in our system, this PHP file will at least replicate the function.

The Code

FTP
To test out this code, you guys may have to create a dummy FTP site and store a csv file without an extension name in there. Then, change the default contents of the FTP credential variables in the PHP file. To create a FTP site, Click here on how to do it.

BTW, I bundled all of the components that make up the system in a zip file. You can download them here. The file contents are as follows…

style.css – The CSS Stylesheet for the webpage.

#darkangel {
	background-color: #000000;
	color: #ffffff;
}

h1 {
	font-size: 24px;
	font-weight: bold;
	font-family: sans-serif;
	color: #ffffff;
}

h2 {
	font-size: 18px;
	font-weight: bold;
	font-family: sans-serif;
	color: #ffffff;
}

ul, li, a:link, a:visited, a:active {
	color: #ffffff;
}

a:hover {
	color: #00ff00;
}

.header {
	font-size: 24px;
	font-weight: bold;
	font-family: sans-serif;
	color: #ffffff;
	display: inline;
}

autofetch.php – The PHP script responsible for the auto fetching function. I also included comments to explain each called function.

<html>
<head>
	<title>D and H Inventory Control Panel</title>
	<link rel="stylesheet" type="text/css" href="style.css" />
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body id="darkangel">
<h1>D and H Inventory (Auto Fetch System)</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
  FTP Host: <input type="text" name="linkftp" size="45" /><br><br>
  FTP Username: <input type="text" name="ftpuser" /> FTP Password: <input type="text" name="ftppass" /><br><br>
  FTP Folder: <input type="text" name="ftpfolder" /> File: <input type="text" name="filename" /><br><br>
  *Leave all fields blank for default. Default will enter predefined credentials. Use this only if there are changes to FTP info.<br><br>
  <input type="submit" name="submit" value="Run Auto Fetch Inventory" />
</form><br><br>

<?php
  // setting error_reporting to 0 will disable all PHP warnings and error reports and prevent it from showing up from the page.
  error_reporting(0);

  // If webpage url is autofetch.php?cron=execute. Run this script.
  if (@$_GET['cron'] == 'execute') {
    echo 'Script has been executed automatically via GET method!<br>';
  // call this function. Refers to function DandHAutoFetch()
    DandHAutoFetch();
  }

  // Call this function when button submit is clicked
  if(isset($_POST['submit'])){
    DandHAutoFetch();
  }

  function DandHAutoFetch() {
   // if variable $linkftp is empty, make it ftp.astralmeta.com else get the url from the form 'linkftp'.
    if(empty($_POST['linkftp'])) {
      $FTPServer = 'ftp.astralmeta.com';
    } else {
      $FTPServer = $_POST["linkftp"];
    }

    if(empty($_POST['ftpuser'])) {
      $FTPUsername = 'dummy@astralmeta.com';
    } else {
      $FTPUsername = $_POST['ftpuser'];
    }

    if(empty($_POST['ftppass'])) {
      $FTPPassword = 'password';
    } else {
      $FTPPassword = $_POST['ftppass'];
    }

  // The root folder.
    if(empty($_POST['ftpfolder'])) {
      $FTPFolder = '/';
    } else {
      $FTPFolder = '/' . $_POST['ftpfolder'];
    }

  // The file to retrieve. ITEMLIST only because this file doesn't have an extension name. Otherwise, you have to specify the extension name.
    if(empty($_POST['filename'])) {
      $Filename = 'ITEMLIST';
    } else {
      $Filename = $_POST['filename'];
    }

  // $Local File is the directory where we are going to create a file called dandhinv.csv
    $LocalFile = 'local/dandhinv.csv';

  // This sets PHP's clock to US/New York time.
    date_default_timezone_set("America/New_York");

  // variable to get date format. m is for month, d for day, and Y for year. Date will look something like 10/2/2018. The \- is a delimiter inserted into the date format to split the date and time. g:i a will make use of the 12-hour time format. For example: 1:51 am.
    $date = date('m/d/Y \- g:i a') . ': ';

  // This is the function that allows you to connect to the FTP. It says there that if connection to the FTP is successful, enter credentials else die or terminate process and show the message inside die's parenthesis. ftp_connect is the function that allows you to connect to an FTP server.
    $FTPConn = ftp_connect($FTPServer) or die('<font color="#ff0000">Connection Error:</font> Unable to connect to ' . $FTPServer);

  // if behind a firewall, switch connection to passive mode. [EDIT: 3/1/2019]
    ftp_pasv($FTPConn, true);

  // ftp_login enters your username and password into an FTP server.
    $Login =  ftp_login($FTPConn, $FTPUsername, $FTPPassword) or die('<font color="#ff0000">Connection Error:</font> Invalid Username and Password');

  // ftp_get is the function use to grab a file from FTP Server. FTP_BINARY is the file type. The grabbed file will be converted into this format. Technically, this format refers to any file. 
    if(ftp_get($FTPConn, $LocalFile, $Filename, FTP_BINARY)) {
      echo 'File has been automatically fetched and uploaded into local.';
    } else {
            // file_exists checks if the file is present in local.
			if(!file_exists($LocalFile)) {
				echo '<font color="#ff0000">Error Creating File:</font> Cannot find Source Directory or File!';
			} else {
      echo 'There was a problem fetching the file. Please check your FTP Credentials!';
           // This means create a file error.log in local folder and enter the date and the message into that file. PHP_EOL tells PHP to create a new line while FILE_APPEND tells it to add new texts into it instead of overwriting an existing one.
			file_put_contents('local/error.log', $date . 'There was a problem fetching the File! - Generated by Anthony Y.\'s Script' . PHP_EOL, FILE_APPEND );
			}
    }
   // This closes the FTP connection.
    ftp_close($FTPConn);
  }

?>

</body>
</html>

If there are some errors in the process, the PHP file will abort the process and create error reports, like for example, when you enter a wrong username and password, it will show a message “Connection Error: Invalid Username and Password” or if it fails to get the file from the FTP server, it will display the message “There was a problem fetching the file. Please check your FTP Credentials!”. It will also generate an error.log file in the local folder that contains the log of the issues the system undergone.

Error Autofetch

Download

I have bundled all the components, including the PHP file, the CSS, and the folder where it will store the FTP downloaded file, in a zip file. In order to make this work, you need XAMPP or WAMP to provide you with the local Apache environment or upload them in your host. All 3 must be in the same folder. You may also need to change the variables – $FTPServer, $FTPUsername, $FTPPassword, $FTPFolder, and $Filename to match the FTP server and credentials needed to initiate File Transfer Protocol.

You can actually use this PHP file to grab a file from any FTP Servers as long as your FTP Server and credentials are valid.

Download AutoFetch PHP File

Follow me at:

One Reply to “Auto Fetch File from FTP Server”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.