Koozali.org: home of the SME Server
Contribs.org Forums => General Discussion => Topic started by: alext on February 10, 2011, 10:14:09 AM
-
(Running SME 7.5.1)
I am trying to use fopen to create a new text file and populate it from a mysql database.
My initial idea was for it to be created on the client's local workstation by my server based php script. I soon ran into problems so I changed my strategy slightly and thought about creating it in a shared directory on the server so that the client could access it via samba.
I successfully installed SharedFolders package from contribs.org and tested it for samba access by mapping a drive on the client's MS Windows workstation. I soon ran into problems trying to access this directory via my php script.
I modified my php.ini allow_url_fopen parameter so that phpinfo() shows "on" for both Local Value and Master Value.
To make testing simpler, I created a new directory named XXX in the main-root level of my server and made sure that chmod was 777 and the owner/group was admin/shared.
I changed my fopen statement to reflect this directory and tried again but php wouln't create a file pointer for the output file.
My script is as follows
<?php
$filename = "/XXX/Target.txt";
$fp = fopen($filename, "w");
if(!$fp)
{
echo "Could not create a file pointer.\nFor $filename";
die();
}
fwrite($fp, 'Got it');
fclose($fp);
?>
I copied a simple text file, Source.txt, to the /XXX directory and ensured that the access was 777 then modified my script to do an fgets call.
I got the same file pointer problem: (script below).
<?php
$filename = "/XXX/Source.txt";
$fp = fopen($filename, "r");
if(!$fp)
{
echo "Could not create a file pointer.\nFor $filename";
die();
}
$inbuf = fgets($fp);
fclose($fp);
?>
I guess by problem is something to do with access rights and/or my path details.
Can anybody help me?
(Sorry if my description is a little unclear)
Thanks.
Alex
-
Moving to General Discussion
-
You might be hitting a PHP BaseDir restriction (which is also mentioned in the function help on PHP: http://php.net/manual/en/function.fopen.php). Here is how to solve ibay PHP BaseDir restrictions: http://wiki.contribs.org/PHP#Open_basedir_restriction
Your best bet is to go looking through the httpd logfiles (/var/log/httpd/error_log*) and see what that says, that makes troubleshooting a lot more to the point than the self programmed error message that only shows the handle to you file does not exist.
-
Thank you Cactus, your advice has narrowed down the problem.
I checked in the messages.log file and found:
Feb 11 00:19:43 mordor httpd: PHP Warning: fopen(): open_basedir restriction in effect. File(/XXX/Target.txt) is not within the allowed path(s): (/home/e-smith/files/ibays/foi/) in /home/e-smith/files/ibays/foi/html/admin/TEST-filesave-3.php on line 3
Feb 11 00:19:43 mordor httpd: PHP Warning: fopen(/XXX/Target.txt): failed to open stream: Operation not permitted in /home/e-smith/files/ibays/foi/html/admin/TEST-filesave-3.php on line 3
I went to your suggested site, http://wiki.contribs.org/PHP#Open_basedir_restriction, and attempted to execute the instructions
But after entering the first command, db accounts getprop foi PHPBaseDir, I got no output and was just returned to the prompt.
I then tried db accounts show and got a listing of all accounts. Checking just the foi=ibay section I found no key labeled PHPBaseDir (in fact none of the ibay sections had this key).
Am I looking in the wrong place?
Is PHPBaseDir set up somwhere else as a global value for all ibays?
Cheers,
Alex
-
But after entering the first command, db accounts getprop foi PHPBaseDir, I got no output and was just returned to the prompt.
That is normal, it means nothing additional is entered and that it will be set (per default) to the html folder of your ibay.
Is PHPBaseDir set up somwhere else as a global value for all ibays?
No, just follow the rest of the instructions but be sure to also include the absolute path to the html folder of your ibay as well as the additional folder(s) where you want to write your files.
To make things easier you should be able to write to the files folder in your ibay by default, can't your users pull the file from there? It should save you the PHPBaseDir modification.
You should also be able to make users download the file without writing them to the filesystem, here is a very limited example on how to do that: http://www.plus2net.com/php_tutorial/php_file_download.php
-
OK, I will try to set up PHPBaseDir later today and see how it goes.
I will also try the example from the file download dynamic data web page. It looks promising and also very relevant as I want my users to be able to easily download a CSV file containing address data which is created from a mysql database.
I too thought about using the files folder in the ibay. So I changed the $filename parameter in my test programmes to see if it worked.
The fopoen($filename, "r") test worked but the fopen($filename, "w") failed to create a file pointer.
(Code for both tests below. The ../../ is due to the fact that the running directory is one lower than the html folder):
<?php
echo "start<br>";
$filename = "../../files/Source.txt";
$fp = fopen($filename, "r");
if(!$fp)
{
echo "Could not create a file pointer.<br>For $filename";
die();
}
$inbuf = fgets($fp);
echo $inbuf . "<br>";
fclose($fp);
?>
<?php
echo "start<br>";
$filename = "../../files/Target.txt";
$fp = fopen($filename, "w");
if(!$fp)
{
echo "Could not create a file pointer.<br>For $filename";
die();
}
fwrite($fp, 'This is some output text');
echo "end<br>";
fclose($fp);
?>
Thanks for your help Cactus.
-
I too thought about using the files folder in the ibay. So I changed the $filename parameter in my test programmes to see if it worked.
The fopoen($filename, "r") test worked but the fopen($filename, "w") failed to create a file pointer.
Most likely because the user the webserver and thus php is running under (www) does not have write access to the ibay.