PHP XML Get Error - php

I wrote a PHP Application (that works) and I have moved it to a different directory. As a result it needs to get a file from a directory above it, but the file must remain hidden, ie:
Root
--config
---users.xml (Permissions 600)
--lib
----loginUtil.php
I have tried two different methods of getting the file:
$xml = file_get_contents("../config/users.xml");
Which returns the following Error:
[13-Jun-2012 08:54:04] PHP Warning: file_get_contents(../config/users.xml) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: No such file or directory in public_html/test/lib/loginUtil.php on line 18
and
$xml = simplexml_load_file("../config/users.xml");
which returns
[13-Jun-2012 08:56:17] PHP Warning: simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: I/O warning : failed to load external entity
I have tried using the full URL, and that works, but I must then make the users.xml file public (as PHP is doing a URL Request).
What can I do to resolve this issue? (Thanks for your help)

try setting the full path and not the relative
$path = '/var/full/path_dir/file.xml';
if(!is_file($path)){
// if this is the is output then you need to check permissions
// double check the file exists also.
// check the web service can read the file
echo "FILE NOT FOUND FOR $path";
}
else{
$xml = file_get_contents($path);
echo $xml;
}

Related

PHP failed to open stream: Is a directory

Very simple PHP script I'm running within an EE template on an Ubuntu Webserver I set up personally.
I know it has something to do with permissions, and I've already changed the owner of the directory I'm trying to write as being the Apache user.
<?php
$dir = 'export';
$drop =' {exp:ajw_export
sql="SELECT member_id, screen_name, email FROM exp_members"
format="xml"
} ';
echo $dir;
file_put_contents($dir, $drop);
?>
Error I get is:
A PHP Error was encountered
Severity: Warning
Message: file_put_contents(export): failed to open stream: Is a
directory
Filename: libraries/Functions.php(689) : eval()'d code
Line Number: 16
Your problem is that you're not defining the actual file, only a directory.
$file = $dir . '/export.txt';
file_put_contents($file, $drop);
Otherwise, how will PHP know where to place the content?

Exec and Include function not working

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

PHP fgets() warning

I have following code:
$fh=fopen($api,"r");
$theData = fgets($fh);
echo $theData;
This throws the error as :
Warning: fopen(http://xxxxxx.xxxxxxxx.com:8080) [function.fopen]: failed to open stream: Connection refused in /home/xxxx/public_html/xxxx/index.php on line 18
Warning: fgets(): supplied argument is not a valid stream resource in /home/xxxx/public_html/xxxx/index.php on line 19
What is the solution for this?
Your file did not open as you intended. Check the value of $api and be sure the file name is in the current working directory, or the entire path to the file.
Since it is a remote system on :8080, be sure the port is open to the public. This is typically a value reserved for localhost (ie: the webserver is only a webserver to that computer, no one else's) and wouldn't be accessible to you. If the url you are opening is the same computer you are using, navigate to the file using local folders instead.

load xml content from external xml file to php

I have written simple code to get content from xml file to php.
$xml = simplexml_load_file("http://localhost/xml_load/test_xml.xml");
print_r($xml);
First time It has run successfully but right now it is giving me warning and not executing properly.
Warning: simplexml_load_file(http://localhost/xml_load/test_xml.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! <?xml version="1.0" encoding="ISO-8859-1"?> in C:\xampp\htdocs\XML_load\load_file.php on line 2
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://localhost/xml_load/test_xml.xml" in C:\xampp\htdocs\XML_load\load_file.php on line 2
No file found on your respective location. Now, either check if your file exist. However, do try to make your local path doesn't based over BASEURL. Add that path further like
$xml = simplexml_load_file("xml/files/myxml.xml"); // PATH TO YOUR FILE.
echo "<pre>"; print_r($xml); "</pre>";

move_uploaded_file() not working as expected (i.e at all) in PHP

using this line
$file_move = move_uploaded_file($_FILES['uploadedfile']['tmp_name'],
plugins_url('/css', __FILE__));
returns:
move_uploaded_file(http://localhost/*) [function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections in /Applications/MAMP/htdocs/***/as_settings.php on line 60
I have checked both arguments, and they are correct. I'm new to this side of coding, what have I missed?
---EDIT
In response to answers, have changed code to:
$dir = ABSPATH . 'wp-content/plugins/app-switcher/css';
$file = $_FILES['uploadedfile']['tmp_name'];
$file_move = move_uploaded_file($file,$dir);
Now my error response is:
Warning: move_uploaded_file(/Applications/MAMP/htdocs//wp-content/plugins/app-switcher/css/) [function.move-uploaded-file]: failed to open stream: Is a directory in /Applications/MAMP/htdocs//wp-content/plugins/app-switcher/as_settings.php on line 61
The error message is pretty obvious, your destination file should be a path, not a URL
You can't use a http:// URL as the target for move_uploaded_file(). You need to use a file path.
You're not saying what framework you are using, but it may have a counterpart to plugins_url() that returns a file path.
second parameter should not be URL
It's still obvious.
you have to pass a filename, not directory to this function

Categories