php结合ssh2.sftp和zip文件 Package 器

pinkon5k  于 2022-12-02  发布在  PHP
关注(0)|答案(1)|浏览(127)

有没有办法将ssh2.sftp://和zip://文件 Package 器结合起来?
以下是我尝试过的方法:

$handle = fopen("zip://ssh2.sftp://{$session}/myfile.zip#myfile.csv",'r');

$handle1 = fopen("ssh2.sftp://{$ssh_session}/myfile.zip",'r');
$handle2 = fopen("zip://{$handle1}",'r');

(没想到第二个还能用)

ppcbkaq5

ppcbkaq51#

It is not possible to combine the ssh2.sftp:// and zip:// file wrappers in PHP because the zip:// wrapper only supports local files, not remote files accessed through protocols like SFTP.
In order to extract the contents of a ZIP file located on a remote server using SFTP, you will need to first download the file to your local machine, then use the zip:// wrapper on the local copy of the file.
Here is an example of how this could be done:

// Connect to the remote server using SFTP
$sftp = ssh2_sftp($ssh_session);

// Download the ZIP file to a local temporary directory
$temp_dir = sys_get_temp_dir();
$temp_file = tempnam($temp_dir, 'zip');
ssh2_scp_recv($ssh_session, "/path/to/myfile.zip", $temp_file);

// Open the local copy of the ZIP file using the zip:// wrapper
$handle = fopen("zip://{$temp_file}#myfile.csv", 'r');

// Read the contents of the CSV file from the ZIP file
// (assuming the CSV file is named "myfile.csv" and is located at the root of the ZIP file)
$contents = fread($handle, filesize($temp_file));

// Close the handle to the ZIP file
fclose($handle);

// Delete the local temporary copy of the ZIP file
unlink($temp_file);

Note that this example uses the ssh2_scp_recv() function to download the ZIP file from the remote server, but you could also use the ssh2_sftp_read() function if you prefer.

相关问题