How to download xml file through in php? - php

I created xml file in php. This file is successfully saved in my server directory.
Now, I want to download this file only through php, not javascript.
How can I save this file?

Try file_get_contents() and file_put_contents().
<?PHP
$xml = file_get_contents("http://yoursite.com/yourxml.xml"); // your file is in the string "$xml" now.
file_put_contents("/path/to/file/yourxml.xml", $xml); // now your xml file is saved.
?>

Related

PHP: save a file (generated by aspx) to server

I have an url provided by a wholesaler. Url generates xml file which I need to save on my server.
I use PHP - file_get_contents and file_put_contents to do that:
$savepath = "path_to_my_server_folder";
$xmlurl = "http://usistema.eurodigital.lt/newxml/xmlfile.aspx?xml=labas&code=052048048048051057049050048049052";
file_put_contents($savepath.'eurodigital.xml', file_get_contents($xmlurl));
File is generated on my server, but its content is empty. I have no problems with other xml files if I provide direct xml url, but in this situation file is generated by aspx file dynamically. Xml url is actual url I use. When I open xmlurl in browser, xml file gets saved to device.
Can you help me with moving xml to my server? What function should I use? Is it even possible to do that using PHP 5? "allow_url_fopen" is ON.
Thank you in advance!

Save xml api as and excel file in php

I have this code which save xml api as an xlsx. But When I save it, i can not open on computer. But txt doc and other formats support it. How can I save it as an xlsx file successfully.
<?php
$xlsx = file_get_contents("http://cbu.uz/uzc/arkhiv-kursov-valyut/xml/");
file_put_contents("currency/kurs.xlsx", $xlsx);
?>

read contents of zipped files through file_get_contents in codeigniter in .srt format and upload to server

I am trying to read contents of zipped file as
$subda=file_get_contents('http://www.yifysubtitles.com/subtitle/thewilbyconspiracy1975dvdripxvid-english-128250.zip');
And trying to upload at my online server as below
$this->load->library('zip');
$data = $subda;
$name = 'myfile.srt';
$this->zip->add_data($name, $data);
$this->zip->archive('assets/subtitles/myzipfile.zip');
But when I check this uploaded file at my server it does not compressed properly.
it does not contain any data.
when I echo $subda it give results like.
Where I am wrong...
through file_get_contents I am already getting contents of zip file.
You cannot do:
$subda=file_get_contents('http://www.yifysubtitles.com/subtitle/thewilbyconspiracy1975dvdripxvid-english-128250.zip');
This will just load the ZIP content into the string rather than uncompressed.
See this on how you can read using a lib in php:
Best way to read zip file in PHP

PHP Parse a PHP file echoing XML data

I have a php file which generates and echo's XML data.
So basically it shows a XML but it's a PHP file.
I need to read and parse this data.
I've seen this: simplexml_load_file('some.xml'); but in this case I cannot do this as I've got the xml as a php file.
How can I do this?
Just the same way, the file extension doesn't matters (.xml or .php) what it really matters is the actual contents of the file, so if your file have a .php extension but its contents are valid xml then you should have no problem:
simplexml_load_file('somepage.php'); //this is fine
Set mime type to xml header('Content-Type:text/xml');

PHP object-oriented concept in file handling

I want to read content from a text file and download it into a pdf file using PHP object orientation. How I read content from a file, is it same as simple PHP?
The simplest way to get a file’s contents is file_get_contents:
$contents = file_get_contents('filename');
Do you want to know how to get the contents of a file using object-oriented notation, or how to store the contents as an object, or how to move the contents into a pdf?
Assuming the last 2:
//First set the file path and get the contents of the file:
$textfile->path = "path/to/file.txt";
$textfile->contents = file_get_contents($textfile->path);
//Next create the pdf, both as a handler and as a file on disk:
$pdf = PDF_new();
PDF_begin_document($pdf, "file.pdf", "");
//Then put the text file contents into the pdf:
PDF_show($pdf, $textfile->contents);
//Finally, save and close the pdf:
pdf_save($pdf);
pdf_close($pdf);
If you want the script to return the pdf from a request and not save it to the server, simply change "file.pdf" to "" and use the header() function to set the filename.

Categories