I am attempting to write a script which automatically unzips 7zip archives. I'm able to get the command to run from the command prompt, but it doesn't work when running from within the php script.
Here's what I have in terms of code:
$filefolder = "F:/dev/";
$filename = "archive.7z";
$filepath = $filefolder . $filename;
$unzip = "cmd /c 7z x " . $filePath . " -o" . $fileFolder . " -spf";
print_r($unzip . "<br>"); //checking to make sure the command is formed correctly
exec($unzip, $outcome, $rtnStatus);
print_r($outcome);
print_r($rtnStatus);
The following is returned for $outcome and $rtnStatus:
Array ( )
1
What am I missing here?
Related
The PHP exec command is not executing the same as the shell's interaction.
cd /var/www/myfolder
zip -r /var/www/myfolder/temp/newfile.zip ./*
generates just a zip of files in the temp directory. However (simplified version):
$zip_dir = '/var/www/myfolder';
$temp_dir = $zip_dir . '/temp/';
chdir($zip_dir);
exec('zip -r ' . $temp_dir . 'newfile.zip ./*', $return);
generates the same zip but with the full path's of var and www (which results in two copies of myfolder so my file is twice as large as needed). The $return however has the same output as the command line execution. Both state only 15 files directories/folders were zipped. There is no mention of var or www in the PHP output.
I believe the chdir() command will not have any bearing on how the commands in exec() are run. So this might fix it:
$zip_dir = '/var/www/myfolder';
$temp_dir = $zip_dir . '/temp/';
$cmd = 'cd ' . escapeshellarg($zip_dir) . ' && zip -r ' . escapeshellarg($temp_dir . 'newfile.zip') . ' ./*';
exec($cmd, $return);
Note we always escape variables being passed to the command line.
But why not just zip within PHP?
<?php
$zip_target = "/var/www/myfolder";
$zip_file = "/var/www/myfolder/temp/newfile.zip";
$zip_temp = tempnam(sys_get_temp_dir(), 'a458');
$zip_obj = new \ZipArchive();
if ($zip_obj->open($zip_temp, ZIPARCHIVE::OVERWRITE)) {
$zip_obj->addGlob("$zip_target/**/*.*");
}
$zip_obj->close();
rename($zip_temp, $zip_file);
My script packs some files with ZIP then uploads this ZIP to another server. After the upload it checks size of the ZIP on the FTP and locally. If sizes are the same- the local ZIP is deleted.
The problem is ftp_size() returns -1. But only in the real script. In my test script it works fine.
Test script works like this:
$f = ftp_connect(HOST);
$res = ftp_login($f, USER, PASS);
$fname = 'archive_2018-09-18_13-39';
$fsize = ftp_size($f, "$fname-img.zip").'';
$fsize2 = filesize("backup/$fname-img.zip").'';
echo $fsize . '<br>' . $fsize2;
and it returns:
22907946995
22907946995
The real script works like this:
$f = ftp_connect(HOST);
$res = ftp_login($f, USER, PASS);
$fname = 'archive_' . date('Y-m-d_H-i');
exec("zip -r -0 backup/$fname-img.zip \"website\" 2>&1");
exec('curl -T "' . "backup/$fname-img.zip" . '" ftp://' . HOST . ' --user ' . USER . ':' . PASS . ' 2>&1');
$fsize = ftp_size($f, "$fname-img.zip").'';
$fsize2 = filesize("backup/$fname-img.zip").'';
echo $fsize . '<br>' . $fsize2;
and it shows:
-1
22907946995
Real script uploads file just fine. It just doesn't show correct size on the FTP server.
So it's not a problem with size of the file and not a problem with FTP connection.
Try moving ftp_connect only after the call to curl. There is possibly some caching involved that prevents the FTP server from returning correct size immediately, if the file is uploaded using a different connection.
Though I'd strongly suggest you to use PHP functions to upload the file.
I am trying to execute a PHP script using crontab but it doesn't seem to work. I am running AWS EC2 Linux and here is the PHP script:
FOREACH (GLOB("*.jpg") AS $filename) {
ECHO "$filename size " . FILESIZE($filename) . "\n";
UNLINK($filename);
}
FOREACH (GLOB("*.jpeg") AS $filename) {
ECHO "$filename size " . FILESIZE($filename) . "\n";
UNLINK($filename);
}
FOREACH (GLOB("*.gif") AS $filename) {
ECHO "$filename size " . FILESIZE($filename) . "\n";
UNLINK($filename);
}
FOREACH (GLOB("*.png") AS $filename) {
ECHO "$filename size " . FILESIZE($filename) . "\n";
UNLINK($filename);
}
When I execute this script manually from a browser, it works normally. But it doesn't work using crontab
Here is my cron command:
00 * * * * php /var/www/html/*****/*****/delete.php
And here is the log:
Nov 28 02:12:01 ip-##-##-##-## CROND[#####]: (root) CMD (/usr/bin/php
/var/www/html/*****/*****/delete.php)
What am I possibly doing wrong?
I just had a similar issue...
Cron is setup correct (I think) but is not running
It is now working for me:
edit /etc/crontab directly and be sure to check your paths.
(You can even cd into the correct directory like this, perhaps that will solve your issue too..)
00 * * * * cd /var/www/html/*****/*****/ && php ./delete.php
Also check if just php will do the trick.
call:
which php
to see the full path of PHP and use that instead.
If it is working fine in browser then you can set the cron job as below,
00 * * * * wget -q -O /dev/null http://example.com/delete.php
this will work same like you calling in browser just change the "http://example.com/delete.php" with the url which you call in browser.
I want to execute shell command from PHP, running under apache. I make script, that shows all info about enviroment.
PHP Script:
<?php
$root = dirname(__FILE__);
$coffeeFile = $root . DIRECTORY_SEPARATOR . 'Script.coffee';
$jsFile = $root . DIRECTORY_SEPARATOR . 'Script.js';
echo "User: " . exec('whoami') . "\n";
echo "Which: " . exec('which coffee') . "\n";
echo "Coffee file perms: " . substr(sprintf('%o', fileperms($coffeeFile)), -4) . "\n";
echo "Js file perms: " . substr(sprintf('%o', fileperms($jsFile)), -4) . "\n";
echo "Dir perms: " . substr(sprintf('%o', fileperms($root)), -4) . "\n";
$command = "coffee -bo $root -c $coffeeFile";
exec($command, $output);
if (filemtime($coffeeFile) > filemtime($jsFile)) {
echo 'compile failed. command: ' . $command . PHP_EOL;
echo "Output: " . implode("\n", $output) . PHP_EOL;
} else {
echo 'compile success. command: ' . $command . PHP_EOL;
}
It I'll execute it from command line from _www user, it will work:
Command:
sudo -u _www php index.php
Output from CLI:
User: _www
Which: /usr/bin/coffee
Coffee file perms: 0777
Js file perms: 0777
Dir perms: 0777
compile success command: coffee -bo /Users/username/htdocs/testcase -c /Users/username/htdocs/testcase/Script.coffee
But if run it from browser, compile fails, but not errors or output is there.
Output in browser:
User: _www
Which: /usr/bin/coffee
Coffee file perms: 0777
Js file perms: 0777
Dir perms: 0777
compile failed. command: coffee -bo /Users/username/htdocs/testcase -c /Users/username/htdocs/testcase/Script.coffee
Output: /* empty array in output */
How it can be? I change my file before every execution, it need to be compiled every time. Users are the same, "which" command works, dir and files have permissions, coffee file is valid. Maybe there are some apache or php.ini settings, that locks execution of some shell commands?
Kindly check does PHP have shell_exec access given or not. Default setting shell_exec is blocked and you have edit php.ini to unblock it and restart service and do things.
Note: Providing shell access can be dangerous as it is the open invitation to the hackers to hack your machine.
My goal is to be able to submit a search query from a web form and have an AppleScript execute the search in DEVONagent. The AppleScript works fine in terminal but I get an error when having PHP do a shell_exec().
<?php
$theQuery = $_GET["Query"];
$cmd = "theSearch=\"$theQuery\" osascript -e \"set theSearch to system attribute " . "\\" . "\"theSearch" . "\\" . "\"\" -e \"tell application " . "\\" . "\"DEVONagent" . "\\" . "\"\" -e \"search theSearch using set " . "\\" . "\"Web (Deep Link)" . "\\" . "\"\" -e \"end tell\" 2>&1";
echo "<pre>$cmd</pre><BR><BR>";
$theResponse = shell_exec ( $cmd );
echo "Your search for \"$theQuery\" has started and the results will be emailed to you once complete.";
echo "<pre>$theResponse</pre>";
?>
I end up with the following error from the $theResponse echo:
83:92: syntax error: Expected end of line but found identifier. (-2741)
I'm thinking maybe a permissions thing but I just cannot figure it out.