File I/O

A

Anonymous

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

Code:
     $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
 
Hi Elite,

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

Code:
     $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.
 
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:
$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
 
I think this piece of code has all you need. Can you check if this will work.


Code:
     //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
 
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
 
elitecodex said:
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:
$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:
 
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
 
Try CHMODing the file (and folder, but be careful with regards to security) to 755, and if that doesn't work, 777!
 
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
 
Back
Top