PHP fileopen

Posted on 2006-04-24
Last Modified: 2008-01-09

i am trying to read an xml file, when i load a local file which is in the same directory it works fine... but when i try to load a xml file from another domain it gives me an error...

$xml_file = "";

// Opens the file or gives an error message
$fp = fopen($xml_file, "r") or die("Could not open file");

// Reads the file or gives an error message
$data = fread($fp, filesize($xml_file)) or die("Could not read file");

i get the following error...

Warning: filesize(): Stat failed for (errno=2 - No such file or directory) in /test.php on line 78

Warning: fread(): Length parameter must be greater than 0. in /test.php on line 78
Could not read file

can someone please help...

Question by:iam_david_lee
    LVL 11

    Expert Comment


    try instead
    $file_contents = file_get_contents($xml_file);

    Author Comment

    thanks for the reply... but still same error. i think its its something to do with this line, it doesnt like the filesize or something...

    $data = fread($fp, filesize($xml_file)) or die("Could not read file");
    LVL 5

    Accepted Solution

    With file_get_contents(), you don't need fopen/fread/filesize.  The warnings are because PHP doesn't support filesize and fread on remote files.  All you need to call is:

    $data = file_get_contents($xml_file);
    // entire file contents are stored in $data
    LVL 5

    Expert Comment

    I agree that the file_get_contents() will work, as previously stated.
    LVL 9

    Expert Comment

    when you using fopen(), try to check to make sure that allow_url_fopen is enabled. If it is switched off, PHP will emit a warning and the fopen call will fail.

    LVL 11

    Expert Comment

    i have the same comment as above see my comments above.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now