Skip to content

Instantly share code, notes, and snippets.

@boywondercreative
Forked from m-thomson/import-ftp-proxy.php
Created February 2, 2019 04:53
Show Gist options
  • Select an option

  • Save boywondercreative/0d4bdc8779d9eabb6a8c32f7a45b3f46 to your computer and use it in GitHub Desktop.

Select an option

Save boywondercreative/0d4bdc8779d9eabb6a8c32f7a45b3f46 to your computer and use it in GitHub Desktop.
This script reads the specified file over FTP and outputs it over HTTP. Thus, you can point WP ALL Import at the URL for this script on your server to provide a "bridge" between FTP and HTTP. This is provided with the hope it will be useful but custom PHP and importing over FTP is not officially supported.

Since WP All Import only supports fetching data files over HTTP or HTTPS, many users employ their own proxy script in PHP to act as an intermediary. This script would read the data file from the FTP server and output it over HTTP (for consumption by WP All Import).

For example, you could have a PHP script on your server called ftp-proxy.php and then tell WP All Import to import from that URL:

http://example.com/ftp-proxy.php

Security note: Anyone could access your data if they guess this URL.We recommend either removing this file from the server after you've run your import or renaming this file to something "unguessable" (e.g. 'ftp-proxy-24dxfi3.php') but understand that method is not perfectly secure. For best security use an .htaccess rule allowing access only from localhost.

<?php
// Enter the FTP (or HTTP) URL of your data file here.
$url = "ftp://username:password@hostname.com/path/to/file.csv";
// These headers aren't strictly needed but can be helpful
header('Content-Type: text/plain');
header('Content-Disposition: attachment');
header('Pragma: no-cache');
// Fetch the file and echo it
readfile($url);
<?php
// These headers aren't strictly needed but can be helpful
header('Content-Type: text/plain');
header('Content-Disposition: attachment');
header('Pragma: no-cache');
// Enter the FTP (or HTTP) URL of your data file here.
$url = "ftp://username:password@hostname.com/path/to/file.csv";
// Open remote file
if (($handle = fopen($url, "r")) !== false) {
// Read each line
while (($line = fgets($handle, 4096)) !== false) {
// Output each line from the file as it is read. If needed, you could even filter the output here.
// e.g: echo str_replace("foo", "bar", $line);
echo $line;
}
fclose($handle);
}
else {
// You might want to code some better error handling here.
echo "Error: failed to open remote file\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment