PHP fileopen

Posted on 2006-04-24
Medium Priority
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 = "http://www.somedomain.com/xml/atext.xml";

// 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 http://www.somedomain.com/xml/atext.xml (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

ID: 16525560

try instead
$file_contents = file_get_contents($xml_file);

Author Comment

ID: 16525728
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");

Accepted Solution

tdterry earned 300 total points
ID: 16527538
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

ID: 16530297
I agree that the file_get_contents() will work, as previously stated.

Expert Comment

ID: 16531210
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

ID: 16533538
i have the same comment as above see my comments above.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

839 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