简体   繁体   中英

Upload PDF Files PHP

I am trying to upload only PDF files in my form, but nothing happens. It doesn't save the file name in the database and the file does not save into the directory. My PHP code is

if (isset($_POST['submit'])) {

   $folder_path = 'health/';

   $filename = basename($_FILES['healthfile']['name']);
   $newname = $folder_path . $filename;

   if (move_uploaded_file($_FILES['healthfile']['tmp_name'], $newname)) {
      if ($_FILES['healthfile']['type'] != "application/pdf") {

          echo "<p>Class notes must be uploaded in PDF format.</p>";
      } else {
          $filesql = "INSERT INTO tbl_health (link) VALUES ('{$filename}')".die(mysql_error());
          $fileresult = mysql_query($filesql, $con).die(mysql_error());
     }

     if ($fileresult) {
          echo 'Success';
     } else {
          echo 'fail';
     }
  }

}

my form is

<form action="allhealth.php" method="post" enctype="multipart/form-data">

    <label>Upload Your Health Certificate</label>
    <span class="btn btn-default btn-file">
         Browse <input name="healthfile" type="file">
    </span>
    <br/><br/>
    <button type="button" name="submit" class="btn-success">Submit</button>
</form>

Please help!!

Use this

if(isset($_POST["submit"]))
{
    $folder_path = 'health/';

    $filename = basename($_FILES['healthfile']['name']);
    $newname = $folder_path . $filename;

    $FileType = pathinfo($newname,PATHINFO_EXTENSION);

    if($FileType == "pdf")
    {
        if (move_uploaded_file($_FILES['healthfile']['tmp_name'], $newname))
        {

            $filesql = "INSERT INTO tbl_health (link) VALUES('$filename')";
            $fileresult = mysql_query($filesql);

            if (isset($fileresult))
            {
                echo 'File Uploaded';
            } else
            {
                echo 'Something went Wrong';
            }
        }
        else
        {

            echo "<p>Upload Failed.</p>";
        }


    }
    else
    {
        echo "<p>Class notes must be uploaded in PDF format.</p>";
    }

}

Note Before upload file you should check whether its in correct format

and MySQL extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQ L extension should be used.

Use this it's working now I have tested at local.

<?php 
if (isset($_POST['submit'])) {

    $folder_path = 'health/';

    $filename = basename($_FILES['healthfile']['name']);
    $newname = $folder_path . $filename;

    if ($_FILES['healthfile']['type'] == "application/pdf")
    {
        if (move_uploaded_file($_FILES['healthfile']['tmp_name'], $newname))
        {

            $filesql = "INSERT INTO tbl_health (link) VALUES('$filename')";
            $fileresult = mysql_query($filesql);
        }
        else
        {

            echo "<p>Upload Failed.</p>";
        }

        if (isset($fileresult))
        {
            echo 'Success';
        } else
        {
            echo 'fail';
        }
    }
    else
    {
        echo "<p>Class notes must be uploaded in PDF format.</p>";
    }

}
?>
<form action="" method="post" enctype="multipart/form-data">

            <label>Upload Your Health Certificate</label>
                <span class="btn btn-default btn-file">
                    Browse <input name="healthfile" type="file">
                </span>
            <br/><br/>
            <input type="submit" name="submit" class="btn-success" value="submit">
        </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM