Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

File I/O

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

File I/O

Postby elitecodex » Tue Aug 13, 2002 5:39 am

Im having problems creating a file on my server. Here is my code:

Code: Select all
     $config_handler = fopen("qs_config.php", "w+");
     if ( ! $config_handler )
     {
        fclose($config_handler);
        echo "<br>ERROR - Could not create $config_file<br>";
        exit();
     }

Im at a lost as to why. fopen doesnt return anything, all i see is the error message. The permissons on the folder that these files are in are 755. Im confused, what am I not seeing?

Thanks for your help.

Will
elitecodex
New php-forum User
New php-forum User
 
Posts: 68
Joined: Tue Jul 09, 2002 8:45 am
Location: East Coast, USA

Postby Pim » Tue Aug 13, 2002 6:33 am

Hi Elite,

I think your code works better then you realise!!!

Code: Select all
     $config_handler = fopen("qs_config.php", "w+")
     or die ("could not open file");
     if ( ! $config_handler )
     {
        fclose($config_handler)
        or die "<br>ERROR - Could not close $config_file<br>";
     }
     exit();


I have not tested this but I think you'll see that you coded everything correctly. Only the Echo was unconditional!!!! You told the program to echo it. End so it did.

Please in the future always code an exception behind a IO call. (eg Open, Close, Write, delete, etc)

Good luck,
pim.
Pim
New php-forum User
New php-forum User
 
Posts: 18
Joined: Tue Aug 06, 2002 3:51 am
Location: Netherlands

Postby elitecodex » Tue Aug 13, 2002 6:40 am

I thank you for your input, and have changed my code accordingly. However, I still get the "Could not create file" error. What else am I doing wrong? And if im not doing anything wrong, then why isnt it working?? :?:

Code: Select all
$config_handler = fopen("qs_config.php", "w+") or die("Could not create file");
     if ( ! $config_handler )
     {
        fclose($config_handler) or die("could not release file handle");
        exit();
     }


Thank you
elitecodex
New php-forum User
New php-forum User
 
Posts: 68
Joined: Tue Jul 09, 2002 8:45 am
Location: East Coast, USA

Postby Pim » Tue Aug 13, 2002 7:08 am

I think this piece of code has all you need. Can you check if this will work.


Code: Select all
     //Pick a file you like.
$Filename="qs_config.php";
     //Open the damn file for input
config_handler = fopen($filename, "r+") or die("Could not create file");
    //Get some input to fool around with
$contents = fread ($config_handler, filesize($filename));
     //replace all yes to no
$new_contents = str_replace("YES", "NO", $contents);
    //Reset read/write pointer
rewind($config_handler) or die ("Unable to set pointer at beginning of file");
    //Write new input to file
fwrite($config_handler, &new_contents) or die ("could not write changes to file");
    //ftruncate assures there wont be extra
    //characters if the resulting file is shorter
    //then the original file.
ftruncate($config_handler,ftell($config_handler));
    //Release file pointer
fclose($config_handler) or die("could not release file handle");
    //All good things must come to an end!
exit();


Pim
Pim
New php-forum User
New php-forum User
 
Posts: 18
Joined: Tue Aug 06, 2002 3:51 am
Location: Netherlands

Postby Pim » Tue Aug 13, 2002 7:13 am

Except for the creation of a file that is.

But when this works the create should work also.
Maybe there is a specific point where it fails.

Pim
Pim
New php-forum User
New php-forum User
 
Posts: 18
Joined: Tue Aug 06, 2002 3:51 am
Location: Netherlands

Postby WiZARD » Tue Aug 13, 2002 7:14 am

elitecodex wrote:I thank you for your input, and have changed my code accordingly. However, I still get the "Could not create file" error. What else am I doing wrong? And if im not doing anything wrong, then why isnt it working?? :?:

Code: Select all
$config_handler = fopen("qs_config.php", "w+") or die("Could not create file");
     if ( ! $config_handler )
     {
        fclose($config_handler) or die("could not release file handle");
        exit();
     }


Thank you

Hi!
Check right for creating file at your server , I think what it's not PHP it's server config. Or create code for login at server and after that create file...
Good Luck! :wink:
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Postby elitecodex » Tue Aug 13, 2002 7:20 am

That would be fine, but my problem is that I need this file to be created dynamically. So manually creating the file would not do what i need it to do. I have double and triple checked my code, and realize that it is not wrong. Not to mention, two others have as well. What are the file persmissions suppose to be? Do they need to be a specific value for the folder its in, or the file calling the "fopen()"? I have been able to read from files before in other test scripts I have written, but this is my first time actually trying to dynamically create a file and write to it.

Thanks for everyone's help

Will
elitecodex
New php-forum User
New php-forum User
 
Posts: 68
Joined: Tue Jul 09, 2002 8:45 am
Location: East Coast, USA

Postby Jay » Tue Aug 13, 2002 11:53 am

Try CHMODing the file (and folder, but be careful with regards to security) to 755, and if that doesn't work, 777!
Jay
 

Postby elitecodex » Tue Aug 13, 2002 12:38 pm

I had to chmod the folder to 757. How is that security wise? Is there anyway that can be done in the background so other people that use this script dont have to worry about it?

Thanks for everyone's help!

Will
elitecodex
New php-forum User
New php-forum User
 
Posts: 68
Joined: Tue Jul 09, 2002 8:45 am
Location: East Coast, USA


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.