Skip to content

Instantly share code, notes, and snippets.

@trey8611
Forked from m-thomson/import-ftp-proxy.php
Last active February 18, 2022 16:56
Show Gist options
  • Select an option

  • Save trey8611/b7df39e7c5b09776c0ffc17dd4e2ead1 to your computer and use it in GitHub Desktop.

Select an option

Save trey8611/b7df39e7c5b09776c0ffc17dd4e2ead1 to your computer and use it in GitHub Desktop.
This proxy 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:[email protected]/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:[email protected]/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