Get the file path to read dynamically from user in PHP - javascript

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.

Related

Read pdf without download and print it with html php

Good evening,
I would like to create a feature for my users that allows them to read a pdf file without downloading or printing it.
They will only be able to read the pdf file.
I tried pdf.js which did not work,
I also tried to convert my pdf to html via a script,
I also tried this php code but the download and print button appears:
<?php
// The location of the PDF file
// on the server
$filename = "/path/to/the/file.pdf";
// Header content type
header("Content-type: application/pdf");
header("Content-Length: " . filesize($filename));
// Send the file to the browser.
readfile($filename);
?>
Do you have any solutions or technologies to suggest to me,
I use PHP, HTML and JS.
Try this one:
<?php
// Store the file name into variable
$file = '/path/to/the/filename.pdf';
$filename = 'filename.pdf';
// Header content type
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
// Read the file
#readfile($file);
?>
PHP uses a standard code to display the pdf file in web browser. The process of displaying pdf involves location of the PDF file on the server and it uses various types of headers to define content composition in form of type, Disposition, Transfer-Encoding etc. PHP passes the PDF files to read it on the browser. Browser either shows it or download it from localhost server then display pdf.
Note: PHP is not actually reading the PDF file. It does not recognize File as pdf. It only passes the PDF file to the browser to be read there. If copy the pdf file inside htdocs folder of XAMPP then it does not need to specify the file path.
The php script you offered only can download the file. if you use php ,you can try the demo ,which is using pdf2text here
or try the method in this link read-pdf-files-with-php

Download file from URL, then save to user computer via dialog

How can I download file (I just know the URL - the file can have x MB) and then save via dialog to user computer? (Dialog = user can choose the directory, where he want to save the file). Just like in screen.
I have absolutely no idea how to do it. I Google it and I found only how to load the file via FILE input, but I do not need that.
File suffix is "*.db", thanks guys, I'm not such a web guy.
You can do that pretty easily.
Make a php file, let's call it index.php. And this should be all the code you need:
<?php
$file = "/your/location/file.db";
header("Content-Type: application/octet-stream");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename='" . basename($file) . "'");
readfile ($file);
?>
I hope I've understood what you actually need. This should do the trick.

Access file from project directory and modify it using Javascript

I have a .json file in the same directory as my html file. I want to access this file and modify it. This modified file data will be used to load the contents of the same html file.
How can I access the file and modify it in Javascript?
Being a newbie in Javascript, any help is appreciated.
Thanks.
HTML5 added a file api for interacting with files on your computer.
This HTML5Rocks article explains how to use it.
this MDN page also explains it
html
<input type="file" id="input" multiple onchange="handleFiles(this.files)">
js
var inputElement = document.getElementById("input");
inputElement.addEventListener("change", handleFiles, false);
function handleFiles() {
var fileList = this.files; /* now you can work with the file list */
}
api link:
FileReader.readAsText()
You cannot do this solely with HTML and javascript. What you want to do requires reading the file on the web server, not from the user's hard drive. Therefore, you must use a back-end language, like PHP. It's actually pretty easy.
If you haven't used PHP before, you can test that you have access to it (almost everyone does except those on Microsoft OS web servers). Just create a new file called test.php and make it look like this:
<?php
echo 'Hello there';
Then, navigate to http://whatever_your_domain_is/test.php
To access your JSON document, you can either do it as the page is loading, as follows:
(1) Rename the page from .html to .php -- all this does is allow PHP to be processed on the page. Otherwise, it is exactly the same as an HTML page.
(2) Add a section like this at the top:
<?php
$filePath = "json_file_name.txt";
$handle = fopen($filePath, "r");
$json = fread($handle, filesize($filePath));
?>
(3) Later, in your javascript code, plop that into a variable, like this:
$(function(){
var imported = "<?php echo $json; ?>";
var json = $.parseJSON(imported); //or, JSON.parse(imported);
}); //END document.ready
Or, you can use AJAX to do it on demand (i.e., triggered by a user event, such as a button click).
Here are some examples that demonstrate how easy AJAX is.
Note that using AJAX will not allow you to avoid the back-end server language (PHP). AJAX code communicates with a back-end (PHP) file, which does the same work as that described in the above section, and sends the result back to the AJAX success function in the javascript. The advantage of AJAX is being able to do it on demand, instead of just when the page is initially rendered.
Other refs:
how to parse json data with jquery / javascript?

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.

jQuery + Ajax solution for "browsing" very large files

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.

Categories

Resources