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
Related
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
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.
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.
When using file_get_contents() to get the file contents of a text file on an FTP server at ftp://ipaddress/somefile.txt, I'm getting the following error:
Warning: file_get_contents() [function.file-get-contents]: connect() failed: Permission denied in filename.php on line 1
But when I access ftp://ipaddress/somefile.txt within my webbrowser, it's absolutely no problem.
Why can my browser open the text file, but can file_get_contents() not?
PS: I don't know if it has something to do with it, but the ini directive allow_url_fopen is on.
I had a similar problem. Turns out it was a problem with SELinux on my server:
I ran this line from the terminal to reslove it:
setsebool -P httpd_can_network_connect on
Can you try to use readfile function like this and see if this gets the file contents:
readfile('ftp://anonymous:email#example.com#' . $ipAddress . '/somefile.txt');
I have been working on a upload file that works fine on localhost (windows) but wont work on my linux server I rent. I want it to move it to a subdir '/files' from where the file is.
it gives the errors:
Warning: move_uploaded_file(/home/taalhulp/domains/taalhulpmanager.nl/public_html/files/6) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/taalhulp/domains/taalhulpmanager.nl/public_html/Gegev_Dossier.php on line 66
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpFMaeAs' to '/home/taalhulp/domains/taalhulpmanager.nl/public_html/files/6' in /home/taalhulp/domains/taalhulpmanager.nl/public_html/Gegev_Dossier.php on line 66
Here is a simplified version of my code
$FotoMap = '/home/taalhulp/domains/taalhulpmanager.nl/public_html/files/' ;
if ($_POST['type'] == 'add' & strlen($_FILES['FileUpload']['name']) > 0 && strlen($_POST["GivenFileName"]) > 0 )
{
if (move_uploaded_file($_FILES['FileUpload']['tmp_name'], $FotoMap . $file_name_id))
{
$_SESSION['echo'] = 'succes';
}
else
{
$_SESSION['echo'] = 'fail';
}
}
it says permission denied but is that because I do something wrong or because i need to contact my provider to tell him to enable that, maybe you I can enable that myself?
Make sure that /home/taalhulp/domains/taalhulpmanager.nl/public_html/files/'s permissions are set to 0777.
I do not recommend doing that as it opens a bunch of security issues. You would be better finding out on which user or group the script is being run and modify the directory to be owned by that user instead.
you probably don't have permissions (PHP engine / apache webserver) to access the directory for temporary uploads. try to set your custom temp path.