Generating an HTML page and save under a folder - php

I would like to make a form that once you insert data into the form, it will automatically create a html format of the data you entered in a html and save it in a folder! How do i go by doing this?

No idea what you mean by "html format of the data", but having PHP write a file is trivial:
$data = "your html format goes here";
$fh = fopen('the_file_you_want_to_write_to.html', 'wb') or die('unable to open file for output');
fwrite($fh, $data);
fclose($fh);
Or even more simplistically:
file_put_contents('the_file_you_want_to_write_to.html', $data);

You could simply place a wysiwyg html javascript editor (such as TinyMCE ) inside a form (textarea field), and store the output in a file with Php.
Some basic WIKI's do only that.

Related

How do I save texts sent by users via GET input in separate text files in php?

I know how to save the sent texts in 1 text file created beforehand. but I need some way to save each text in separate text files which are created automatically
The code below will save the text, which was provided by the user via GET parameter named param, to a new file with unique name in form of hash (something like 6cc50baac239a47e165a320d03c76731.txt).
<?php
$text = $_GET['param'];
file_put_contents(md5(uniqid(rand(), true)) . '.txt', $text);
You can use fopen to create a file, but before using that, read following PHP Manual to find which mode is suitable for your case. Have a look in A list of possible modes for fopen() using mode section:
php.net - fopen function
Be successful.

PHP , sharing a variable valuable across external javascript files

http://www.shedlimited.debrucellc.com
The site allows users to upload an image, via PHP script(ajaxupload.php) which is stored in $uploaded_img , inside of the php script. I want to pass the PATH of my newly stored image to a JS file, and the only way I've found to do it is by writing the value to a text file, which is extreme overkill
file_put_contents("testFile.txt", "");
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $upload_image;
fwrite($fh, $stringData);
$stringData = "\n";
fwrite($fh, $stringData);
fclose($fh);
At the same time, I have an external .js file running which loads my html5 Canvas etc,
I set a global variable sourcer , inside the external .js file, which I'm able to access and update from my index.php by reading the URL from the file which I wrote the url to in my php script.:
jQuery.get('scripts/testFile.txt', function(data) {
sourcer = data;
});
Is there any way that I can pass the URL value to sourcer without having to manually insert it in a text file?
Add a block to your head on your php template.
<script type="text/javascript">
var uploaded_img = '<?php echo json_encode($uploaded_img); ?>';
</script>
The json encode makes sure the variable is properly encoded, even if it is an object or array.
If it is purely an ajax thing. Just return the filename in your ajax response.
So post to your ajax upload, and make your ajax script return a json object with the filename.
So in your upload script, at he bottom:
header('Content-type: application/json');
echo json_encode(array('filename'=>$uploaded_img));
And read the response.filename in javascript.
What about using a session variable to store the uploaded image AND an ajax response to work with ?
I see an advantage to use both : you can use the value directly on the upload page right after the image is uploaded. And for the session var, it will ensure that the server side is always able to get the value if you ever need to access it from another context.

Editing an XML document on the server side

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

Using PHP fWrite to save HTML data causes formatting issues

I created a part of my CMS that allows admins to convert all the text on the page to textareas, so they can edit the content. All the content they change is inside a div, and I submit via jQuery / AJAX the div's content to the PHP controller that processes the data. However, jQuery grabs the HTML just perfectly and sends it to the PHP controller, but the PHP butchers some of the content (removing some tags, every so often).
For example, if I submit
<div class="cmsedit" style="background-color:#EEE">Hello</div>
it will save/write the file with this
div class="cmsedit">Hello</div> .
It removes the opening bracket to the div and the b. It doesnt do it all the time. If I don't include a style tag, it generally leave the code just fine.
Here is the code I'm using on the javascript page.
$('.save_page').click(function(){
updated_content = $('.content_to_be_edited').html();
edit_page = $('#edit_page_name').val();
$.post("<?=site_url('admin/update_page_data') ?>", { content:updated_content,page_name:edit_page });
});
Here is the PHP Controller Page
$page_content = $this->input->post('content');
$page_name = $this->input->post('page_name');
$filename = $_SERVER['DOCUMENT_ROOT']."/application/views/".$page_name.".php";
// SAVE NEW FILE
$file = fopen($filename, 'w') or die("Can't open file");
$filedata = htmlspecialchars_decode($page_content);
fwrite($file, $filedata);
fclose($file);
Anyone have any ideas why the tags are getting all screwed up when the PHP processes the data? Thanks!!
Most likely you have the codeigniter XSS filter active which will destroy your data.
You can disable it and then it should work.

to fetch doc file content

$myFile = "pdf/document_file.doc";
$fh = fopen($myFile, 'r');
$theData = file_get_contents($myFile);
fclose($fh);
echo "<pre>";
echo $theData;
echo "</pre>";
My above script fetches the data from document file but it display some of binary numbers.
I need to display document file content in to html content
For Example
If my document file is a resume. i need to fetch the content and display document data same as selected file in html page
It IS binary data. To view it as HTML you need to convert it first.
Convert .doc to html in php
You're reading binary data. Hence the "numbers". The raw data needs to go through some "filter/render implementation" first.
Plus, when you use file_get_contents() there's no need to fopen() and fclose().

Categories