Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP read from uploaded text file?

Tags:

file

php

upload

If I upload a text file via a form, is it possible to output its contents directly from the $_FILES variable rather than saving it onto the server first? I know this is a security risk, but it will only be run on a local machine.

Any advice appreciated.

Thanks.

like image 475
Dan Avatar asked Feb 04 '10 16:02

Dan


People also ask

How do I access my uploaded files in PHP?

In PHP, we can access the actual name of the file which we are uploading by keyword $_FILES[“file”][“name”]. The $_FILES is the by default keyword in PHP to access the details of files that we uploaded. The file refers to the name which is defined in the “index. html” form in the input of the file.

What is Tmp_name in PHP file upload?

tmp_name is the temporary name of the uploaded file which is generated automatically by php, and stored on the temporary folder on the server. name is the original name of the file which is store on the local machine.

What is $_ files in PHP?

PHP $_FILES The global predefined variable $_FILES is an associative array containing items uploaded via HTTP POST method. Uploading a file requires HTTP POST method form with enctype attribute set to multipart/form-data.

Which PHP script variable specifies the path of the file to be uploaded?

$target_file specifies the path of the file to be uploaded.


1 Answers

Doing

file_get_contents($_FILES['uploadedfile']['tmp_name']);  

is valid however you should also check to make sure that the file was uploaded through a form and that no errors occurred during upload:

if ($_FILES['uploadedfile']['error'] == UPLOAD_ERR_OK               //checks for errors       && is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) { //checks that file is uploaded   echo file_get_contents($_FILES['uploadedfile']['tmp_name']);  } 

A helpful link is http://us2.php.net/manual/en/features.file-upload.php

like image 147
MANCHUCK Avatar answered Sep 22 '22 21:09

MANCHUCK