1

So I run a small website for a school teacher, and I have been trying to implement some php script so the teacher can upload the files on the spot, instead of having to come to me to manually upload them to her server.

I get the file itself uploaded, but when I try to use a textbox to get the teacher to input a name, the text inside the field refuses to assign itself to a variable. I used a dump method for all $_POST[] values and it appears in the array.

Here's my code for the form.

<body>
<div id="page">
<div id="header"></div>
<div id="cssmenu">
<ul>
<li><a href="../../index-2.html"><span>Home</span></a></li>
<li><a href="../../tline.html"><span>Timeline</span></a></li>
<li><a href="../../notes-apus.html"><span>Notes</span></a></li>
<li><a href="../../links.html"><span>Links</span></a></li>
<!--   <li class='last'><a href='videos.html'><span>Videos</span></a></li> --></ul>
</div>
<hr />
<div id="content" class="narrowcolumn">
<div class="post" id="div">
<h2 class="style8">File Upload</h2>
<div class="entry" align="center">
      <form action="upload_file.php" method="post" enctype="multipart/form-data">
         <label for="file">File:</label>
         <input type="file" name="file" id="file"><br>
         <label for="text">Entry Name:</label>
         <input type="text" name="Filed" value="File Name" id="FileName">
         <input type="submit" name="submit" value="Submit">
      </form>
</div>
<p class="postmetadata">&nbsp;</p>
</div>
</div>
<hr />
<div id="footer">
<p><br />Last updated
<script type="text/javascript">// <![CDATA[
document.write(document.lastModified);
// ]]></script>
</p>
</div>
</body>

And here's the code that uploads the file.

<body>
<div id="page">
<div id="header"></div>
<div id="cssmenu">
<ul>
<li><a href="../../index-2.html"><span>Home</span></a></li>
<li><a href="../../tline.html"><span>Timeline</span></a></li>
<li><a href="../../notes-apus.html"><span>Notes</span></a></li>
<li><a href="../../links.html"><span>Links</span></a></li>
<!--   <li class='last'><a href='videos.html'><span>Videos</span></a></li> --></ul>
</div>
<hr />
<div id="content" class="narrowcolumn">
<div class="post" id="div">
<h2 class="style8">File Upload</h2>
<div class="entry" align="center">
      <?php
$filenamed = $_GET["Filed"];
$temp = explode(".", $_FILES["file"]["name"]);

  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("uploadedFiles/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../../uploadedFiles/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "uploadedFiles/" . $_FILES["file"]["name"];
      file_put_contents("./php_registries/ScullyFilenameRegistry.txt", $filenamed, FILE_APPEND);
      }
   }

?>
</div>
<p class="postmetadata">&nbsp;</p>
</div>
</div>
<hr />
<div id="footer">
<p><br />Last updated
<script type="text/javascript">// <![CDATA[
document.write(document.lastModified);
// ]]></script>
</p>
</div>
</body>

I just don't understand what's wrong, so I would really enjoy some help on this one.

2 Answers2

3

You are getting the filename from $_GET, not $_POST.

$filenamed = $_GET["Filed"];

Should be:

$filenamed = $_POST["Filed"];
Jonathan Kuhn
  • 14,619
  • 2
  • 28
  • 41
  • Worked! Thanks. I'll look up the differences of $_POST[] and $_GET[] so I don't make the same mistake. – user3516909 Apr 09 '14 at 20:20
  • @user3516909: Here is an explanation of the difference between $_GET and $_POST. http://stackoverflow.com/questions/3477333/what-is-the-difference-between-post-and-get There are also several websites that describe the differences probably better. – Jonathan Kuhn Apr 09 '14 at 20:30
1

You are making a POST request using the form i.e you set method='post'.

But in the php code you are using $_GET['Filed'] to get the filename, instead use $_POST['Filed'].

Sanketh
  • 1,129
  • 9
  • 18