Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleXML import into PHP. Do I need to close file?

Tags:

php

xml

simplexml

I'm importing data into a php array from an xml file using SimpleXML.

I'm using the simplexml_load_file function, but once i've got the data I need, do I need to close the file or similar to clear the memory?

Thanks,

James

like image 995
Ben Iskander Avatar asked Feb 18 '23 09:02

Ben Iskander


1 Answers

No you don't need to do anything with the file. simplexml_load_file() will close the file internally after it has read the content.

If you take a look at the source code for simplexml_load_file, you'll see that it is calling the C function xmlReadFile() form xmllib2, which in turn will close the file after reading.

PHP_FUNCTION(simplexml_load_file)
{
    php_sxe_object *sxe;
    char           *filename;
    int             filename_len;
    xmlDocPtr       docp;
    char           *ns = NULL;
    int             ns_len = 0;
    long            options = 0;
    zend_class_entry *ce= sxe_class_entry;
    zend_bool       isprefix = 0;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|C!lsb", &filename, &filename_len, &ce, &options, &ns, &ns_len, &isprefix) == FAILURE) {
        return;
    }

    docp = xmlReadFile(filename, NULL, options);  <--- reading the file
like image 175
MrCode Avatar answered Feb 27 '23 01:02

MrCode