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
Related
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.
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.
I am creating a zip file of a given file in PHP. Following is the function
function create_zip($file, $file_name)
{
$zip = new ZipArchive();
$zip_name = $file_name. ".zip"; // Zip name
$zip->open($zip_name, ZipArchive::CREATE);
if (file_exists($file)) {
$zip->addFromString(basename($file), file_get_contents($file));
} else {
return "file does not exist";
}
$zip->close();
return $zip_name;
}
I want to add password protection for the Zip files. I have found following code to create a password protected zip file
system('zip -P password file.zip file.txt');
But it is not working properly. Can you please Guide me How can I add the Password protection on the Zip file?
PHP Zip - http://php.net/manual/en/book.zip.php - doesn't support password protected zip files, you will need do it via command line.
To password protect a file, you need to use zip command line, make sure that the zip command line program is present, it is not installed by default on many systems.
In new PHP 5.6 (currently in beta) you can use your ZipArchive to create password protected archives. All you need is to add this code ZipArchive::setPassword($password)
Edit: Apparently it only supports decryption, not encryption yet.
I realize this is an old thread, but anyone struggling with adding a password to an archive in windows environment, I solved this using winrar command line and exec in PHP.
Download winrar from http://www.rarlab.com/ and include the WinRAR.exe in your PHP script directory, or call it from the right directory in your exec command.
exec("winrar a -p[password] [archive name] [file or folders to include]");
winrar in the above example refers to winrar.exe in the same directory as your script. If winrar.exe is NOT in the same directory as your script, you can simply include the path:
exec("C:\Program Files\Winrar...
So, for example:
exec("winrar a -ppassword archive.zip file.txt");
Will result in an archive named "archive.zip" being created with "file.txt" inside, and a password of "password".
For more information on winrar command line: http://acritum.com/software/manuals/winrar/
I have a zip file into a directory like this:
drwxr-xr-x 2 salome salome 4096 Dec 16 17:41 staff.zip
When I unzip the file with ZipArchive class, all the upzipped files are owner by nobody user. Is there a way to avoid this owner change?
I am able to use ftp if it is required (only as salome user).
This script eventually will be shared at multiple hosts, so the idea is keep it as generic as possible.
You might consider extending the zipArchive class and override the extractTo method to also perform a chown() on the files in the directory.
Based on the use case you discussed in the comments, you also might want to consider the use of the Phar archive format. php.net/manual/en/intro.phar.php
Phar's would allow your module submitters to submit a file file of executable PHP code that you would not need to extract at all.
Ok, I have resolved the problem of nobody user. I will try to explain all my workaround.
#Mike Brant's answer
Mike suggest to me make use of chown() function overriding extractTo() method. Well, before to willing with it, I tested the chown() function standalone constantly it printed the error:
failed to create stream: Permission denied in ...
Looks like chown will not work for the major shared hostings
FTP functions
So, keeping going I though that FTP functions I made a script that works fine, at least for now xD. This is a resume what the script does for one zipped file:
Create a temp file using tmpfile().
Using ftp_fput() to put the temp file in the current directory with the zipped file.
Give write permissions using ftp_site and CHMOD 0777.
Read the zipped file content with $content = $zip->getFromName('zipped-file.txt');.
Put the content to the new file using fputs($fp, $content);.
Close connections
The code below illustrates the complete process
$zip = new ZipArchive;
$ftp_path_to_unzip = '/public_html/ejemplos/php/ftp/upload/';
$local_path_to_unzip = '/home/user/public_html/ejemplos/php/ftp/upload/';
if ($zip->open('test.zip') == TRUE) {
//connect to the ftp server
$conn_id = ftp_connect('ftp.example.com');
$login_result = ftp_login($conn_id, 'user', 'password');
//if the connection is ok, then...
if ($login_result) {
//iterate each zipped file
for ($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
//create a "local" temp file in order to put it "remotely" in the same machine
$temp = tmpfile();
//create the new file with the same name from the extracted file in question
ftp_fput($conn_id, $ftp_path_to_unzip . $filename, $temp, FTP_ASCII);
//set write permissions, eventually we will put its content
ftp_site($conn_id, "CHMOD 0777 " . $ftp_path_to_unzip . $filename);
//open the new file that we have created
$fp = fopen($local_path_to_unzip . $filename, 'w');
//put the content from zipped file
$content = $zip->getFromName($filename);
fputs($fp, $content);
//close the file
fclose($fp);
//now only the owner can write the file
ftp_site($conn_id, "CHMOD 0644 " . $ftp_path_to_unzip . $filename);
}
}
// close the connection and the file handler
ftp_close($conn_id);
//close the zip file
$zip->close();
}
This is the firt step to start a more complex customization, because the code above is not able to know if the zipped file is a "directory" or "file".
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