Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is there any problem with the code below? Because I can click the button without error, but the file will not save into the "public_html/resume/"

$uploadOk = true;

if(isset($_FILES)) {

    $folder_dir = "public_html/resume/";

    $base = basename($_FILES['resume']['name']); 

    $resumeFileType = pathinfo($base, PATHINFO_EXTENSION); 

    $file = uniqid() . "." . $resumeFileType;   

    $filename = $folder_dir .$file;  

    if(file_exists($_FILES['resume']['tmp_name'])) { 

        if($resumeFileType == "pdf")  {

            if($_FILES['resume']['size'] < 500000) { // File size is less than 5MB

                move_uploaded_file($_FILES["resume"]["tmp_name"], $filename);

            } else {
                $_SESSION['uploadError'] = "Wrong Size. Max Size Allowed : 5MB";
                header("Location: edit-profile.php");
                exit();
            }
        } else {
            $_SESSION['uploadError'] = "Wrong Format. Only PDF Allowed";
            header("Location: edit-profile.php");
            exit();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
132 views
Welcome To Ask or Share your Answers For Others

1 Answer

Unfortunately petty much every single line of your PHP code should be improved.

The reason for your error was that /public_hml/ was not an absolute file path so PHP was interpreting it as relative. and looking for <current directory>/public_html/resume/ which is almost certainly invalid.

I have fixed pretty much everything for you, below.

$uploadOk = false; // Always assume false until proven ok.  

if(!empty($_FILES['resume'])) {
   // Check errors    
   if($_FILES['resume']['error'] === 0){

        $folder_dir = $_SERVER['DOCUMENT_ROOT']."/resume/"; // use absolute path.

    // $base = basename($_FILES['resume']['name']); //worthless.
    // $resumeFileType = pathinfo($base, PATHINFO_EXTENSION); 

        $finfo = new finfo();
        $fileMimeType = $finfo->file($_FILES['resume']['tmp_name'], FILEINFO_MIME_TYPE);
        if(strtolower($fileMimeType) !== 'applicaton/pdf'){
               $_SESSION['uploadError'] = "Wrong Format. Only PDF Allowed";
                header("Location: edit-profile.php");
                exit();
        }
        $file = uniqid("",true) . ".PDF";    // make sure unique is unique. 

        $filename = $folder_dir .$file;  

    //if(file_exists($_FILES['resume']['tmp_name'])) {  

    //   if($resumeFileType == "pdf")  {//worthless.

       if($_FILES['resume']['size'] > 500000) { // File size is less than 5MB
            $_SESSION['uploadError'] = "Wrong Size. Max Size Allowed : 5MB";
            header("Location: edit-profile.php");
            exit();
       } 

       move_uploaded_file($_FILES["resume"]["tmp_name"], $filename);
   }
   else {
     //There were file upload errors. Handle here.
    }
}

Source


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...