4

As part of a class I've been asked to create a simple file upload system that uploads files in a single button to a folder called ./files. I've been able to do this and I have been able to keep it all contained in one a3.php file with this:

<form action="a3.php" method="post" enctype="multipart/form-data">
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload" name="submitFile">
</form>

Which opens up a file dialog and only requires the user to press two buttons

I use the following function to run the upload function.

if(isset($_POST['submitFile']))
{
    uploadFile();
}

The uploadFile() function simply uses move_uploaded_file to move to file to the ./files folder. Currently without any questions asked.

My issue is that my attempts to make the buttons for the file upload a single one have been stymied. I have attempted to use the following code to resolve the issue:

<form name="frm" action="a3 - Copy.php" method="post" enctype="multipart/form-data"> 
    <input type="file" name="uploadFile" onchange="frm.submit();">
</form>

Which, while it refreshes the page as if an action is being performed, appears to do nothing. I have attempted to look into how exactly .submit() works but haven't gotten any closer to understanding any intricacies it may have while a part of onchange.

I've looked around and I've found code that can upload in a single button, and I've found code that can upload using an internal php function, but none that does both.

Jack Snyder
  • 81
  • 1
  • 8
  • You should change your condition to: `if(isset($_POST['uploadfile'])){ uploadFile();}` – Thanasis1101 Nov 02 '16 at 00:13
  • You got `a3 - Copy.php` in action. Is that correct? – Bart Nov 02 '16 at 00:14
  • Yes, a3 - Copy.php is in action. It's a copy because I have a seperate piece of code that I don't wish to tamper with. – Jack Snyder Nov 02 '16 at 00:16
  • Did you try what I suggested you? – Thanasis1101 Nov 02 '16 at 00:21
  • @ Thanasis Oh, yes, I tried it, though it doesn't seem to have changed anything. Sorry about the late response. – Jack Snyder Nov 02 '16 at 00:21
  • Sorry. It is with a capital f: `if(isset($_POST['uploadFile'])){ uploadFile();}`. Since the page refreshes, it means all the inputs are posted and the name of your input is uploadFile, so it should be set too. This means that the if should be true and execute the uploadFile() function. Try using an echo inside if, to see if it returns true, and tell me. – Thanasis1101 Nov 02 '16 at 00:26
  • @Thanasis I put an echo inside the if statement, but the echo statement didn't seem to fire. I then changed the two part button's name to uploadFile to see if that would work. It did. – Jack Snyder Nov 02 '16 at 00:31
  • This means your problem is solved, right? If not, then something's wrong with your uploadFile() function. – Thanasis1101 Nov 02 '16 at 00:34
  • @Thanasis I need the single button file upload to work, not the two part one. – Jack Snyder Nov 02 '16 at 00:35
  • What I suggested involves one button. I'll post the code as an answer, in order for you to see what I mean. – Thanasis1101 Nov 02 '16 at 00:39

2 Answers2

2

You can try using a button but not displaying it. When the file input is changed, using javascript trigger a click on the hidden button, like in the code below:

<form name="frm" action="a3 - Copy.php" method="post" enctype="multipart/form-data"> 
    <input type="submit" name="submitFile" style="display:none" id="submit">
    <input type="file" name="uploadFile" onchange="document.getElementById('submit').click()">
</form>

<?php
if(isset($_POST['submitFile']))
{
    uploadFile();
}
?>
Thanasis1101
  • 1,614
  • 4
  • 17
  • 28
0

You're basically looking for an ajax upload. You can do this using javascript (on modern browser), and the easiest thing would probably be using a library like jquery. Otherwise, you can do it using an iframe.

Here are a few similar questions with example scripts-

Using HTML5 file uploads with AJAX and jQuery

jQuery Ajax File Upload

Community
  • 1
  • 1
Kayson
  • 425
  • 3
  • 15