Unable to create file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Unable to create file

Post by divito »

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

Post by crazycoders »

Errors in your code?
Wrong path?
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: Unable to create file

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Unable to create file

Post 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"?
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: Unable to create file

Post 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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Unable to create file

Post 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 ...
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: Unable to create file

Post by divito »

It contains what I put in the form.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Unable to create file

Post by Darhazer »

Any warning from the fopen function (make sure warning are enabled)?
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: Unable to create file

Post by divito »

Claiming permission is denied even though I've already changed the permissions. Hmmm.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Unable to create file

Post 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...
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: Unable to create file

Post 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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Unable to create file

Post 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.
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.
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: Unable to create file

Post 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?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Unable to create file

Post 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 :-)
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: Unable to create file

Post 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.
Post Reply