Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

File upload and download in PHP

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...