I want to copy a file from a remote server to a local server via php. I’m not sure of the “best” approach to do this so I’ve tried a few different ways to accomplish this but I’m hitting roadblocks with those methods.
So I came up with a method using JSONP to (A) call the remote server and get the contents of the .php file and pass it back to the local server for the local script to then (B) call to a local php script and use file_put_contents to write the content to the local server.
All works fine UNTIL an ampersand is reached. The file write stops there.
Edit to show data coming back.
if ($mysqlErrorConnectServer || $mysqlErrorConnectDb || $mysqlErrorConfigTableMissing) {
$errorMsg .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
. '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
. '<head>'
. '<title>Example website © - Example description of example site</title>'
. '<style type="text/css">'
So if you look at the code coming back… there is a “©” which needs to be there to display the copyright symbol on the page… but it stops the file_put_contents from writing the rest of the file.
Copied from previous response to answer my own question.
Here’s the answer to the problem. 🙂 Knew it would be something simple I had not run across yet. Before sending the data to the local write script (after receiving it back from the remote), I simply call encodeURIComponent() to my returned response and send it on to the local script to write.
Works like a charm.