2011-01-11 60 views

回答

6

檢查了這一點,從http://www.php.net/manual/en/function.file-put-contents.php#101408

從本地主機上傳文件到任何FTP服務器。 皮斯記「ftp_chdir」已經使用的,而不是把直接遠程文件路徑....在ftp_put ... remoth文件應該是唯一的文件名

<?php 
$host = '*****'; 
$usr = '*****'; 
$pwd = '**********';   
$local_file = './orderXML/order200.xml'; 
$ftp_path = 'order200.xml'; 
$conn_id = ftp_connect($host, 21) or die ("Cannot connect to host");  
ftp_pasv($conn_id, true); 
ftp_login($conn_id, $usr, $pwd) or die("Cannot login"); 
// perform file upload 
ftp_chdir($conn_id, '/public_html/abc/'); 
$upload = ftp_put($conn_id, $ftp_path, $local_file, FTP_ASCII); 
if($upload) { $ftpsucc=1; } else { $ftpsucc=0; } 
// check upload status: 
print (!$upload) ? 'Cannot upload' : 'Upload complete'; 
print "\n"; 
// close the FTP stream 
ftp_close($conn_id); 
?> 
0

如果你想使用file_put_contents具體而言,你必須使用stream context作爲遠程服務器接受上傳的協議。例如,如果服務器配置爲允許PUT請求,則可以創建HTTP上下文並將適當的方法和內容發送到服務器。另一種選擇是設置FTP上下文。

comments for file_put_contents中有一個關於如何將它與FTP的流上下文一起使用的示例。請注意,使用的ftp://user:[email protected] URI方案以明文形式傳輸用戶憑證。

Additional examples

1

我寫了類似PHP file_put_contents(),這是寫入FTP服務器的功能:

function ftp_file_put_contents($remote_file, $file_string) 
{ 
    // FTP login 
    $ftp_server="my-ftp-server.com"; 
    $ftp_user_name="my-ftp-username"; 
    $ftp_user_pass="my-ftp-password"; 

    // Create temporary file 
    $local_file=fopen('php://temp', 'r+'); 
    fwrite($local_file, $file_string); 
    rewind($local_file);  

    // Create FTP connection 
    $ftp_conn=ftp_connect($ftp_server); 

    // FTP login 
    @$login_result=ftp_login($ftp_conn, $ftp_user_name, $ftp_user_pass); 

    // FTP upload 
    if($login_result) $upload_result=ftp_fput($ftp_conn, $remote_file, $local_file, FTP_ASCII); 

    // Error handling 
    if(!$login_result or !$upload_result) 
    { 
     echo('FTP error: The file could not be written on the remote server.'); 
    } 

    // Close FTP connection 
    ftp_close($ftp_conn); 

    // Close file handle 
    fclose($local_file); 
} 

// Usage 
ftp_file_put_contents('my-file.txt', 'This string will be written to the remote file.');