I have a PHP script that is trying to open the directory "C:\Users\userA\Desktop". However, the opendir() call is returning false. Here is my code:
$path = 'C:\Users\userA\Desktop';
var_dump (is_dir($path)); //prints TRUE
var_dump (is_readable($path)); //prints TRUE
var_dump (is_writable($path)); //prints TRUE
var_dump (open_dir($path)); //prints FALSE
When I run the above from the command line, everything is TRUE, and works as expected. When I run it via an Ajax call from a web browser, opendir fails. This is running in IIS 7.5. I have set the permissions for that directory to full permissions for "Everyone". Yet, it still seems to be some sort of permissions issue. I'm not sure where to go from here.
I found the solution to my problem. I had added permissions to "C:\Users\userA\Desktop". I needed to go up one more directory. I granted IIS_IUSRS privileges to "C:\Users\userA", and it solved my problem.
Related
Ive tried searching for the answer but cant find anything proper in relation to my case.
What i want to execute is very simple...its a call to the SPMF framework jar file.
here is the code -
<?php
exec('java -jar spmf.jar run Apriori /opt/lampp/htdocs/rrugd/ip.txt /opt/lampp/htdocs/rrugd/output.txt %20');
?>
why is this not executing?
it obviously runs perfectly using cli.
the folder having all the files (jar file,ip.txt,output.txt) have permissions set to r/w to all users
I am on Linux - Ubuntu 13.10
I found the solution.
The problem was in the path to the spmf zip file.
Either it needs a full path or should be in the same folder as the script.
I solved it by placing it in the folder same as the script.( under htdocs as i am using lampp)
Also, the folder needs to have permissions for other to be set to "Create and Delete" for php to be able to access and write output to the folder.
Basic debugging: If exec() doesn't work, then you need to check return values:
exec('some command here', $output, $return_var);
var_dump($output, $return_var);
NEVER assume that an external resource succeeded. ALWAYS check return values and output for error messages.
Most likely java isn't in the path of whatever shell PHP is using to handle the exec() call, meaning you'll need exec('/full/path/to/java ...') instead.
This particular PHP file works perfectly when executed via the browser. However, I'd like it to run on task scheduler in Windows so I set the scheduler to launch php.exe and point it to the correct file.
Task scheduler is basically doing the same thing as if I type it directly into the CLI I believe. Now, it seems to have worked a few times but now it repeatedly fails even when I manually call the task via CLI.
The relevant code is:
include_once("simple_html_dom.php");
$results = ....Some CURL Commands to retrieve data....
$html = str_get_html($results);
foreach($html->find('tr') as $tr)
{
....do stuff....
}
In CLI it says
Fatal error: Call to a member function find() on a non-object in C:\php\report.php on line...
Why does CLI find fault here and browser does not? Again, this has worked once or twice on CLI so it might some kind of time-out setting.
When you run the script on CLI, did you check if file_get_html() is returning FALSE?
If that is the case, maybe the script can't reach the resource from the terminal using curl for some reason (e.g.: proxy settings).
Make sure to check that case on what you get from that function with something like:
$html = str_get_html($results);
if ($html !== FALSE) {
// treat the success case.
}
All your answers led me to figure out the problem. I investigated the permissions angle but that didn't solve it. There is another 'include' file I have called common_functions.php which I also include. Permissions on that also didn't solve the problem.
However, the curl function is actually located in common_functions. Upon investigating that file, it contains references to cookies.txt where the path was not absolute. I had not setup my environment variables correctly so CLI could not find the cookie which made the Curl function fail.....I corrected and it works now.
Lesson learned. Thank you all for the clues you provided.
I'm now facing the following problem. Please help me out.
I have two file in the same directory: test.php and test.r
test.php
<?php
exec("Rscript test.r");
?>
test.r
d=c(1:10);
write.csv(d, file="test.csv", fileEncoding="utf-8");
q();
While I access the test.php via chrome, it gives no test.csv.
Although it works fine on cmd Rscript test.r, it does not work while executing test.php.
Any suggestion?
Thanks
There are a few things to check:
File permissions. Set them to 0777 temporarily to see if it's working
Maybe the exec function is blocked. Check php.ini (or phpinfo()) for disabled_functions
Third options - is one you really always need to consider - an error in your script.
Happy debugging !
PHP/IIS will need to read/execute permissions to the command you are trying to execute with your exec() function.
Go through PHP documentation for exec() function article.
Hope, It will resolve your problem.
I am having difficulty with the PHP exec() function. It seems to not be calling certain functions. For instance, the code echo exec('ls'); produces no output whatsoever (it should, there are files in the directory). That main reason this is a problem for me is that I'm trying execute a .jar from a PHP exec() call.
As far as I know I'm calling the java program properly, but I'm not getting any of the output. The .jar can be executed from the command line on the server. (For the record, it's an apache server).
My php for the .jar execute looks like this:
$output = array();
exec('java -jar testJava.jar', $output);
print_r($output);
All I get for output from this exec() call is Array().
I have had success with exec() executing 'whoami' and 'pwd'. I can't figure out why some functions are working and some aren't. I'm not the most experienced person with PHP either, so I'm not too sure how to diagnose the issue. Any and all help would be appreciated.
The reason why you are not able to execute ls is because of permissions.
If you are running the web server as user A , then you can only ls only those directories which have permissions for user A.
You can either change the permission of the directory or you can change the user under which the server is running by changing the httpd.conf file(i am assuming that you are using apache).
If you are changing the permissions of the directory, then make sure that you change permissions of parent directories also.
To change the web server user, follow following steps:
Open the following file:
vi /etc/httpd/conf/httpd.conf
Search for
User apache
Group apache
Change the user and group name. After changing the user and group, restart the server using following command.
/sbin/service httpd restart
Then you will be able to execute all commands which can be run by that user.
EDIT:
The 'User' should be a non-root user in httpd.conf. Apache by default doesnot serve pages when run as root. You have to set user as a non-root user or else you will get error.
If you want to force apache to run as root, then you have to set a environment variable as below:
env CFLAGS=-DBIG_SECURITY_HOLE
Then you have to rebuild apache before you can run it as root.
I have found the issue - SELinux was blocking PHP from accessing certain functions. Putting SELinux into permissive mode has fixed the issues (although, I'd rather not have to leave SELinux in permissive mode; I'd rather find a way of allowing certain functions if I can).
I have a solution:
command runs from console, but not from php via exec/system/passthru.
The issue is the path to command. It works with the absolute path to command
So that:
wkhtmltopdf "htm1Eufn7.htm" "pdfIZrNcb.pdf"
becomes:
/usr/local/bin/wkhtmltopdf "htm1Eufn7.htm" "pdfIZrNcb.pdf"
And now, it's works from php via exec
Where command binary you can see via whereis wkhtmltopdf
Tore my hair out trying to work out why PHP exec works from command line but not from Apache. At the end, I found the following permissions:
***getsebool -a | grep httpd*** ---->
**httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_sys_script_anon_write --> off**
USE: setsebool -P httpd_ssi_exec 1
SEE: https://linux.die.net/man/8/httpd_selinux
Your problem is not an execution issue but the syntax of the exec command. The second argument is always returned as an array and contains a single line of the output in each index. The return value of the exec function will contain the final line of the commands output. To show the output you can use:
foreach($output as $line) echo "$line\n";
See http://php.net/manual/en/function.exec.php for details. You can also get the command's exit value with a third argument.
I'm running wampserver and php5. exec() works when I run the script through the command-line, but when I try to run it through the server, it fails.
I looked at all the error logs, there were none. I redirected stderr to stdout, there was still no output when I run it from the server.
Any suggestions ?
EDIT: I should have mentioned -- I'm running on WinXP and safe_mode is off.
Perhaps you need to turn off safe_mode in php.ini (safe_mode = Off)
Can you post what exactly (code) you try to run?
Did you try this instead?
$return = system('dir');
Do you have a problem with the directories on the server maybe?
Just had the same problem. I don't whether this is a different case than yours or not.
I was trying to create new file(image) in a directory. Apparently the problem was on the folder permission. I used Linux Ubuntu.