Home > other >  PHP When use fopen to read files
PHP When use fopen to read files

Time:02-10

I did not find a clear solution for me so I'am asking it here: When do I use fopen to read files with php?

I've a bunch of xml files stored on my server. Another application can also access these files (read and write). I want to read (only) these files. Until now, I am using simple_xml_load_file for reading the xml files. But I was asking myself if it is neccessary to use fopen and fclose to read the file.

So this is what I am using by now:

$filename = 'PATH_TO_FILE/myXmlFile.xml';

if (file_exists($filename)) {
    $xml = simplexml_load_file($filename);
}

This is the solution I though of:

$filename = 'PATH_TO_FILE/myXmlFile.xml';

if (file_exists($filename)) {
    $file = fopen($filename, 'r');
    $data = fread($file, filesize($filename));
    $xml = simplexml_load_string($data);
    fclose($file);
}

So it there any difference between these two snippets? When should I use fopen and fclose for reading files or is this neccessary when writing files only?

CodePudding user response:

  •  Tags:  
  • Related