RoR application not talking to PHP file

ImmaNoob Source

I am running a RoR application using PSQL and Heroku for production. I am trying to add a feature where the user can upload a file (any extension) and then submit the form along with the attached file to be saved in the database/server side. So far, only the upload button appears on the web page. How do I turn this into a Browse where the user can select a file from his/her PC and submit the form in its entirety?

Below is my /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>

Below is my /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;
}
?>
phphtmlruby-on-rails

Answers

answered 3 months ago Foo Bar Zoo #1

You have to add a file input into your form;

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

Also you have to set the form's enctype as multipart/form-data.

comments powered by Disqus