设置imagepng路径以通过FTP保存

时间:2012-08-30 03:01:27

标签: php image ftp

如何通过FTP保存图像?这是我的代码:

$conn_id = ftp_connect('***');
$login_result = ftp_login($conn_id, '***','***'); 
if( $login_result) echo 'connected';
$save = "FTP://temp/". $FileName;

imagepng($this->Picture, $save);

/*if (ftp_put($conn_id,$save,$save, FTP_ASCII)) 
  echo "successfully uploaded \n";
else 
  echo "There was a problem while uploading \n";
*/

2 个答案:

答案 0 :(得分:3)

你可以这样做:

ob_start();
imagepng($this->Picture);
$image = ob_get_clean();
$stream = stream_context_create(array('ftp' => array('overwrite' => true))); 
file_put_contents("ftp://user:pass@host/folder/".$FileName, $image, 0, $stream); 

答案 1 :(得分:0)

希望这有助于你

<?

//Configuration
$ftp_server = "ftpaddress";
$ftp_user = "username";
$ftp_pass = "password";

// set up a connection or die
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server"); 

//trying to connect with login details
if (@ftp_login($conn_id, $ftp_user, $ftp_pass)) {
    echo "Connected! ";
} else {
    echo "Couldn't connect!";
}

//You can change it with the file name, this is for if you're upload using form.

   $myName = $_POST['name']; //This will copy the text into a variable
   $myFile = $_FILES['file_name']; // This will make an array out of the file information that was stored.
?>

<?PHP
        $destination_path = "dest/path/"; 

//your desired path for uploading)

    $destination_file = $destination_path."img.jpg";

//This will create a full path with the file on the end for you to  use, I like splitting the variables like this in case I need to use on on their own or if I'm dynamically creating new folders.

        $file = $myFile['tmp_name'];

//Converts the array into a new string containing the path name on the server where your file is.

    $upload = ftp_put($conn_id, $destination_file, $file, FTP_BINARY);// upload the file
    if (!$upload) {// check upload status
        echo "FTP upload of $destination_file has failed!";
    } else {
        echo "Uploaded $file to $conn_id as $destination_file";
    }

?>