Ankpro Infomatic
Ankpro Infomatic

Reputation: 27

file create error in php

I'm creating the file using the php function but it is not working.I have created the folder and set its permission 0777 but it is not creating the file.

I'm using the following code. plz let me know what is going wrong.

   $cachefile ='/cache/cache.text';
   $file=chmod($cachefile, 777);
   if (file_exists($file)) {
    include($file); 
    } else{
   $fp = fopen('/cache/cache.txt', 'w');    
   fwrite($fp, ob_get_contents());
   fclose($fp);
    ob_end_flush();

for ob_get_contents() function I'm using the ob_start () function. If I create the file manually it is working and showing the buffering data.Plz let me know how can I create the file using the fopen function.

Upvotes: 1

Views: 1234

Answers (1)

kapa
kapa

Reputation: 78671

This line has several issues:

$file=chmod($cachefile, 777);
  1. At that time, the file may not exist, so it may show an error (you cannot do chmod on a file that does not exist).

  2. chmod() will return either true or false. You are trying to use this return value in your code later, saved in the $file variable.

This should work if the cache folder is writable:

   $cachefile ='cache/cache.txt';
   if (file_exists($cachefile)) {
       include($cachefile); 
   } 
   else {
       $fp = fopen($cachefile, 'w');    
       fwrite($fp, ob_get_contents());
       fclose($fp);
       ob_end_flush();
   }

Upvotes: 1

Related Questions