RoR应用程序不与PHP文件对话

时间:2018-03-13 20:11:34

标签: php html ruby-on-rails

我正在使用PSQL和Heroku运行RoR应用程序进行生产。我正在尝试添加一个功能,用户可以上传文件(任何扩展名),然后提交表单以及附加文件以保存在数据库/服务器端。到目前为止,网页上只显示上传按钮。如何将其转换为 浏览 ,用户可以从他/她的PC中选择文件并完整提交表单?

以下是 /app/views/static_pages/project.html.erb

<div class="container"> 
    <div class="container">
        <div class="tab-content">         
            <form action="/add/projects" method="post">
              <div class="field-wrap">
                <label>
                  Project subclass<span class= "req">*</span>
                <select>
                  <option value="--">--</option>
                  <option value= "Schematic">Schematic</option>
                  <option value= "Layout">Layout</option>
                  <option value= "Gerbers">Gerbers</option>
                  <option value= "BOM">BOM</option>
                  <option value= "Assembly">Assembly</option>
                  <option value= "CAD">CAD</option>
                  <option value= "Proto-production">Proto-production</option>
                  <option value= "Pre-production">Pre-production</option>
                  <option value= "Production">Production</option>
                </select>

                </label></div>    

                    <input type="submit" onclick= "upload.php" value="Upload" id="Upload" />
              <!-- ####################### Add to database button #######################-->
              <input type="submit">
            </form>
        </div>
    </div>
</div>

以下是 /app/views/static_pages/upload.php

<?php
$uploadDir = '/uploads/';

$verifyToken = md5('unique_salt' . $_POST['timestamp']);

if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
    $tempFile   = $_FILES['Filedata']['tmp_name'];
    $uploadDir  = $_SERVER['DOCUMENT_ROOT'] . $uploadDir;
    $targetFile = $uploadDir . $_FILES['Filedata']['name'];

    // Save the file
    move_uploaded_file($tempFile, $targetFile);
    echo 1;
}
?>

1 个答案:

答案 0 :(得分:1)

您必须在表单中添加文件输入;

<input name="myFile" type="file">

此外,您必须将表单enctype设置为multipart/form-data