Friday, October 7, 2022

[SOLVED] chmod and fopen not working

Issue

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.


Solution

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);
}


Answered By - NDM
Answer Checked By - Marie Seifert (WPSolving Admin)