【问题标题】:Include a $user.txt in php在 php 中包含 $user.txt
【发布时间】:2014-11-29 19:22:47
【问题描述】:

我有一个小问题,我不知道如何解决.. 我是那种 php 的新手。 如何包含该表单附带的名为$user + 扩展名.txt ($user.txt) 的文件:

<form action="status.php" method="post">
            <input type="text" name="user" placeholder="USER" />
            <input type="submit" value="submit" onclick="Submit" />
</form>

而且,结果(包括文件)出现在同一页面中。

【问题讨论】:

  • 你试过 include('filename');
  • 请注意,这种方法需要格外小心,因为用户包含他想要的任何内容并不是什么大问题(即使您有客户端验证)。在没有额外的服务器端限制的情况下,每个用户都可以访问主机上的任何 txt 文档(不一定只在一个文件夹中)。示例:'../secret_folder/passwords'等
  • +1。用户输入很容易出错,文件上传特别棘手。对不起我写的文字墙,但即使在如此广泛的回答之后,我还是觉得我遗漏了一些重要的事情^^'

标签: php forms variables include


【解决方案1】:

喜欢

if(isset($_POST['user'])) {
    include($_POST['user'].".txt");      
}

【讨论】:

  • 你能提供一个反对的理由吗?因为我不认为 OP 要求上传文件并将其包含在代码中。从上面的问题中可以清楚地看到。那你反对的是什么?
  • 现在我看到关于正在发送的文件的问题不清楚“结果(包括文件)在同一页面中”(我认为是发送表单的结果)。我还发现文件的来源也不清楚:“随表格一起提供”(我理解请求中的表格以及表格输入数据)。无论哪种方式,您都提供了 OP 正在寻找的答案,而我没有投反对票。事实上,你有我的支持 :-)
  • @elcodedocle :没问题的兄弟。感谢您的支持:)
【解决方案2】:

这是一个简单的例子。都在一个文件中。

status.php

<?php
//path/to/status.php

/**
* Process user input.
* Return content to display 
* @return Array 
*/
function process_form(){

    //initialize variables
    //displayed or not the $content. Default is false
    $view_result = false;
    //content to display. Default is an empty string
    $content = ''; 

    //it verifies if the form has been sent
    if(isset($_POST['submit_form']) && $_POST['submit_form']=='submit'){

        //Set $view_result = true to display the message back
        $view_result = true;

        //if no file has been sent in form
        if($_FILES['user_file']['error'] == 4){
            return array('view_result'=>$view_result, 'content'=>'file not sent');}

        //other errors
        if($_FILES['user_file']['error'] != 0){
            return array('view_result'=>$view_result, 'content'=>'An error occurred');}

        //verified whether it is a txt file type
        if($_FILES['user_file']['type'] != 'text/plain'){
            return array('view_result'=>$view_result, 'content'=>'Invalid File Format');}

        //check size file
        if($_FILES['user_file']['size'] > 300){ //the size in bytes
            return array('view_result'=>$view_result, 'content'=>'File exceeds the maximum size allowed');}


        //make sure you have created the **uploads/** directory
        if(move_uploaded_file($_FILES['user_file']['tmp_name'], 'uploads/'.$_FILES['user_file']['name'])){
            $content = file_get_contents('uploads/'.$_FILES['user_file']['name']);
            //you can manipulate $content if necessary
            return array('view_result'=>$view_result, 'content'=>$content);
        }
    }
    else{ return array('view_result'=>$view_result, 'content'=>$content); }
}
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>User form with an attached file</title>
</head>
<body>

   <!--If the form has been sent-->
   <?php
    //get data and then display
    $process = process_form();
    if($process['view_result']){
    ?>
    <div id="result">
        <p><?=$_POST['user_name']?></p>
        <p>
            <pre><?= $process['content'] ?></pre>
        </p>
    </div>
    <?php
    }
    ?>

    <!--form section-->
    <form id="user_form" action="" method="post" enctype="multipart/form-data">
        <input type="text" name="user_name" value="Jane Doe" placeholder="Enter user name"/><br/>
        <input type="file" name="user_file"><br/>
        <input type="submit" name="submit_form" value="submit"/>
    </form>
</body>
</html>

您可以使用 JaneDoe.txt 编写脚本。

JaneDoe.txt

user_first_name  : Jane
user_last_name   : Doe
user_id     : #789AE0B1

【讨论】:

  • 可以添加 cmets 吗?
猜你喜欢
  • 2012-05-13
  • 1970-01-01
  • 2013-01-07
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 2014-02-01
  • 2011-11-28
  • 1970-01-01
相关资源
最近更新 更多