-1

I am a beginner in PHP coding. I try to create a page using $_FILES under XAMPP localhost environment. PHP is 7.2.7 version. The problem is that, when I click the submit button to upload the file, nothing happens. (I use a "if...else" loop to see if all conditions not happen, and the result is nothing happen.)

HTML:

<form action="upload.php" method="POST" enctype="multipart/form-data">
    <label for="file">File upload:</label> 
    <input type="file" name="User Name">
    <br><br>
    <input type="submit">
    <br><br> 
</form>  

PHP:

if(isset($_POST['submit'])){
  if ($_FILES["file"]["error"] > 0)
  {
    echo "Error: " . $_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 "Stored in: " . $_FILES["file"] ["tmp_name"];
  }
}
else {
  echo "nothing";
}
  • 3
    You IF condition is wrong... you did not defind an input name "submit" ...isset($_POST['submit']), I would also set an input name without spaces... – Guy Louzon Sep 20 '18 at 11:37
  • 2
    Also $_FILES["file"] doesn't exist. Your input is with name "User Name", not "file". – AnTrakS Sep 20 '18 at 11:38
  • 1
    @GuyL Thanks a lot :) – Oceane Tan Sep 20 '18 at 13:17
  • @D. Dimitrov Thank you. I google for the answer and tried some solution in StackOverflow with the similar questions but didn't realize it is my coding problem. It works after I change input "User Name" to "file" and add name to submit button, tq again. – Oceane Tan Sep 20 '18 at 13:18
  • you're welcome :) – Guy Louzon Sep 20 '18 at 13:25

2 Answers2

0

First , you din't define a name for the input type submit

To upload a file into the database or in the directory Try this:

if isset($_POST['submit'])

{

$filename=$_FILES['fileupload']['name'];

$filetmp=$_FILES['fileupload']['tmp_name'];

$filesize=$_FILES['fileupload']['size'];

$file_basename=basename($_FILES['fileupload']['name']).date("dmY")."_".time();

$dir="uploads/";

$final_dir=$dir."$file_basename";

//echo "$final_dir";

move_uploaded_file($filetmp, $final_dir);

}

//Insertion Query Goes here........ 
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
ruchika vasu
  • 101
  • 1
  • 1
  • 9
  • I tried this solution just now but 2 errors occurred as below: Warning: move_uploaded_file(tmp/.my.PNG): failed to open stream: No such file or directory in D:\XAMPP\htdocs\file.php on line 31 Warning: move_uploaded_file(): Unable to move 'D:\XAMPP\tmp\phpEC95.tmp' to 'tmp/.my.PNG' in D:\XAMPP\htdocs\file.php on line 31 – Oceane Tan Sep 20 '18 at 14:04
  • It works after I changed move_uploaded_file($_FILES["file"]["tmp_name"],$final_dir); to move_uploaded_file($_FILES["file"]["tmp_name"],$dir); Thanks for help ;D – Oceane Tan Sep 20 '18 at 14:31
  • if it solved your problem , please mark it as accepted solution :D – ruchika vasu Sep 21 '18 at 05:47
-1

Your form is wrong. $_POST['submit'] refers to a form control with the name "submit". This can be easily fixed by changing your form to the following:

<form action="upload.php" method="POST" enctype="multipart/form-data">
    <label for="file">File upload:</label> 
    <input type="file" name="User Name">
    <br><br>
    <input name="submit" type="submit">
    <br><br> 
</form>

Note that I have changed <input type="submit"> to <input name="submit" type="submit">

I hope this helps :)

Harvey Fletcher
  • 1,167
  • 1
  • 9
  • 22
  • Thanks ;) I have another mistake in input's name. The name is not correct. Anyway, the problem is solved perfectly, thank you for your help ;D – Oceane Tan Sep 20 '18 at 14:07
  • If this was the solution that fixed your problem, please don't forget to mark as the accepted solution, it's much appreciated. :) – Harvey Fletcher Sep 20 '18 at 15:43