[英]Upload all files from a local folder to SFTP using PHP
Can I upload local directory, to remote directory in SFTP? 我可以上传本地目录到SFTP的远程目录吗? Or can I upload all files in a directory to SFTP? 还是可以将目录中的所有文件上传到SFTP?
I tried to upload all file in a directory this way: 我试图通过这种方式将所有文件上传到目录中:
$file = "C:/incoming/360/upload/".date("Ymd")."/*.*";
$dest = "/testing/*.*";
$contents = file_get_contents($file);
file_put_contents("ssh2.sftp://{$sftp}/testing/{$dest}", $contents);
I think that the code above just uploads by filename. 我认为上面的代码只是按文件名上传。
You have iterate the local files and upload them one-by-one: 您已经遍历了本地文件,并一步一步上传它们:
$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);
}
Though note that using file_get_contents
and file_put_contents
is pretty ineffective, when dealing with larger files, as you load whole file contents to a memory. 但是请注意,在处理较大的文件时,将整个文件的内容加载到内存中时,使用file_get_contents
和file_put_contents
效果很差。
Better use fopen
and stream_copy_to_stream
. 最好使用fopen
和stream_copy_to_stream
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.