hey guys, so i have a website hosted via hostica.com, and php is enabled, but for some reason my fopen command isnt creating a file!

im trying to make a ratings system, and its not working. here's the code on one of the pages:

(or part of it at least)

$filename = "../".$g."s/".$c."/".$n.".txt";
   $file = @file($filename);
   $file[] = "$rating\n";
   $rates = count($file);
   $total = array_sum($file);

   $fp = fopen($filename,'w');
   foreach ($file as $line) { fwrite($fp, $line); }
   fclose ($fp);

   echo "<br>$rates\n votes";
   echo '<br><b>Average rating</b> - <font size="+"1>'.number_format($total/$rates, 2)."</font>";

    Perhaps no rights to write in the folder you want to use?

      See if it's writable:

      echo var_dump(is_writable($filename));

        What error message are you getting? Have you tried looking on the PHP error log?

        You do have error_reporting(E_ALL) on, right?

        Did you consider making the script test the result from fopen() ?

        (I never do this, because my error handler normally terminates when any error happens)

        Mark

          I like to do something along the lines of this (in theory):

          <?php
          $fp = fopen($filename,'w');
          
          if(!$fp)
          {
          	echo 'Could not create a file pointer.';
          	exit;
          	{
          else {
          	if(!is_writable($file))
          	{
          		echo 'Can not write to file [' . $file . ']';
          		exit;
          	}
          	else {
             		foreach ($file as $line)
             		{
             			$write = fwrite($fp, $line);
          		}
          
          	if ($write !== FALSE)  // edited per BG
          	{
          		echo 'success. the file was written...';
          	}
          }
          $close = fclose ($fp);
          }
          ?>
          
            if ($write == true) 

            Unless it only writes 1 character, that will never work.

            fwrite() returns the number of bytes written, or FALSE on error.

            That would better be written as:

            if ($write !== FALSE)

              < humbled > DOH! Yes, thanks, Brad! < / humbled >

                MarkR wrote:

                What error message are you getting? Have you tried looking on the PHP error log?

                You do have error_reporting(E_ALL) on, right?

                Did you consider making the script test the result from fopen() ?

                (I never do this, because my error handler normally terminates when any error happens)

                Mark

                there's no error message at all, it just ends up not writing the file.

                i guess ill try doing what brad and rodney told me to do, see if that works out

                thanks for the help so far!

                  a month later

                  Run phpinfo() and you might be able to figure out the problem. The host may have disabled fopen and other functions.

                  <?php
                  phpinfo();
                  ?>
                  

                    so, it says allow_url_fopen is off, is that what it would be? and is there any way to change that?

                      3 years later

                      I realize this reply will not be of much use to dudesweetradica at this point, since the post is dated 2006 and I'm replying in 2009. However, in case anyone else surfs by, there is a way to change your settings on a hosted site, or at least it should work.

                      Simply create a text file named php.ini and enter:

                      allow_url_fopen=On

                      on the first line. You can space down and add as many PHP variables as you need. You will also need to upload a copy of the php.ini file to every directory you want to use the new values.

                      As long as your site host has not disabled this ability by banning files with an .ini extension, your host's installation of PHP will first search in the directory for a php.ini file, and if it finds it, the settings specified inside will take precedence to those it retrieves from the host's php.ini file. You can verify that the settings are enabled with by creating a php file and including:

                      <?
                      phpinfo();
                      ?>

                      inside, dropping it in the directory you want to check and accessing it via your web browser.

                        mrrena wrote:

                        Simply create a text file named php.ini

                        mrrena wrote:

                        You will also need to upload a copy of the php.ini file to every directory you want to use the new values.

                        That's not true, for a couple of reasons - a) not all hosts have PHP configured to look for an additional php.ini file, and b) those that do almost always have the PHP parser search for the file in a specific place regardless of where the PHP script is running at the moment (this location is generally outside of the web root, unless your host isn't very intelligent).

                          You mean, "that is not always true" or even "that is not often true." 😉

                          However, it does happen to be the case with 1&1 (allegedly the world's largest shared hosting company), and in any case, while I should have been more explicit that it is not guaranteed to work, it is certainly worth a try.

                            3 months later
                            Rodney H.;10706344 wrote:

                            I like to do something along the lines of this (in theory):

                            <?php
                            $fp = fopen($filename,'w');
                            
                            if(!$fp)
                            {
                            	echo 'Could not create a file pointer.';
                            	exit;
                            	{
                            else {
                            	if(!is_writable($file))
                            	{
                            		echo 'Can not write to file [' . $file . ']';
                            		exit;
                            	}
                            	else {
                               		foreach ($file as $line)
                               		{
                               			$write = fwrite($fp, $line);
                            		}
                            
                            	if ($write !== FALSE)  // edited per BG
                            	{
                            		echo 'success. the file was written...';
                            	}
                            }
                            $close = fclose ($fp);
                            }
                            ?>
                            

                            The code has a curly bracket that is backwards. It's right after the first 'if' statement. Here is the corrected code.

                            <?php
                            $fp = fopen($filename,'w');
                            
                            if(!$fp)
                            {
                            	echo 'Could not create a file pointer.';
                            	exit;
                            	}
                            else {
                            	if(!is_writable($file))
                            	{
                            		echo 'Can not write to file [' . $file . ']';
                            		exit;
                            	}
                            	else {
                               		foreach ($file as $line)
                               		{
                               			$write = fwrite($fp, $line);
                            		}
                            
                            	if ($write !== FALSE)  // edited per BG
                            	{
                            		echo 'success. the file was written...';
                            	}
                            }
                            $close = fclose ($fp);
                            }
                            ?>
                            
                              Write a Reply...