PHP fileopen

hi

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...


thanks
iam_david_leeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

star_trekCommented:

try instead
$file_contents = file_get_contents($xml_file);
0
iam_david_leeAuthor Commented:
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");
0
tdterryCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

mnb93Commented:
I agree that the file_get_contents() will work, as previously stated.
0
blue_hunterTechnical ConsultantCommented:
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.


0
star_trekCommented:
i have the same comment as above see my comments above.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.