PHP Snippets



<?php

$fileContent = 'a';

ini_set('display_errors', TRUE);
error_reporting(E_ALL);

$ftp_server = "listbr.realedainfo.com";
$ftp_user_name = "username";
$ftp_user_pass = "password";
$remote_file_path = "./";
$remote_file_name = "LISTBR.in";

//When you have your file contents as a string, create temporary stream and use that as a file handle.
$tempHandle = fopen('php://temp', 'r+');
fwrite($tempHandle, $fileContent);
rewind($tempHandle);

$ourFileHandle = $tempHandle;

$conn_id=ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
    echo "FTP connection failed to connect to server " . $ftp_server . " for user " . $ftp_user_name;
    exit;
} else {
    echo "<br>Connected to $ftp_server, for user $ftp_user_name".".....";

    ftp_pasv($conn_id, true);
   
    $upload =  ftp_fput($conn_id, $remote_file_path . $remote_file_name, $ourFileHandle, FTP_ASCII);
    if (!$upload) {
        echo "FTP upload has failed!";
    } else {
        echo "Uploaded  to $ftp_server ";
    }
    ftp_close($conn_id);
}
fclose($ourFileHandle);






//
//function fwrite_stream($fp, $string) {
//    for ($written = 0; $written < strlen($string); $written += $fwrite) {
//        $fwrite = fwrite($fp, substr($string, $written));
//        if ($fwrite === false) {
//            return $written;
//        }
//    }
//    return $written;
//}



























Comments