On my CentOS machine I have installed PHP. When I log onto that machine from another machine(fedora) through ssh and run a PHP script for writing to a file, it works perfectly.
But when I open a browser from my fedora machine and open the same PHP script, it fails to open that file for writing. The browser works for read and other echo commands.
I don't get the reason why is it happening. What would be a quick solution to my problem?
When you use CLI, you execute script as user that you logged in as through SSH. On the other hand, when you execute a script through a web browser and your have PHP installed as Apache module, it is running as apache user on CentOS (source).
These two users may have different permissions, so one has permissions to write to the file and other does not.
Long story short: change file permissions so apache user can write to it.
Related
I have a PHP script that I am testing with XAMPP that will have a user upload a file, run file through Python script, then output an output Excel file. Everything works great on my local machine. However I want to put this tool on my works internal server (running Ubuntu Linux) so everyone can use the tool. The issue I am running into is that in my PHP script, I check the client's directory to ensure that the output directory is created, if not it is created for them. Since the tool has been moved to the Linux side, I am unsure how I can reference the users directories to check to see if a specific folder exist, or even to specify that location that output should be written.
One thought I had was to call a Python script to SFTP the selected file over to the Linux side, run the process, then SFTP the output file. However, the issue I have found is that I need to know the full path to the file in order to do so, and the upload process only seems to capture the file name itself (for security reasons I understand). Ultimately I could use a suggestion on how I can take this working PHP/Python process using XAMPP, and move it to a local Linux server and still be able to check the Windows file directories and create a Windows folder if needed.
i have used simple PHP script to execute exe file through php script, below is my PHP code
<? exec('"C:\Windows\notepad.exe"');?>
my problem is, when i check task manager after executing above code, notepad.exe is there but it is not diplaying
same way if i execute below mentioned code
<? exec('"C:\Program Files\WinSCP\WinSCP.exe"'); ?>
task manager shows WinSCP.exe, but winscp is not diplaying
can anyone tell me why applications are not showing ?
Whatever your web server is (Apache, Nginx, IIS...) it's clearly not configured to run with the credentials of your administrator user account. That's the sensible default. Background services are designed to run unattended.
You have several choice, from worse to better:
Kill security in your web server configuration. If web server runs as service, open Windows Service Manager and configure it to run with your account (don't forget to come back here if you change your password in the future). This way you allow any stranger in your computer, local network and/or internet to run programs in your name, in this case Notepad and WinSCP.
If the web server allows it, run it from a command-prompt in your own session. It's similar to #1 but will at least not run 24/7.
Any of the above, with a restricted account.
Don't use a web server. PHP can run from the command-line just fine, what means that you can even create a *.bat file and assign an icon to it.
As about your code, when it finally works your script will sleep until you close Notepad. This can be a bug or a feature depending on your needs.
Had the same problem with WAMP 2.4 & windows XP, the following helped:
Win+R, services.msc
edit wampapache and wampmysqld to allow affecting desktop.
Alternatively you can try:
Win+R, services.msc
edit wampapache and wampmysqld to log on as the same user.
I have tried to use the Zend Framework to do my PDF conversion in PHP but cannot figure out how to install it. I found this program that allows for PDFs to be printed/converted via the command line, so I thought I could do an exec() command in PHP to run the program and convert my file on the server for me.
The program is PDFCreator from pdfforge.org and I am trying to convert .DOC files to .PDF files. This command line below works perfectly when running from cmd.exe. However, when running it from PHP I believe that it is trying to execute under the user System and not working properly. (I did a php exec('whoami') and it came back as System.) I also changed the user of which Apache opens up as to an Admin account and still have been unable to get it to work.
"C:\Program Files\PDFCreator\PDFCreator.exe" /OptionsFile"C:\PDFCreator.ini" /PF"C:\test.doc"
Any thoughts as to why it I cannot get the same behavior when running the command using exec()?
Thats because Apache user and account user should be the same, ex: your windows user is Pedro, so Apache user should be Pedro...you shoul open the services.msc and in the session tab allow apache to interact with the current desktop....in that way, the share the same windows session and desktop....but it shoud represent a security risk and it's not recommended.
Saludos.
i read so many similar questions but nothing works with me
im using wamp 2.2 ,Apache 2.0 , PHP V5.3.8
safe_mode=off - disabled_functions deleted from php.ini i'm trying to exec
exec("chrome.exe google.com");
nothing happens and when i try
exec("calc");
the windows shows weird message to execute calc in different user although i changed the user for the apache service to Administrator and i verify the user using
exec("whoami");
where is the problem?
Environment path for CMD and php shell might not be the same.
You either have to give entire path of the file, or export the path of chrome.exe in php first
Also, I believe that chrome.exe opening a page requires XServer, php can't open graphical interfaces in shell. These commands are not passed to actual shell as a parent, so php file can't "launch" applications for you.
Please read specifics of the exec function:
PHP exec Reference
Also try adding 2> errors.txt to see what are the errors if any in the execution of the programme.
--
The program needs to know what X server to connect to, and it needs to have permissions to connect to that server. You specify the X server with the DISPLAY environment variable; this will usually be set automatically if you are running the PHP program from a terminal in
X, or from a GNOME panel or something similar; however, if you are running the PHP script in some other manner, it likely won't know what X server to connect to.
X has various ways of specifying permission to connect to a server, but the most common one is using a file called ".Xauthority" in the users home directory. Because only the user who is logged in at the X server can read this file, they are the only user who can run GUI programs. So, if you start the PHP user as the same user who is logged in at the X server, you shouldn't have any problem with permissions. However, if the PHP program is running as a different user, you will have to give that user permission to access the X server.
Reference: http://bytes.com/topic/php/answers/838364-cant-launch-graphical-apps-php-exec-ubuntu-8-04-system
(I know that link is for linux and won't have exact same solution for Windows, but exec() still needs to know which X interface to refer to)
Stop Apache running as a service.
When windows runs a service it it is not running directly as the user that started the server or manages due to this it could well be starting chrome in a service environment so you wont see it load on your desktop there are 2 ways to get around this
Stop apache service browse to your apache directory and run httpd.exe manualy then try your script it should work or if it is not required to be running though a web request so it not using anything from the browser you can allow it to work with c:\wamp\php\php.exe yourfile.php (your php path should be replaced for c:\wamp\php)
Try PHP script?
<?php shell_exec('notepad.exe');?>
It's working.
I am working a LAMP web app running on Ubuntu 11.10.
I followed instructions on the web to harden my apache, php and mysql.
I have a PHP script which work fine when I run from the command line under my own id. But when I put the scripts into the web app framework, it can't not even open a log file to write (in /tmp) and it can't read other files in /var/www/myapp/html as well.
I used Ajax to retrieve file contents on the server and then serve those files to the browser. So my url will look like: "php/myphpscript.php?arg=.......".
My directory structure is
/var/www/myapp/html|php|js|cfg.
I know this problem has something to do with permission, security but I am quite at loss.
Can someone describe what I need to do here?
Thanks,
I suspect you copied the files with your username, but apache executes as user www-data an thus has no access to your files. Either change them to belong to the apache user, or if you are the only develper on this machine, cahnge apache to run as you.