Can you unzip a .zip on a remote server? - php

I have a script unzips files on the server but is it possible to unzip files on an external or remote server.
For instance can I go
<?php
$zip = new ZipArchive;
$zip->open('ftp://user:password#host/path/file.zip');
$zip->extractTo('ftp://user:password#host/');
$zip->close();
echo "Ok!"; ?>
Thanks a lot

to unzip a remote file on a server with PHP a rather simple solution that worked for me is:
ftp the zip file, say a.zip, to the remote folder where you want it extracted
create a php file unzip.php and ftp it to the same folder as the zip file above
insert the following code into unzip.php:
<?php
$zip = new ZipArchive;
$zip->open('a.zip');
$zip->extractTo('x/');
$zip->close();
echo "Ok!";
?>
set chmod of unzip.php to executable
execute the php file from any browser - you shd get an 'Ok' confirming the extraction

You could try the following via an ssh connection to that server:
<?php
$connection = ssh2_connect('example.com', 22);
ssh2_auth_password($connection, 'username', 'password');
$stream = ssh2_exec($connection, 'unzip /path/to/file.zip');
?>
More info here:
http://www.php.net/manual/en/function.ssh2-exec.php

Related

How do I unzip a 5GB zip file on my web server without PHP timeout?

How do I unzip a 5GB zip file on my web server without PHP timeout?
I need to unzip a 5GB zip file on server.
When I run the unzip it times out because the file extraction takes too long.
I do not have access to shell console, all I have is a FTP and I can execute PHP by accessing the PHP file in my web browser.
How do I unzip the large file?
$dirName = $_POST["dirName"];
echo "$dirName<br>\n";
$dirName = htmlspecialchars($dirName);
echo "$dirName<br>\n";
flush();
$zipFileName = getcwd() . "/$dirName/aTest.zip";
$destDirName = getcwd() . "/$dirName/aTest";
echo "Zip file is $zipFileName<br>\n";
echo "Directory is $destDirName<br>\n";
flush();
// Unzip selected zip file
$zip = new ZipArchive;
$res = $zip->open($zipFileName);
if ($res === TRUE) {
// Extract file
$zip->extractTo($destDirName);
$zip->close();
}
echo "Successfull<br>\n";
flush();
I found an answer here but the link ("try PclZip") does not work.
following is the restrictions as per PHP Info
disable_functions
exec,system,passthru,shell_exec,escapeshellarg,escapeshellcmd,proc_close,
proc_open,ini_alter,dl,popen,popen,pcntl_exec,socket_accept,socket_bind,
socket_clear_error,socket_close,socket_connect,socket_create_listen,
socket_create_pair,socket_create,socket_get_option,socket_getpeername,
socket_getsockname,socket_last_error,socket_listen,socket_read,
socket_recv,socket_recvfrom,socket_select,socket_send,socket_sendto,
socket_set_block,socket_set_nonblock,socket_set_option,socket_shutdown,
socket_strerror,socket_write,stream_socket_server,pfsockopen,
disk_total_space,disk_free_space,chown,diskfreespace,getrusage,
get_current_user,set_time_limit,dl,leak,listen,chgrp,link,symlink,
dlopen,proc_nice,proc_get_stats,proc_terminate,shell_exec,sh2_exec,
posix_getpwuid,posix_getgrgid,posix_kill,ini_restore,mkfifo,dbmopen,
dbase_open,filepro,filepro_rowcount,posix_mkfifo,_xyec,mainwork,
get_num_redirects,putenv,geoip_open,sleep,imap_open
You can try to change max-execution-time value with
<?php
ini_set('max-execution-time', 600); // 600 seconds
If it's not working, you should ask to the administrator of the server.

Not able to zip folder in remote server

I'm using Phpseclib to retrieve files from remote server. Everything works fine but when I tried to zip a folder to download. The zip file I created using code below remain empty. I'm out of idea how to make it works. Is there something wrong in my code?
$sftp = new Net_SFTP($host);
if (!$sftp->login($user, $password)) {
exit('login failed');
}
$sftp->mkdir($zipfolder);
$sftp->put($zipfolder.'/'.$file, $sftp->get($file) );
$sftp->enablePTY();
$sftp->exec('cd '.$filepath.' && zip '.$zipfilename.' '.$zipfolder);
To ZIP directory you should write:
$sftp->exec('cd '.$filepath.' && zip -r '.$zipfilename.' '.$zipfolder);

How do I make this script copy files to local disk?

