Here I’m going to show you how to use PHP streams to connect to an FTP server, create a new file and place content into this file. We also need an additional stream context telling PHP to overwrite the file if it exists. A common stumble I see in scripts like this all the time.
/* prepare the FTP connection values */
$user = "user-name";
$pass = "user-pass";
$host = "example.com";
$file = "some-file.txt"; // the name of the file we will create
$hostname = $user . ":" . $pass . "@" . $host . "/" . $file;
/* the content we will place into the file. */
$content = "this is just a test.";
/* create a stream context telling PHP to overwrite the file */
$options = array('ftp' => array('overwrite' => true));
$stream = stream_context_create($options);
/* and finally, put the contents */
file_put_contents($hostname, $content, 0, $stream);
You could also assign to the $content value the entirety of a file on your server using file_get_content() or perhaps a generated CVS or JSON or XML data file.