Search…
⌃K

Code examples

PHP (ext-ftp)

This example needs the PHP FTP extension to be installed. It creates a CSV file in memory based on an array of stock data.
<?php
// Define shop upload key and inventory
$upload_key = '<<your upload key>>';
$inventory = [
[
'barcode' => '2001234567',
'price' => '1.99',
'quantity' => 24,
'currency' => 'USD'
],
// ...
];
// Connect to FTP server
$connection = ftp_connect('ftp.near.live');
ftp_login($connection, 'apikey', $upload_key);
ftp_pasv($connection, true);
// Turn array into CSV
$handle = fopen('php://temp', 'r+');
ftruncate($handle, 0);
fputcsv($handle, array_keys($inventory[0]));
foreach ($inventory as $item) {
fputcsv($handle, array_values($item));
}
rewind($handle);
// Upload via FTP as stock.csv
ftp_fput($connection, 'stock.csv', $handle, FTP_BINARY, 0);
// Close file handle and connection
fclose($handle);
ftp_close($connection);

Javascript (Node.js)

This example uses the ftp-deploy package to upload the stock file to the FTP server. It expects a file named stock.csv to exist in the script's directory.
const FtpDeploy = require('ftp-deploy');
const ftpDeploy = new FtpDeploy();
const uploadKey = '<<your upload key>>';
const stockFileName = 'stock.csv';
const config = {
user: 'apikey',
password: uploadKey,
host: 'ftp.near.live',
port: 21,
localRoot: __dirname,
remoteRoot: '/',
include: [stockFileName],
deleteRemote: false,
forcePasv: true
};
ftpDeploy.deploy(config, function (err, files) {
if (err) {
console.log(err);
process.exit(1);
return;
}
console.log('Uploaded files', files);
});

PowerShell

A great low-tech way to get file uploads working in a PowerShell script that you execute on a timer.
# Update these variables
$csvPath = "C:\local\path\stock.csv" # Point this to your CSV file
$uploadKey = "xxxxxxxxx" # Set your Upload key / FTP password here
# Everything below here can stay the same
$request = [Net.WebRequest]::Create("ftp://ftp.near.live/stock.csv")
$request.Credentials = New-Object System.Net.NetworkCredential("apikey", $uploadKey)
$request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$fileStream = [System.IO.File]::OpenRead($csvPath)
$ftpStream = $request.GetRequestStream()
$fileStream.CopyTo($ftpStream)
$ftpStream.Dispose()
$fileStream.Dispose()
If you wish to verify your upload, you can get details like file size and upload date like this:
# This is optional - get file size and upload date to verify upload
$request = [Net.WebRequest]::Create("ftp://ftp.near.live/stock.csv")
$request.Credentials = New-Object System.Net.NetworkCredential("apikey", $uploadKey)
$request.Method = [System.Net.WebRequestMethods+FTP]::GetDateTimeStamp
$response = $request.GetResponse()
$timestamp = $response.StatusDescription.Trim()
$request = [Net.WebRequest]::Create("ftp://ftp.near.live/")
$request.Credentials = New-Object System.Net.NetworkCredential("apikey", $uploadKey)
$request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectoryDetails
$response = $request.GetResponse().GetResponseStream()
$StreamReader = New-Object System.IO.StreamReader $response
$filesize = 0
While ($file = $StreamReader.ReadLine()) {
If($file -like "*stock.csv*") {
$filesize = (($file -split "\D+") | Where-Object {$_})[3].Trim()
}
}
Write-Host "Upload date: $timestamp"
Write-Host "File size: $filesize"