PHP - How to run a Batch File with parameters? - php

I'm developing a new portal for my business.. I have to start many batch files for different pc names.. so I try to run my batch with one parameter..
$path = $db->givePath($service);
$path = $path . " " . "PCNAME";
if(exec("cmd /c" . $path)){
echo "Successful sent";
} else {
echo "Error";
}
But if I run this command, nothing happens..
The old version worked:
$path = $db->givePath($service);
if(exec("cmd /c" . $path)){
echo "Successful sent";
} else {
echo "Error";
}
Can someone assist me here?

You are likely encountering issues when passing the PCNAME as an argument because it has characters that need to be escaped. You can read more on escapeshellarg the TL;DR is it escapes any control characters that would cause the execution to exit in an unexpected way. Using something like UNC path for PCNAME withouth using escapshellarg() would trick windows into thinking there was another argument being specified.
$path = $db->givePath($service);
$batchCmd = "C:\{$path} " . escapeshellarg('PCNAME');
if(exec("cmd /c {$batchCmd}")) {
echo 'Successfully sent';
} else {
echo 'Error';
}

You can to use cmd shell:
system("cmd /c C:" . $path_to_file);

Related

How I can move file on a SFTP server in PHP

How can I move a file from one directory into another directory?
I have try this, but it doesn't work:
$success = #rename('ssh2.sftp://' . $sftp . $path_from, 'ssh2.sftp://' . $sftp . $path_to);
if ($success) {
echo "moving success!";
}else {
echo "moving failed!";
}
I'm using PHP SSH2 functions.
Can someone help me?
Use ssh2_sftp_rename:
ssh2_sftp_rename($sftp, $path_from, $path_to);
Assuming both variables contain full paths to files, e.g. like:
$path_from = "/source/directory/myfile.txt";
$path_to = "/destination/directory/myfile.txt";

shell_exec does not execute from browser requests

After the user submits a data via POST, I show a temporary page and do the process on the background with shell_exec, atleast that's what I'm trying to do.
Here's my page setup:
C:\laragon\www\index.php
<?php
try {
shell_exec("php " . __DIR__ . "/test.php");
} catch(Exception $e) {
echo "Exception: " . $e;
}
?>
C:\laragon\www\test.php
<?php
$myfile = fopen(__DIR__ . "/testfile.txt", "w");
echo "test";
?>
If I go to localhost or localhost/index.php, the second script doesn't run. However, when I try to call both scripts from cmd, it works with both of them.
php C:\laragon\www\index.php
php C:\laragon\www\test.php
They both work and create a file called testfile.txt
Your webserver runs as a specific user and needs the path to php.exe as there is no path environment variable for the webserver user:
shell_exec("c:\path\to\php " . __DIR__ . "/test.php");

Connection between php and scilab (FuzzyToolbox)

I want to do connection between php and scilab, this my code
try {
$path = 'C:\\wamp64\\apps\\scilab-5.5.2\\bin\\Scilex.exe';
$path_script = "ea=loadfls('C:\\wamp64\\www\\scilab\\estilosaprendizaje.fls');res=evalfls([-11,11],ea); disp(res);exit;";
$command = $path . ' -nb -e "' . $path_script.'"';
echo $command;
exec($command, $output);
foreach ($output as $line) {
print_r($line);
echo "<br />";
}
} catch (Exception $e) {
echo 'Excepción capturada: ', $e->getMessage();
}
but when I run the php, it does not work, it keeps loading, I do not have error messages or anything.
In scilab my code it works.
My output in scilab
Try adding -nw to $command to lauch scilab in console mode. It may be because scilab try to launch itself in graphical mode. I dont have php nor fuzzy toolbox, so I could not test it.

How to use Unlink() function

I'm trying to use PHP unlink() function to delete away the specific document in the folder. That particular folder has already been assigned to full rights to the IIS user.
Code:
$Path = './doc/stuffs/sample.docx';
if (unlink($Path)) {
echo "success";
} else {
echo "fail";
}
It keep return fail. The sample.docx does reside on that particular path. Kindly advise.
I found this information in the comments of the function unlink()
Under Windows System and Apache, denied access to file is an usual
error to unlink file. To delete file you must to change the file's owner.
An example:
chown($tempDirectory . '/' . $fileName, 666); //Insert an Invalid UserId to set to Nobody Owern; 666 is my standard for "Nobody"
unlink($tempDirectory . '/' . $fileName);
So try something like this:
$path = './doc/stuffs/sample.docx';
chown($path, 666);
if (unlink($path)) {
echo 'success';
} else {
echo 'fail';
}
EDIT 1
Try to use this in the path:
$path = '.'
. DIRECTORY_SEPARATOR . 'doc'
. DIRECTORY_SEPARATOR . 'stuffs'
. DIRECTORY_SEPARATOR . 'sample.docx';
Try this:
$Path = './doc/stuffs/sample.docx';
if (file_exists($Path)){
if (unlink($Path)) {
echo "success";
} else {
echo "fail";
}
} else {
echo "file does not exist";
}
If you get file does not exist, you have the wrong path. If not, it may be a permissions issue.
This should work once you are done with the permission issue. Also try
ini_set('display_errors', 'On');
That will tell you whats wrong
define("BASE_URL", DIRECTORY_SEPARATOR . "book" . DIRECTORY_SEPARATOR);
define("ROOT_PATH", $_SERVER['DOCUMENT_ROOT'] . BASE_URL);
$path = "doc/stuffs/sample.docx";
if (unlink(ROOT_PATH . $Path)) {
echo "success";
} else {
echo "fail";
}
// http://localhost/book/doc/stuffs/sample.docx
// C:/xampp/htdocs\book\doc/stuffs/sample.docx
You need the full file path to the file of interest. For example: C:\doc\stuff\sample.docx. Try using __DIR__ or __FILE__ to get your relative file position so you can navigate to the file of interest.

MkDir Failed creating directory PHP

I am trying to create a directory using PHP this works:
<?php
$uid = "user_615";
$thisdir = getcwd();
if(mkdir($thisdir ."/userpics/" . $uid , 0777))
{
echo "Directory has been created successfully...";
}
else
{
echo "Failed to create directory...";
}
?>
but this does not work
<?php
session_start();
$uid = $_SESSION['username'];
$thisdir = getcwd();
if(mkdir($thisdir ."/userpics/" . $uid , 0777))
{
echo "Directory has been created successfully...";
}
else
{
echo "Failed to create directory...";
}
?>
Yes the session variable is populated with the exact same thing as above 'user_615' so why would the second one be failing?
EDIT:
So I took the suggestion of #stefgosselin and re-designed the code to be
<?php
session_start();
$uid = $_SESSION['username'];
$thisdir = getcwd() . "/userpics" . $uid;
if(mkdir($thisdir , 0777))
{
echo "Directory has been created successfully...";
}
else
{
echo "Failed to create directory...";
echo "Your thisdir Variable is:'" . $thisdir . "'" ;
}
?>
And the output is
Failed to create directory...Your thisdir Variable is:'/unified/b/bis/www.mysite.com/jou/userpics/user_615
Any other ideas on what would cause the a Session variable not to be able to used in creating a directory?
As a small tip, I would simply put all of $thisdir in a variable and check if the output of that adds up to the result you are expecting.
IE: Having $thisdir ."/userpics/" . $uid defined in a variable would give you the possibility to easily output and validate the argument value you are passing to mkdir.
Edit: Adjusted minor phrasing for better english translation. Sorry above wasn't clear, Wesley understood the simple point I was clumsily trying to make.

Categories