-1
error_reporting(E_ALL); 
ini_set('display_errors','1'); 
if (file_exists('/../_config.php')) { 
    $f = fopen('/../_config.php', 'w') or die($php_errormsg);
    fwrite($f, '<?php');
    fclose($f);
}
else {
    echo 'file doesnt exist';
}

Returns: Nothing

I checked the _config.php file, and it's empty. It was supposed to contain <?php.

Absolutely no errors & the code doesn't die at all.

Is it a permission related problem? I am on Windows 7.

Jony Kale
  • 979
  • 3
  • 15
  • 35

2 Answers2

1

use proper path to file:

error_reporting(E_ALL); 
ini_set('display_errors','1'); 

$configFile = __DIR__ . '/../config.php';

echo $configFile, "\n";

if (file_exists($configFile)) { 
    $f = fopen($configFile, 'w') or die('cannot open file');
    fwrite($f, '<?php');
    fclose($f);
}
else {
    echo 'file doesnt exist', "\n";
}

Instead of __DIR__ you can use dirname(__FILE__) but this is only necessary in outdated (dead) PHP versions.

hakre
  • 193,403
  • 52
  • 435
  • 836
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
  • so, `echo $configFile;` to see what file is being checked, maybe add or remove `/../` – Iłya Bursov Oct 15 '13 at 21:22
  • That worked now, thanks. and it did write the file. I need to understand why did it do that, when file_exists returns true with MY path. – Jony Kale Oct 15 '13 at 21:23
  • @Jony Kale: Only you write in there some crazy string path you don't understand doesn't help. Your path is just representing the root directory which does exist. But it's not a file. As the PHP manual page of `file_exists` cleary states, it checks if a directory exists. So you can congratulate yourself that you have just verified that the root directory exists :) - in short: you checked for something that always exists on a unix system. :D – hakre Oct 15 '13 at 21:35
  • @hakre maybe offtopic but according to http://w3techs.com/technologies/details/pl-php/5/all php 5.2 is about 37%, so it is not dead, but of course outdated – Iłya Bursov Oct 15 '13 at 21:40
  • @Ilya Bursov: These numbers are of no interest in the context of a programmer asking a PHP programming question on Stackoverflow, it's only waste if you put dead code into an answer on SO. – hakre Oct 15 '13 at 21:43
0

check your php file permission that its set to 666 and also check permission of directory make sure its set to 777

user889030
  • 4,353
  • 3
  • 48
  • 51