0

Here is my code:

<?php
    include ('navbar.php');
?>
<form class = "container m-5" method = "POST" action = "upload.php" encytype = "multipart/form-data">
<input class = "form-control w-25" type = "file" name = "file">
<button class = "btn" type = "submit" name = "Upload">Upload</button>
</form>

<?php
include'connect.php';
$statusMsg = '';

// File upload path
$targetDir = "image";
$fileName = $_FILES["file"]["name"];
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
    // Allow certain file formats
    $allowTypes = array('jpg','png','jpeg','pdf');
    if(in_array($fileType, $allowTypes)){
        // Upload file to server
        if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
            // Insert image file name into database
            $insert = $db->query("INSERT into userid (FileName) VALUES ('".$fileName."'");
            if($insert){
                $statusMsg = "The file ".$fileName. " has been uploaded successfully.";
            }else{
                $statusMsg = "File upload failed, please try again.";
            } 
        }else{
            $statusMsg = "Sorry, there was an error uploading your file.";
        }
    }else{
        $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';
    }
}else{
    $statusMsg = 'Please select a file to upload.'; 
}

// Display status message
echo $statusMsg;
?>

The only message that I received is this

Notice: Undefined index: file in C:\xampp\htdocs\Project\upload.php on line 15 Please select a file to upload.

And nothing shows in my database

Biax20
  • 91
  • 7
  • 1
    You're fetching `$fileName = $_FILES["file"]["name"];` before checking if it exists. Just move those lines inside your `if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"]))` instead. And change `isset($_POST["submit"])` to `isset($_POST["Upload"])` (which is the name of the submit button) – M. Eriksson Apr 12 '21 at 06:28
  • 1
    @MagnusEriksson good catch on the name of the submit button, didn't even see that. – Ivan86 Apr 12 '21 at 06:36

2 Answers2

2

First check if the file is set:

if(isset($_FILES["file"])) {
    include'connect.php';
    $statusMsg = '';

    // File upload path
    $targetDir = "image";
    // ...
}

You seem to also be missing a / here

$targetDir = "image/";   // <-- here
$targetFilePath = $targetDir . $fileName;

Since you are doing this

if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])) {

you can then move the php code that is under the HTML inside the if statement in which case you don't need to check if the file is set before hand since empty() also covers isset():

if(isset($_POST["Upload"]) && !empty($_FILES["file"]["name"])) {
    include'connect.php';
    $statusMsg = '';

    // File upload path
    $targetDir = "image/";
    $fileName = $_FILES["file"]["name"];
    $targetFilePath = $targetDir . $fileName;
    $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

    // Allow certain file formats
    $allowTypes = array('jpg','png','jpeg','pdf');
    
    //...
}

Note: as pointed out in the comments by @MagnusEriksson, your button's name is Upload so that is what you should be checking for in your isset() function.

Ivan86
  • 5,695
  • 2
  • 14
  • 30
  • Notice: Undefined variable: db in C:\xampp\htdocs\Project\upload.php on line 29 Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\Project\upload.php:29 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Project\upload.php on line 29 i got this error i already the $insert outside my if statement – Biax20 Apr 12 '21 at 08:42
1

Try this code.

you have 2 typo in your code.

  1. form attribute should be enctype not encytype.

  2. change submit button name like Upload.

  3. Change the input type file name to some other name like filename

     <form class = "container m-5" method = "POST" action = "upload.php" enctype="multipart/form-data">
     <input class = "form-control w-25" type = "file" name = "filename">
     <button class = "btn" type = "submit" name = "Upload">Upload</button>
     </form>
    
     <?php
     include'connect.php';
     $statusMsg = '';
    
     // File upload path
    
     if(isset($_POST["Upload"]) && !empty($_FILES["filename"]["name"])){
         $targetDir = "image/";
         $fileName = $_FILES["filename"]["name"];
         $targetFilePath = $targetDir . $fileName;
         $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
         // Allow certain file formats
         $allowTypes = array('jpg','png','jpeg','pdf');
         if(in_array($fileType, $allowTypes)){
             // Upload file to server
             if(move_uploaded_file($_FILES["filename"]["tmp_name"], $targetFilePath)){
                 // Insert image file name into database
                 $insert = $db->query("INSERT into userid (FileName) VALUES ('".$fileName."'");
                 if($insert){
                     $statusMsg = "The file ".$fileName. " has been uploaded successfully.";
                 }else{
                     $statusMsg = "File upload failed, please try again.";
                 } 
             }else{
                 $statusMsg = "Sorry, there was an error uploading your file.";
             }
         }else{
             $statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.';
         }
     }else{
         $statusMsg = 'Please select a file to upload.'; 
     }
    
     // Display status message
     echo $statusMsg;
     ?>
    
Vel
  • 9,027
  • 6
  • 34
  • 66