Page 1 of 1
Unable to create file
Posted: Thu May 14, 2009 2:50 pm
by divito
Other than permissions, what other issues could cause PHP to be unable to create a file?
Re: Unable to create file
Posted: Thu May 14, 2009 2:57 pm
by crazycoders
Errors in your code?
Wrong path?
Re: Unable to create file
Posted: Thu May 14, 2009 3:28 pm
by divito
Probably errors in the code, but even when I had it at its simplest, it still didn't work.
Code: Select all
<?php
// Retreiving Form Elements from Form
$thisStart = addslashes($_REQUEST['thisStartField']);
$thisEnd = addslashes($_REQUEST['thisEndField']);
$thisName = addslashes($_REQUEST['thisNameField']);
$thisLocation = addslashes($_REQUEST['thisLocationField']);
$thisGames = addslashes($_REQUEST['thisGamesField']);
$thisStory = addslashes($_REQUEST['thisStoryField']);
$thisStanding = addslashes($_REQUEST['thisStandingField']);
$filename = $thisStanding;
$Content = $_REQUEST['thisStandingsField'];
$handle = fopen($filename, 'x+');
fwrite($handle, $Content);
fclose($handle);
In the end though, it's not a huge issue to manually create the file, although I'd prefer to have it as easy as possible.
Re: Unable to create file
Posted: Thu May 14, 2009 3:39 pm
by requinix
So you have both "thisStandingField" and "thisStandingsField"?
And I hope you don't plan on permanently using user input to decide which file to write to like that. What if they typed "index.php"?
Re: Unable to create file
Posted: Thu May 14, 2009 3:43 pm
by divito
thisStanding is simply for the file name I want for the standings.
thisStandings is for the input, which in the case I'm using it will be standings from tournaments.
Not a huge issue, I could easily change the variable names. And this page is only going to be accessed by myself.
Re: Unable to create file
Posted: Thu May 14, 2009 3:50 pm
by Darhazer
Check what $filename contains? I'm not sure addslashes is suitable for file names, best use basename() to ensure the name contains only filename, not path ...
Re: Unable to create file
Posted: Thu May 14, 2009 3:55 pm
by divito
It contains what I put in the form.
Re: Unable to create file
Posted: Thu May 14, 2009 4:06 pm
by Darhazer
Any warning from the fopen function (make sure warning are enabled)?
Re: Unable to create file
Posted: Thu May 14, 2009 4:13 pm
by divito
Claiming permission is denied even though I've already changed the permissions. Hmmm.
Re: Unable to create file
Posted: Thu May 14, 2009 4:18 pm
by Darhazer
Please post the exact warning.
Exact permission as well.
What is your operating system?
Sorry for asking such question, but it will really speed up finding the problem if we have more information. Sometimes the most obvious mistakes are the harder to find...
Re: Unable to create file
Posted: Thu May 14, 2009 4:24 pm
by divito
Warning: fopen(wcg2001.txt) [function.fopen]: failed to open stream: Permission denied in path/insert_event.php on line 20
Warning: fwrite(): supplied argument is not a valid stream resource in path/insert_event.php on line 21
Warning: fclose(): supplied argument is not a valid stream resource in path/insert_event.php on line 22
Permission for insert_event.php is 777. Server is on Linux.
Re: Unable to create file
Posted: Thu May 14, 2009 4:32 pm
by Darhazer
Well, I assume the file (wcg2001.txt) do not exist in the folder ('cause x+ won't replace / append to file and will fail) and the folder is with 777 permission?
Additionally, try
Code: Select all
$handle = fopen( dirname(__FILE__).'/'.$filename, 'x+');
To ensure that you are really writing in the same folder.
Re: Unable to create file
Posted: Thu May 14, 2009 4:44 pm
by divito
Double-checked the permissions on the folders and the files I was using and it still didn't work, but when I recursed all files, it finally worked. Thanks for all your help.
And something I haven't looked into, am I forced to write to the same directory?
Re: Unable to create file
Posted: Thu May 14, 2009 4:50 pm
by Darhazer
Hi,
Sorry, what means 'recursed all files'?
No, you are not forced to write to the same directory, but it is good to know exactly to what directory you are witting, i.e. use absolute paths. Otherwise you are witting to the current directory ( getcwd() ).
Also, when the filename comes trough the web, sanitize it. If it is not supposed to have any folder information, use basename(). In this way nobody can send you /etc/something as filename. And if it can contain folder names, make sure that the folder is under the path you want... just some general advices

Re: Unable to create file
Posted: Thu May 14, 2009 4:56 pm
by divito
As far as I know, just means to "copy" in a way. I had my folder set as 777 and there is an option to recurse to subdirectories (which would just take the value of the folder I'm on and do it throughout). I chose to recurse all files and folders so now everything was set to 777. Not ideal, but it solved the problem.
As for sanitizing, I will be the only one accessing the page and entering information, so it won't be a big deal.