REVISED QUESTION:
have an xml document, i wish to change the qty of a book in the xml to increment by 1 on command. is there anyway of updating an XML document through the web.
Many Thanks
You can use codemirror editor, to edit your file and you need to implement saving in php on server side. You can post the content using jquery
$.post('your_script.php', {file: myCodeMirror.getValue()}, function() {
// your file was save
});
and in php
if (isset($_POST['file'])) {
$f = fopen('your.xml', 'w')
fwrite($f, $_POST['file']);
fclose($f);
}
Here is what i was getting at:
http://www.php.net/manual/en/domdocument.save.php
the save method allows you to save it to the webserver, i will use php to open it and then save and close it with this
Related
I use the FPDF library to display online PDF files, with the ouput option « I » (which output the content to browser).
Then, I need to be able to save some of those generated pdfs on my server (like does the ouput option « F »).
In order to do that, I wrote this script:
$pdf_data = file_get_contents('https:/HOST/Folder/file.php');
$path =__DIR__.'/Folder/file.pdf';
file_put_contents( $path, $pdf_data );
It succeeds to create a file with pdf extension to the directory, but :
- I got an error: « Format error : not a pdf or corrupted » when I try to open the "pdf" file,
- The saved file is empty (?).
So, what is wrong?
How can I get the content generated by the php file, and save it as a pdf on my server?
Finaly, I forgot this script and found a new solution that works!
I set up two outputs into my PHP code build with FPDF, one to display the file online and one to save the file on the server.
The content generated by the PHP code is always displayed, and I control the PDF backup with a string on the first output.
So, when I need to backup, I use a Jquery function to execute the PHP code in the background and upload the PDF file (hidden iframe works too).
Below the end of my PHP code:
<?php
...
if (mycondition) {$string='F';}
else {$string ='I';}
...
$pdf->Output(__DIR__.'/../../Folder/file.pdf',$string); // save on the server
$pdf->Output('file.pdf','I'); // display on the browser
?>
Below the jQuery function I use:
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript">
var phpfile = <?php echo json_encode($Filename); ?>; // the php file to upload
function uploadpdf() {
$.get(phpfile);
return false; }
</script>
Hope this will help!
I have link to XML file, which locate on the server. When user click button, he must take dialog to save XML file to local disk. I'm determine link to XML file in "a href", but browser opened this file, not save. If i "save link as.." all OK. Help me please to solve this problem.
Update:
Server - IIS.
XML files create dynamically.
onClick event i send to js link to my XML file, js POST link to php using ajax. How modify my php to open "Save Dialog" to save XML file ?
js:
function funk(url)
{
var ajax = getRequest();
ajax.onreadystatechange = function()
{
if(ajax.readyState == 4)
{
...
}
}
ajax.open("POST", "/do_query.php", true);
ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var data = 'info='+url;
ajax.send(data);
}
php (do_query.php):
<?php
if (isset($_POST['info']))
{
$info = $_POST['info'];
}
?>
The XML file should be served with the HTTP header Content-Disposition: attachment. This tells the browser to download the file instead of opening it. Of course, the Content-Type header must also be set appropriately (application/xml is the generic XML mime type). How you add HTTP headers depends on your server configuration: please edit your question to add some details.
In Apache .htaccess, it's
Header set Content-Disposition attachment
In other servers, it'll vary.
Easiest way would be to make a php file that you link to, and have that php file contain something like
<?php
header('Content-Type: application/xml;');
header('Content-Disposition: attachment; filename=blah.xml;');
readfile('blah.xml');
that should force the file to be seen as a download.
I've got a large form where the user is allowed to input many different fields, and when they're done I need to send the contents of the form to the server, process it, and then spit out a .txt file containing the results of the processing for them to download. Now, I'm all set except for the download part. Setting the headers on the response to the jQuery .post() doesn't seem to work. Is there any other way than doing some sort of iframe trick to make this work (a la JavaScript/jQuery to download file via POST with JSON data)?
Again, I'm sending data to the server, processing it, and then would like to just echo out the result with headers to prompt a download dialog. I don't want to write the result to disk, offer that for download, and then delete the file from the server.
Don't use AJAX. There is no cross-browser way to force the browser to show a save-as dialog in JavaScript for some arbitrary blob of data received from the server via AJAX. If you want the browser to interpret the results of a HTTP POST request (in this case, offering a download dialog) then don't issue the request via AJAX.
If you need to perform some kind of validation via AJAX, you'll have to do a two step process where your validation occurs via AJAX, and then the download is started by redirecting the browser to the URL where the .txt file can be found.
Found this thread while struggling with similar issue. Here's the workaround I ended up using:
$.post('genFile.php', {data : data}, function(url) {
$("body").append("<iframe src='download.php?url="+url+"' style='display: none;'></iframe>");
});
genFile.php creates the file in staging location using a randomly generated string for filename.
download.php reads the generated file, sets the MIME type and disposition (allowing to prompt using a predefined name instead of the random string in the actual filename), returns the file content and cleans up by deleting the source file.
[edit] might as well share the PHP code...
download.php:
<?php
$fname = "/tmp/".$_GET['url'];
header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename="plan.xml"');
echo file_get_contents($fname);
unlink ($fname);
?>
genFile.php:
<?php
$length = 12;
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = substr( str_shuffle( $chars ), 0, $length ).'.xml';
$fh = fopen(('tmp/'.$str), 'w') or die("can't open file");
fwrite($fh,$_POST["data"]);
fclose($fh);
echo $str;
?>
Rather than using jQuery's .post(), you should just do a normal POST by submitting the form, and have the server respond with appropriate Content-Encoding and MIME-type headers. You can't trigger a download through post() because jQuery encapsulates the returned data.
One thing I see in use rather frequently, though, is this:
$.post('generateFile.php', function(data) {
// generateFile builds data and stores it in a
// temporary location on the server, and returns
// the URL to the requester.
// For example, http://mysite.com/getFile.php?id=12345
// Open a new window to the returned URL which
// should prompt a download, assuming the server
// is sending the correct headers:
window.open(data);
});
In my site, I have implemented the following functionality: If the user clicks on a button it triggers a PHP function which generates an XML file (that PHP function is called by AJAX). Everything is working well, but here's one thing I want to change: I don't want an .XML file to be created on the server machine; instead, I want the user to be prompted to save the .XML file locally. How do I do that? My PHP script currently looks like this:
$xml = new DOMDocument("1.0", "UTF-8");
$rootElement = $xml->appendChild($xml->createElement("SomeNodeName"));
...
// the following doesn't really work - xml doesn't get formatted :)
$xml->formatOutput = true;
// this creates the actual file and places it on server. that's not what i need
$xml->save("MyXMLfile.xml");
Thanks for all the help.
Every web content has a header, so if you specify the header for an xml file (through the use of the header() function with the appropriate code it'll work.
This would mean doing something like this:
<?php
header('Content-type: text/xml');
// set the filename
header('Content-Disposition: attachment; filename="pwet.xml"');
// echo the content here
echo $xml; // simply like this, maybe?
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.