jQuery + Ajax solution for "browsing" very large files - javascript

I've got some potentially very large (multiple GB) text files that I'd like to be able to view within a div.
In concept it's not hard... make an AJAX request for a more sensibly sized chunk of file, the AJAX script (PHP would be nice) seeks to an intelligent position within the file, and sends me what I need, which is then rendered vis JS in to an element.
This can't be a new problem, and I'm hoping someone else has already solved it.
Is there such a bit of Javascript (jQuery preferably) with related server scripts already out there somewhere?

I don't know of any library which does this. But it shouldn't be too hard doing yourself. The server side is almost trivial:
<?php
$chunkSize = $_GET['chunksize'];
$offset = $_GET['offset'];
$handle = fopen("text_file.txt", "r");
fseek($handle, $offset);
$chunk = fread($handle, $chunkSize);
fclose($handle);
header('Content-Type: text/plain');
echo $chunk;
?>
This is without any error handling, but that shouldn't be too much hassle as well.
Now, for the client side, there are of course some subtleties, as you will need to have two chunks in the text field at once in order to provide a "smooth" experience, but it should be doable.

Related

"Unexpected token <" when using <?php in a Javascript file

Being new to this, I'm trying to pass a variable from PHP to Javascript.
In my php page, I use a test variable:
$testval = "x";
In my .js file:
var exarr = <?php echo json_encode($testval); ?>;
I've tried several things, but it seems I always get Unexpected token < when I include "
What am I doing wrong ?
Thanks !
In order to use PHP code in any file, the web server has to run that file through the PHP processor. This is configured to happen by default with .php files, but not with .js files.
You could configure your server to process .js files as PHP, but that's generally unwise. At the very least, it creates a lot of unnecessary overhead for those files since most of them won't (or shouldn't) have PHP code.
Without knowing more about the structure of what you're trying to accomplish, it's difficult to advise a "best" approach. Options include (but may not be limited to):
Defining that one var on a PHP page which references the JS file, thereby making it available to the JavaScript code.
Putting the value in a page element somewhere that it can be accessed by JavaScript code, either as a form value or perhaps a data value.
Making an AJAX request to the server to get that value (and other values) after the page has been loaded.
If you have that in a js file (like somefile.js) then PHP isn't going to parse that file by default. In the PHP file that links to that JS you can output a script tag and the var you want like:
echo "<script>var exarr = " . json_encode($testval) . "; </script>";
And make sure your script is linked in after that code;
.js files are not compiled by PHP. The easiest workaround is to put the Javascript in a <script> block within a .php, but you're making one of the most basic of serverside/clientside mistakes and should rethink your entire approach.

The query string seems to affect page loading speed

I'm working on this.
It's hosted on c9.io, a browser based collaborative programming IDE
I have an index.php with a small script:
<?php
$path = ltrim($_SERVER['REQUEST_URI'], '/');
$elements = explode('/', $path);
if(is_numeric($elements[0])){
require_once 'post.php';
}elseif($elements[0]==''){
require_once 'main.php';
}else{
require_once '404.php';
}
?>
When there is nothing in the url (only https://dickbutt-etaoin.c9.io ) it displays the page pretty much instantly but keeps loading for another five seconds during which none of the javascript on the page works
When I have a few numbers (for instance https://dickbutt-etaoin.c9.io/241 ) it's about as fast as it gets.
How can I make both the pages load at a normal speed?
I've tried reversing the order of if statements and it doesn't change a thing. Both the pages are relatively big, contain both php and javascript and a giant css file.
After some prodding in the network tab and googling, I found my answer.
I added this line of code to index.php
header('Connection: close');
Now it does what I wanted it to do. Thank you all for trying to help!

Get the file path to read dynamically from user in PHP

After doing some research, i found a way to read files in php using the following codE:
<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>
Now i want to get the filename(path) through the pop up window which enables user to select the file from his/her file explorer. How to do that behind the html button:
<input type="file" id="loadFile"/>
I'm not 100% sure what you are asking, but I think this answers your question:
After the user has selected a file, they need to upload (POST) it to the server (but submitting the form). For a simple, but good, tutorial on this, check out http://www.tizag.com/phpT/fileupload.php
After the file has been received and saved server-side you can read it with the code you have, or the simpler:
$contents = file_get_contents($filename);
You should be very careful accepting uploads from users and should make sure that you make sure you accept only files you are expecting so you could get your server compromised.

C JAVA PHP TXT ... read instead download

I have website, and I already can highlight code that is written in HTML.
I can store documents on my website, but when I store document (like .php , .c, , .cpp) I can only download them, not opening in next window.
I need some idea how to pass raw code of that file to another page.
I have apache server and I use PHP
I'm not 100% sure what you're trying, but how about posting the file name to a PHP script and reading the file contents using fopen/fread and then just echo it? You should of course make sure that it'll only output files in a specific folder, so this can't be used to hack your site!
<?php
if(isset($_POST['filename']) && is_allowed_to_be_shown($_POST['filename']))
{
$filename = $_POST['filename'];
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
}
?>
Try accessing the pages ending in .php, .c, .cpp, etc via typing in "localhost/[path to file]" in the address bar. That way the files are read by the sever, before being outputted to the browser, meaning that .php (and the like) files will be handled correctly rather than just HTML files.

How Can I scrape the Google Keyword Tool?

I want to extract data from the Google keyword tool - https://adwords.google.com/select/KeywordToolExternal. Their site is in Javascript, the script I've been working on is in PHP. Anyway to do this?
Alternatively, if there is no good solution, I was thinking of downloading the csv file and extracting data from it. Unfortunately, I've never done something where I download a csv file then extracted data from it. Can anyone point me in the right direction?
Scraping the Keyword tool is not easy (and it breaks the terms and conditions).
If you download the CSV file you can read it with php (it is a plain text file). You will have to create the logic to use the data in the file.
this will get you started
$file_handle = fopen("myfile", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
echo $line;
}
fclose($file_handle);

Categories

Resources