[英]Upload all files from a local folder to SFTP using PHP
我可以上傳本地目錄到SFTP的遠程目錄嗎? 還是可以將目錄中的所有文件上傳到SFTP?
我試圖通過這種方式將所有文件上傳到目錄中:
$file = "C:/incoming/360/upload/".date("Ymd")."/*.*";
$dest = "/testing/*.*";
$contents = file_get_contents($file);
file_put_contents("ssh2.sftp://{$sftp}/testing/{$dest}", $contents);
我認為上面的代碼只是按文件名上傳。
您已經遍歷了本地文件,並一步一步上傳它們:
$files = glob("C:/incoming/360/upload/".date("Ymd")."/*");
foreach ($files as $file)
{
$contents = file_get_contents($file);
$basename = basename($file);
file_put_contents("ssh2.sftp://{$sftp}/testing/{$dest}", $contents);
}
但是請注意,在處理較大的文件時,將整個文件的內容加載到內存中時,使用file_get_contents
和file_put_contents
效果很差。
最好使用fopen
和stream_copy_to_stream
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.