I have copied and tried many PHP scripts from SO posts. I am trying to download files from a server running Centos. Via psftp (putty) I can login manually and copy files. But I want to automate the process, hence the need for a script.
On a similar server running on Windows am able to download files by ftp via a simple Perl script. On the Centos server I get connection refused with the Perl script. So I tried several php scripts. Are the scripts below (from SO posts) for the job? or what is wrong with the scripts?
script 1
#!/usr/bin/php
<?php
include('Net/SSH2.php');
$sftp = new Net_SFTP('xx.xx.xxx.xxx');
if (!$sftp->login('myuser', 'mypasswd')) {
exit('Login Failed');
}
// outputs the contents of filename.remote to the screen
echo $sftp->get('gateway_data*');
?>
Script 2
#!/usr/bin/php
<?php
include('Net/SSH2.php');
username='myuser';
password='mypasswd';
// Create SCP connection using a username and password
$scp = new SCP(
'xx.xx.xxx.xxx',
new SSH2Password($username, $password)
);
#################################
$sftp = ssh2_sftp($conn);
// Create a new local folder
ssh2_sftp_mkdir($sftp, './data');
// Retrieve a list of files
$files = scandir('ssh2.sftp://' . $sftp . '/data/gateway_data*');
################################################################
?>
In the first of PHP script you have posted you're doing echo $sftp->get('gateway_data*'); whereas in the Perl script you're doing cp gateway_data_301.txt. Try doing that in the PHP script. eg. echo $sftp->get('gateway_data_301.txt');.
As is it is unclear what you're expecting to happen. Unless the file name /actually/ has a wild card in it then are you expecting it to download every file that starts off with gateway_data* and just concatenate them in the output? Personally, I think just returning false or NULL would be better than that.
You can use your script 2 in PHP. However something is missing there. You are only opening the source directory. You must write a loop over all files in that folder.
// Retrieve a list of files
$files = scandir('ssh2.sftp://' . $sftp . '/data/gateway_data*');
foreach ($files as $key => $value) {
See the example of how to send a file with SFTP using SFTPConnection.

How to execute .sh files one after another with php

I need to run a series of six .sh files on the server.
An example of one of the .sh files:
wget ftp://xxxxxx:xxxxxx#ftp.interhome.com/accommodation.xml.zip
unzip accommodation.xml.zip
php accommodation.php
rm -rf accommodation.xml.zip
rm -rf accommodation.xml
I tried running the following from a php file:
echo shell_exec('sh accomodation.sh');
Which was stupid because the file appears to execute repeatedly and I think I've just taken down the server. Whoops.
I've inherited this site and have never used .sh files before. I'm also a php novice.
How would I go about running the files only once and then running the next?
Many thanks
you can do all this from within PHP, you do not need any shell-script.
/* get the file via ftp */
// connect to server
$ftp = ftp_connect('ftp.interhome.com');
// login
$login = ftp_login($ftp,"username","password");
// download file to tmp.zip
$file = ftp_get($ftp, 'tmp.zip', 'accommodation.xml.zip', FTP_BINARY);
// disconnect from server
ftp_close($ftp);
/* unzip the file */
// new zip-instance
$zip = new ZipArchive;
// open downloaded file
$res = $zip->open(’tmp.zip’);
// check if file is readable
if ($res === TRUE) {
// extract to current directory
$zip->extractTo(’./’);
// close zip-file
$zip->close();
}
/* your code from accommodation.php goes here */
// delete files
unlink('tmp.zip');
unlink('accommodation.xml');
voila

What is a simple PHP File download script?

I only just bought a web domain yesterday(cryptum.net) and I intend to upload some files to it. My problem is I don't have a good PHP5 download script. I just want a simple script that will save the file to the users download folder. The files are located on the websites ftp server. Here is what I have so far-
<?php
$conn = ftp_connect("ftp://username#cryptum.net") or die("Could not connect");
ftp_login($conn,"username","password");
//download script goes here
ftp_close($conn);
?>
Another very elegant solution which uses the powerful stream features of PHP:
<?php
$ftp_server = 'ftp://username:password#cryptum.net/';
$remote_file = 'user/directory/file';
$local_file = '/path/to/the/local/file';
file_put_contents($local_file, file_get_contents($ftp_server.$destination_file));
<?php
//...
//download script goes here
ftp_get ($conn, '/destination/on/my/hardDrive', '/file/to/download');
//...
?>
and you could use php-curl instead...

Categories