简体   繁体   English

读取php文件形式的内容

[英]read contents of file forms php

I'm trying to create a local php script that lets the user select a file, then outputs the file contents. 我正在尝试创建一个本地php脚本,该脚本可让用户选择一个文件,然后输出文件内容。 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? 我的问题是,由于我不想将文件上传到任何地方,只需阅读文件的内容(此脚本是个人的,并且在本地运行,因此我不担心安全性),有没有办法从文件中提取文件内容在<input type="file">而不以表格形式输入? I'm new to this stuff and want to make it as simple as possible to just read the file's content. 我是新手,想要使其尽可能简单以仅读取文件的内容。

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 PHP是一种服务器端语言,这意味着您需要“上载”文件才能进行处理,即使Web服务器是本地服务器,浏览器也需要将文件发送到PHP脚本


Here is a guide for file uploads via POST: http://php.net/manual/en/features.file-upload.post-method.php 这是通过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: 为了在变量中获取脚本的内容,您必须在通过表单的post方法上传文件后读取临时文件:

$tmp_file = $_FILES['fileToUpload']['tmp_name'];
$contents = file_get_contents($tmp_file);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM