I try this
in var/www/shop/ I have composer.json and lock
putenv('COMPOSER_HOME=' . static::$root); // var/www/shop/
$cmd = '/usr/bin/composer "cd ' . static::$root . 'composer show" 2>&1';
exec($cmd, $output, $return);
$result = print_r($output,true) . ' - ' . $return;
the result is :
Array ( [0] => [1] => [2] => Command "cd var/www/shop/composer show" is not defined. [3] => [4] => ) - 1
If I do inside the directory : var/www/shop/
composer show
I have the good result
thank you.
I think your issue is trying to do the directory change as the composer command, which will not work. You should be able to run multiple commands separating them with &&. Try this:
$cmd = 'cd ' . static::$root . ' && composer show';
Related
Im trying to launch a jar file with an UI but when i run my code the UI doesnt appear which probably means the jar its not running properly and i dont understand why because when i run the string in the cmd variable on the command line it works fine.
public function actionImport($id){
$javaComand = 'C:/RobotToTestLink/ExecutableJars/uploadxpdl.jar ' . '0 ' . '4 ' . 'PFDemoSuite ' . '1';
$cmd = 'java -jar ' . $javaComand;
$response = exec($cmd);
if($response !== null) {
$caseFind = NodesHierarchy::find()
->where(['parent_id' => $id])
->all();
return $this->render('view', [
'model' => $this->findModel($id),
'cases' => $caseFind,
]);
}else{
print ("Failed loading .Jar File");
}
}
try the following :
1 - put uploadxpd1.jar in the same directory as your XController.php
2 - give the jar file all access rights
3 - use exec($cmd , $outputArr); // outputArr will containe all output from command
I am organizing my folders, the thing is I can't move my files due to permissions but when I checked, folder being created, files being moved and upper level folder is associated with same user and group apache:apache. I am also running this php code with apache user. What could be the problem?
Quick view;
exec('mv '.$path.$result->token.'-*.mp4 '.$path.getWeekNo($result->date).'/ 2>&1', $moveOut, $moveResult);
Code:
function processFilesByID($db, $path, $id, $type ='token')
{
$result = $db->get_row('SELECT id,token,title,date FROM DB_TABLE where id="' . $id . '";');
/* Debug - Start */
$db->debug();
// return "ID: " . $result->id . "<br />Token: " . $result->token . "<br />Title: " . $result->title . "<br />Date: " . $result->date;
/* Debug - End */
if (isset($result)) {
// Check if any file exists with given token command.
exec('ls -1 ' . $path . $result->token . '-* | wc -l', $out, $res);
// Check if noOfFile is greater than 0 and less than 10. Also check if command succeed.
if ($res == 0 && $out[0] > 0 && $out[0] < 10) {
// Move its media files [WHERE PROBLEM OCCURS]
exec('mv '.$path.$result->token.'-*.mp4 '.$path.getWeekNo($result->date).'/ 2>&1', $moveOut, $moveResult);
if($moveResult == 0){
// Double check if it's moved succesfully.
exec('ls -1 ' . $path.getWeekNo($result->date).'/'. $result->token . '-* | wc -l', $newOut, $newRes);
if ($newRes == 0 && $newOut[0] > 0 && $newOut[0] < 10) {
// Update DB with new path
if($db->query("UPDATE DB_TABLE SET token = '".getWeekNo($result->date).'/'.$result->token."' WHERE id= '".$result->id."'")){
}else{
}...
Output
Array
(
[0] => mv: cannot move '/###PATH###/+++FILE-NAME1+++' to '/###PATH###/16401c4a73848b7be4720d2e13a50a430c7b/+++FILE-NAME1+++': Permission denied
[1] => mv: cannot move '/###PATH###/+++FILE-NAME2+++' to '/###PATH###/16401c4a73848b7be4720d2e13a50a430c7b/+++FILE-NAME2+++': Permission denied
[2] => mv: cannot move '/###PATH###/+++FILE-NAME3+++' to '/###PATH###/16401c4a73848b7be4720d2e13a50a430c7b/+++FILE-NAME3+++': Permission denied
)
I also tried with php's built-in command but no luck so far.
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?
Following Command search a file in a directory and zip it,it works well
$command = "cd {$root}/files && mkdir -p {$identifier} && zip -jFS -0 {$root}{$zipname} 2491/'test&.txt'";
exec($command);
But changing files as variable is not allowing shell to execute,the below code does not work
$container_name = "2491";
$files = Array ( '0' => 'test&.txt' ,'1' => 'test5.txt','2' => 'test6.txt');
$files = " " . $container_name . "/'" . implode("' " . $container_name . "/'", $files) . "'";
$files = str_replace('$', '\$', $files);
$command = "cd {$root}/files && mkdir -p {$identifier} && zip -jFS -0 {$root}{$zipname} {$files}";
exec($command);
$root,$identifier,$zipname not causing the problem,its $files What can be the issue?
Update
var_dump for $command before execution:
string(128) "cd /var/www/files && mkdir -p zip--1 && zip -jFS -0 /var/www/files/zip--1/1002_22-06022-06022-_content.zip 2491/'test&.txt'"
which if I execute as
exec("cd /var/www/files && mkdir -p zip--1 && zip -jFS -0 /var/www/files/zip--1/1002_22-06022-06022-_content.zip 2491/'test&.txt'");
runs perfectly
Error Reponse:
zip error: Nothing to do! (/var/www/files/zip--1/1002_22-06022-06022-_content.zip)
I got it fix, issue was with file name having '&' which was chanding to '&', thus breaking the command. To get it I passed through the file name with [htmlspecialchars_decode].1
I'm using a Linux local computer and need to backup/mirror some very large file structures regularly. I only have access to SFTP.
I was after a simple one click solution. I originally tried to write the little script in BASH but I've never used it before and am not up to scratch with the syntax so I resorted to PHP. (I do understand PHP is not designed for this kind of work, but I'm on a tight time scale and don't have the time to get into BASH atm)
<?php
//init
parse_str(implode('&', array_slice($argv, 1)), $_GET);
$error = array();
$lPrefix = '/home/hozza/Sites/';
$archiveLocation = '/home/hozza/Backups/';
$lDir = isset($_GET['l']) ? $_GET['l'] : $error[] = 'Local Directory Required';
$rDir = isset($_GET['r']) ? $_GET['r'] : $error[] = 'Remote Directory Required';
$bookmark = isset($_GET['b']) ? $_GET['b'] : $error[] = 'lftp Bookmark Required';
//Check for args
if(count($error) == 0) {
$archiveName = end(explode('/', $lDir)) . '_' . date('Y-m-d_H-i');
//Validate local dir
if(is_dir($lPrefix . $lDir)) {
//preserve Sublime Text 2 config SFTP files
$ST2_SFTP_conf = false;
if(file_exists($lPrefix . $lDir . '/sftp-config.json')) {
$ST2_SFTP_conf = file_get_contents($lPrefix . $lDir . '/sftp-config.json');
unlink($lPrefix . $lDir . '/sftp-config.json');
}
//Start mirror
$lftOutput = explode("\n", shell_exec('lftp -e "mirror -e -p --parallel=10 --log=' . $archiveLocation . 'logs/' . $archiveName . '.txt ' . $rDir . '/ ' . $lPrefix . $lDir . '/; exit top" ' . $bookmark));
//Tar regardless of lftp error or success
$tarOutput = shell_exec('cd ' . $lPrefix . ' && tar -czf ' . $archiveLocation . $archiveName . '.tar.gz ' . $lDir);
//Output completion or errors
shell_exec('notify-send -i gnome-network-properties -t 0 "Mirror & Archive Complete" "' . $archiveName . '\n\n' . implode('\n', $lftOutput) . $tarOutput . '"');
//put back ST2 SFTP conf
if($ST2_SFTP_conf != false) file_put_contents($lPrefix . $lDir . '/sftp-config.json', $ST2_SFTP_conf);
exit;
}
else shell_exec('notify-send -i error -t 0 "Mirror & Archive Error" "' . date('Y-m-d') . ' ' . date('H-i') . '\n' . $lDir . ' \n Does not exist! D:"');
}
else shell_exec('notify-send -i error -t 0 "Mirror & Archive Error" "' . date('Y-m-d') . ' ' . date('H-i') . '\n' . implode('\n', $error) . '"');
?>
It can be run for many sites via a short-cut like so...
terminator -T "Mirror & Archive" -e "php ~/Programs/mirror.php l=local-dir_path r=./ b=lftp-bookmark-name"
If no password is in the LFTP bookmark (there shouldn’t be as it's stored in plain text) the terminal prompts for a password, after the script has run, a nice notification is given with some info about files/folders/speed etc.
However, when the script is running in a terminal, only the "input password" bit is output to the terminal, I would like all the output displayed in the terminal (normally that would display what file/folder is currently working with etc.)
Anyone know how to do that?
IIRC the reason that you see the password prompt output to the terminal is that it is using stderr. You could try redirecting stdout to stderr for your commands which should show you the 'real-time' progress. Tack this on to the end of the shell_exec() command: 1>&2
ie:
shell_exec('lftp -e "mirror -e -p --parallel=10 --log=' . $archiveLocation . 'logs/' . $archiveName . '.txt ' . $rDir . '/ ' . $lPrefix . $lDir . '/; exit top" ' . $bookmark . ' 1>&2')
However, this will preclude you from having anything returned by shell_exec for logging purposes. What I would suggest is something like:
$log_stem = '/tmp/' . time() . '_'; // ie: /tmp/1357581737_
$lfOutfile = $log_stem . 'lftp.log';
$tarOutfile = $log_stem . 'tar.log';
shell_exec('lftp -blah | tee ' . $lfOutfile ' 1>&2' );
shell_exec('tar -blah | tee ' . $tarOutfile ' 1>&2' );
$lfOut = file_get_contents($lfOutfile);
$tarOut = file_get_contetns(tarOutfile);
// remove tmp files
unlink($lfOutfile);
unlink($tarOutfile);
Which will capture a copy of the output to a file before redirecting the output to stderr so you can watch it live.
However, if you want to run this via cron I would recommend against writing anything to stderr that is not an error, otherwise cron will send a warning email every time it is run.
I think the last answer was close:
Either this:
shell_exec('lftp -blah |& tee ' . $lfOutfile );
shell_exec('tar -blah |& tee ' . $tarOutfile );
Or if that still doesn't work try this:
shell_exec('lftp -blah 2>&1 | tee ' . $lfOutfile );
shell_exec('tar -blah 2>&1 | tee ' . $tarOutfile );