Unable to create file
Moderator: General Moderators
Unable to create file
Other than permissions, what other issues could cause PHP to be unable to create a file?
-
crazycoders
- Forum Contributor
- Posts: 260
- Joined: Tue Oct 28, 2008 7:48 am
- Location: Montreal, Qc, Canada
Re: Unable to create file
Errors in your code?
Wrong path?
Wrong path?
Re: Unable to create file
Probably errors in the code, but even when I had it at its simplest, it still didn't work.
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.
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);Re: Unable to create file
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"?
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
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.
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
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
It contains what I put in the form.
Re: Unable to create file
Any warning from the fopen function (make sure warning are enabled)?
Re: Unable to create file
Claiming permission is denied even though I've already changed the permissions. Hmmm.
Re: Unable to create file
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...
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
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.
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
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
To ensure that you are really writing in the same folder.
Additionally, try
Code: Select all
$handle = fopen( dirname(__FILE__).'/'.$filename, 'x+');
Last edited by Benjamin on Thu May 14, 2009 4:33 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Reason: Changed code type from text to php.
Re: Unable to create file
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?
And something I haven't looked into, am I forced to write to the same directory?
Re: Unable to create file
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
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
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.
As for sanitizing, I will be the only one accessing the page and entering information, so it won't be a big deal.