Home > Enterprise >  Upload a pdf file in core php
Upload a pdf file in core php

Time:05-15

//=============== PDF File Upload=====================
if (isset($_FILES["pdfile"])) {
        $filename = $_FILES['pdfile']['name'];
        $dir = "../pdfs/students/".$filename;
        $tmp_name = $_FILES['pdfile']['tmp_name'];
        $file1 = explode('.',$filename);
        $ext = $file1[1];
        $allow = array('pdf');
        if(in_array($ext, $allow))
            move_uploaded_file($filename, $dir);
    }
    else
        die("There is no file to upload.");

//=============== End Pdf File Upload=====================

//=============== Image File Upload=====================
if(isset($_FILES['profile']['tmp_name'])){
        $dir = "../images/students/".$id.".jpg";
        $file = $_FILES['profile']['tmp_name'];
        if(move_uploaded_file($file, $dir)){
            echo "Image Uploaded Successfully";
            $pic = mysqli_query($con, "update students set profile ='$id.jpg' where id = $id ")or die(mysqli_error($con));
        }
        else{
            echo "Error Occured";
        }
    }
//=============== End Image File Upload=====================

PDF File didn't get upload in directory. I don't know why and same code for profile picture uploading works fine. Help me with this guys.

CodePudding user response:

Sorry I didn't see at first time at my code. the error is of $tmp_name at move_upload_file($filename, $dir). Replace $filename to $tmp_name and it works fine.

//=============== PDF File Upload=====================
if (isset($_FILES["pdfile"])) {
        $filename = $_FILES['pdfile']['name'];
        $dir = "../pdfs/students/".$filename;
        $tmp_name = $_FILES['pdfile']['tmp_name'];
        $file1 = explode('.',$filename);
        $ext = $file1[1];
        $allow = array('pdf');
        if(in_array($ext, $allow))
            move_uploaded_file($filename, $dir);
    }
    else
        die("There is no file to upload.");

//=============== End Pdf File Upload=====================

CodePudding user response:

Change this line

move_uploaded_file($filename, $dir);

To this

move_uploaded_file($tmp_name, $dir);

CodePudding user response:

if (isset($_FILES["pdfile"])) {
    $filename = $_FILES['pdfile']['name'];
    $dir = "../pdfs/students/".$filename;
    $tmp_name = $_FILES['pdfile']['tmp_name'];
    $file1 = explode('.',$filename);
    $ext = $file1[1];
    $allow = array('pdf');
    if(in_array($ext, $allow))
        move_uploaded_file($filename, $dir);
}

move_uploaded_file should be:

move_uploaded_file($tmp_name, $dir);

more tips:

<?php
if (isset($_FILES["pdfile"])) {
  $filename = $_FILES['pdfile']['name'];
  $tmp_name = $_FILES['pdfile']['tmp_name'];
  $dir      = "../pdfs/students/" . $filename;
  $file1    = explode('.',$filename);
  // use end() to get last
  // for example: filename.jpg.pdf
  $ext = end($file1);
  $allow = array('pdf');
  if(in_array($ext, $allow))
      move_uploaded_file($tmp_name, $dir);
}
  • Related