0

I'm trying to create a local php script that lets the user select a file, then outputs the file contents. I don't want to save the file anywhere, just read its contents. I've been reading guides on file input types and this is the examples I'm seeing:

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

My question is, since I don't want to upload the file anywhere, just read it's contents (this script is personal and runs locally so I'm not worried about security), is there a way to extract the file contents from the file selected in the <input type="file"> without putting this in a form? I'm new to this stuff and want to make it as simple as possible to just read the file's content.

MarksCode
  • 8,074
  • 15
  • 64
  • 133
  • 1
    PHP is a server side language, whichs means you'd need to "upload" the file in order to process it, even if web server is local, the browser needs to send the file to the PHP script. – Devon Bessemer Sep 15 '16 at 06:41
  • Thanks for the explanation. In the case, is there a way to upload the file then and save it's contents in a variable for later use? – MarksCode Sep 15 '16 at 06:43
  • Later use as in? Later use in the same execution of that script? Later that day? Week? Year? – Devon Bessemer Sep 15 '16 at 06:46
  • later in same execution, I don't want to upload the file to some server – MarksCode Sep 15 '16 at 06:48
  • You'd have to upload it to the server to get it in the variable in the first place. – Devon Bessemer Sep 15 '16 at 06:50
  • Related (using Javascript): http://stackoverflow.com/questions/750032/reading-file-contents-on-the-client-side-in-javascript-in-various-browsers – Progrock Sep 15 '16 at 08:22

1 Answers1

1

PHP is a server side language, whichs means you'd need to "upload" the file in order to process it, even if web server is local, the browser needs to send the file to the PHP script


Here is a guide for file uploads via POST: http://php.net/manual/en/features.file-upload.post-method.php

In order to get the contents of the script in a variable, you'd have to read the temporary file after the file has been uploaded through a form's post method:

$tmp_file = $_FILES['fileToUpload']['tmp_name'];
$contents = file_get_contents($tmp_file);
Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95