Save iframe content as text file - javascript

How do you save the content of an iframe as a text file? Currently i only know ways to save the content of the webpage itself, but not any child iframes. I'm thinking some javascript, PHP and/or HTML5.

Using PHP and Javascript you can do this easily
Javascript (JQuery)
var data = document.getelementbyid("idofiframe").html();
var url = 'write.php';
$.ajax({url: url,type:"POST",data: data});
PHP
//Log all Proxy Requests
$file = 'file.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
date_default_timezone_set('GMT');
$current .= date('Y-m-d H:i:s'). ",".file_get_contents('php://input')."\n";
// Write the contents back to the file
file_put_contents($file, $current);

Related

How to save div content (img) on server

I am developing a web application where users can crop images.Users must be able to email the URL so others can view the cropped image. This means every cropped image must be stored on the server so that the URL will never die. The portion of my HTML that contains the cropped image is:
<div class="contain" id="myDiv">
<img src="" id="croppedImage">
</div>
The cropping function works fine and it is written in JavaScript. I am using PHP to parse through the DOM, extract the image and save it on the server but it is not working. Any help will be greatly appreciated.
<?php
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$divContent = $xpath->query('//img[id="croppedImage"]');
$my_server_img = $divContent;
$img = imagecreatefromjpeg($my_server_img);
$path = 'images_saved/';
imagejpeg($img, $path);
?>
After reviewing your code and checking out the functions you are using I looked at this part: $xpath->query('//img[id="croppedImage"]'); and compared to http://php.net/manual/en/domxpath.query.php you can see that the function returns a DOMNodeList element (even if nothing is found it will return the object with no children.. You are then taking this object and passing it to imagecreatefromjpeg($filename) that accepts a string as $filename, not an object. Also your xpath selector is wrong, you need to prefix id with # like so: $xpath->query('//img[#id="croppedImage"]');
So here is some code that will grab an <img /> element off a page by it's id and then download the URL of the src attribute and save it. You will want to make sure that the src of the image you are downloading is a jpg file otherwise imagecreatefromjpeg() will fail. You could use CURL or another method to download images (so you can get png images as well) but that is out of the scope of this answer.
$dom = new DOMDocument();
$dom->loadHTML('
<html>
<img src="http://images.freepicturesweb.com/img1/18/02/13.jpg" id="croppedImage2"/>
<img src="http://images.freepicturesweb.com/img1/18/02/14.jpg" id="croppedImage3"/>
</html>
');
$xpath = new DOMXPath($dom);
$imageElements = $xpath->query('//img[#id="croppedImage2"]');
// make sure $imageElements isn't empty
if($imageElements->length) {
// grab first item in list (should only be one)
/** #var DOMElement $imageElement */
$imageElement = $imageElements->item(0);
// get the src attribute off the <img>
$src = $imageElement->getAttribute('src');
// download and create image resource based off $src
$img = imagecreatefromjpeg($src);
// save image with random name to current directory
$filename = __DIR__.'/'.base_convert(sha1(uniqid(mt_rand(), true)), 16, 36).'.jpg';
imagejpeg($img, $filename);
echo "File saved to: ".$filename;
}
Should also be noted that you were assigning a variable to a variable. Things can get messy quick by doing this. If you want a variable to be called something else just set the name when you initialize it instead of assigning it to another variable with the name you want (I'm guessing this is just something you left over after trying some code out, but still worth mentioning).

How to create an Image object with a base-64 source?

I have a remote web page, on which I use CURL and DOMDocument to retrive the src attribute of an image.
I use the following code, mixing JS in a PHP echo
<?php
$toEcho = "";
$toEcho .= // some stuff here...
$toEcho.="
<script>
//I create the object
var img = new Image();
//I set the source with the PHP variable : I get an error because of unexpected token ":"
img.src = $imgsrc;
</script>"
echo $toEcho;
?>
I checked the source, it is valid, and displays properly in my browser(s).
Where could this come from ?
Set the source like this:
img.src = '$imgsrc';

write a file on local disk from web app [duplicate]

I am trying to create and save a file to the root directory of my site, but I don't know where its creating the file as I cannot see any. And, I need the file to be overwritten every time, if possible.
Here is my code:
$content = "some text here";
$fp = fopen("myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
How can I set it to save on the root?
It's creating the file in the same directory as your script. Try this instead.
$content = "some text here";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
If you are running PHP on Apache then you can use the enviroment variable called DOCUMENT_ROOT. This means that the path is dynamic, and can be moved between servers without messing about with the code.
<?php
$fileLocation = getenv("DOCUMENT_ROOT") . "/myfile.txt";
$file = fopen($fileLocation,"w");
$content = "Your text here";
fwrite($file,$content);
fclose($file);
?>
This question has been asked years ago but here is a modern approach using PHP5 or newer versions.
$filename = 'myfile.txt'
if(!file_put_contents($filename, 'Some text here')){
// overwriting the file failed (permission problem maybe), debug or log here
}
If the file doesn't exist in that directory it will be created, otherwise it will be overwritten unless FILE_APPEND flag is set.
file_put_contents is a built in function that has been available since PHP5.
Documentation for file_put_contents
fopen() will open a resource in the same directory as the file executing the command. In other words, if you're just running the file ~/test.php, your script will create ~/myText.txt.
This can get a little confusing if you're using any URL rewriting (such as in an MVC framework) as it will likely create the new file in whatever the directory contains the root index.php file.
Also, you must have correct permissions set and may want to test before writing to the file. The following would help you debug:
$fp = fopen("myText.txt","wb");
if( $fp == false ){
//do debugging or logging here
}else{
fwrite($fp,$content);
fclose($fp);
}

Passing variables in a URL of a php page to JavaScript and to another PHP

Based on the code in this link http://webaudiodemos.appspot.com/AudioRecorder/index.html, I am making changes to send the recorded audio file to server by passing a sessionId via URL. The php page is http://xxxxx/abc.php?sessionId=Sam. PHP versions: PHP 5.4 PHP 5.5.22. I am using the 2nd method from this link:How to pass variables and data from PHP to JavaScript?. The abc.php page has reference to a few JS codes as with the index.html from the link above. abc.php page process the URL values correctly with the following code:
<div id="bottom">
<?php
$faid = $_GET["sessionId"];
echo htmlspecialchars($faid); // tested working
?>
</div>
On the recorder.js JavaScript,I have a function that tries to pass the URL values to another PHP while uploading the audio file to server - The fname is not being passed on ... it seems .. can the xhr.send(blob) will still send the fname?
Recorder.setupDownload = function(blob){
var div = document.getElementById("bottom");
var fname = div.textContent;
var xhr = new XMLHttpRequest();
xhr.open('POST', "./uploads.php?" + fname, true);
xhr.onload = function(e) {};
// Listen to the upload progress.
var progressBar = document.querySelector('progress');
xhr.upload.onprogress = function(e) {
if (e.lengthComputable) {
progressBar.value = (e.loaded / e.total) * 100;
progressBar.textContent = progressBar.value; // Fallback for unsupported browsers.
}
};
xhr.send(blob);
The uploads.php in the server has the following script to receive the value and to create an audio file - but it is not creating the audio file - however, if I fix the file name (eg: "filename123") it writes the audio file - so the issue is in passing the variable name - I am a newbie and I wonder what am I missing?:
<?php
ini_set("display_errors", true);
error_reporting(E_ALL);
if(isset($_GET['fileId']) && !empty($_GET['fileId'])){
$id = $_GET["fileId"];
}
$fp = fopen( $id, 'wb' ); // writes the audio file
fwrite( $fp, $GLOBALS['HTTP_RAW_POST_DATA'] );
fclose( $fp );
?>
Update: It is working!
You didn't give your value a name, so you're passing a value that will appear as DIFFERENT key in every page.
e.g. each of your users will have something like
http://example.com?foo
http://example.com?bar
leading to $_GET['foo'] and $_GET['bar'] in PHP. But since foo/bar are some randomish value representing your session ID, you have no idea what that value will be. So... give it a name:
http://example.com?key=foo
Now you just do $key = $_GET['key'] and can always access your session value, no matter what value it really as - it'll always be assigned to the key.

Convert contents of an HTML file to a string

I'd like to know how to input a .html file to a browser, which then takes the contents of said .html file and converts it into one big string so that I can pass it into a JavaScript function to parse it. As I understand it, HTML5 implemented a file API but I'm not entirely sure if it's capable of doing what I want, or how to really use it for all that matter.
You can use the following :
$filename = "yourhtmlfile.html";
$handle = fopen($filename, "r");
// Retrieve the content of HTML file, and stocks it into $contents var
$contents = fread($handle, filesize($filename));
fclose($handle);

Categories

Resources