0

I am trying to change the file permission on the server, and I can't get it working. I need it for some specific files which are .csv format and which need to be read after the upload. Fopen fails to open the file, I think it is the problem with the permission. Default is 640, and I need it in 777. The error I get is:

chmod() [function.chmod]: No such file or directory
fopen(http://.../file.csv) [function.fopen]: failed to open stream: No such file or directory

File is uploaded and the path in fopen is ok, so this is not a problem. Problem is in the permission. So I tried to change it trough php like this:

$csv_file = $csvpath."import/".$typ."/".$id."/".$flname; // path to the csv file
chmod($csv_file, 0777); 

I tried with relative and absolute path and still not working. Safe mode is off. Any help would be appreciated.

enigmaticus
  • 548
  • 3
  • 8
  • 26

2 Answers2

0

You are trying to fopen a file using http://, hence it throws an error: failed to open stream.

you should use the full server path.

$csvFile = $csvpath."import/".$typ."/".$id."/".$flname;
$filePath = realpath($csvFile); // also checks if the file exists!
if (!$filePath || !is_file($filePath)) {
    die('file not found: ' . $csvFile);
} else {
    fopen($filePath);
}
NDM
  • 6,731
  • 3
  • 39
  • 52
  • it does not die with 'file not found: ...'? and `fopen` still throws the same error (no such file or directory) that's quite impossible :) – NDM Feb 26 '14 at 09:14
  • I've added an additional check for a file, so you're not trying to open a directory. – NDM Feb 26 '14 at 09:16
  • I have it like this: ERROR chmod() [function.chmod]: No such file or directory file not found: /_lib/file/import/2/222/test.csv locally works everything fine, on the server not – enigmaticus Feb 26 '14 at 09:16
  • you should not do a `chmod()` since you uploaded the file using the same user (i.e.: the webserver's user) – NDM Feb 26 '14 at 09:18
  • try and work with absolute paths, compiled using the [`__DIR__`](http://www.php.net/manual/en/language.constants.predefined.php) constants. – NDM Feb 26 '14 at 09:19
0

Is the "allow_url_fopen" Flag in your PHP settings set accordingly?

-> PHP filesystem configuration

Or maybe something else blocks PHP from accessing files via HTTP?

ryu
  • 312
  • 3
  • 6
  • please do not use this, it's a security risk. http://stackoverflow.com/questions/127534/should-i-allow-allow-url-fopen-in-php – NDM Feb 26 '14 at 09:28