0

I am making an application form for users to apply for jobs and upload their CVs. The php code I made sends the file name to the DB so it is working correctly I guess.

Since I don't see any files after few tests. I tried to change the upload directory so when users upload their CVs it goes to the "uploads" folder in the root.

<?php

if(isset($_POST['uploadCV'])) {
    $file = $_FILES['uploadCV'];
    $fileName = $_FILES['uploadCV']['name'];
    $fileTmpName = $_FILES['uploadCV']['tmp_name'];
    $fileSize = $_FILES['uploadCV']['size'];
    $fileError = $_FILES['uploadCV']['Error'];
    $fileType = $_FILES['uploadCV']['type'];

    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('jpg', 'jpeg', 'png', 'doc', 'docs', 'pdf');
    if(in_array($fileActualExt, $allowed) ) {
        if($fileError === 0) {
            if($fileSize < 50000 ) {
                $fileNameNew = uniqid('', true). '.' . $fileActualExt;
                $fileDestination = 'uploads/' . $fileNameNew;
                move_uploaded_file($fileTmpName, $fileDestination);
                echo "File uploaded!";
            } else {
                echo "File is too large...minimum size is 50MB";
            }
        } else {
            echo "there was a error uploading your file, please ty again!";
        }
    } else {
        echo "You can't upload this file type!";
    }
}

As for the HTML:

<input type="file" name="uploadCV"/>

create-form.php:

<?php
header("Location: http://localhost/Rocket/includes/thankYou.php");

include('connection.php');

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

    $fullName = $_POST['fullName'];
    $email = $_POST['email'];
    $mobile = $_POST['mobile'];
    $dob = $_POST['dob'];
    $degree = $_POST['degree'];
    $expYears = $_POST['expYears'];
    $position = $_POST['jobPosition'];
    $whyHire = $_POST['whyHire'];
    $uploadCV = $_POST['uploadCV'];
    $dateApplied = $_POST['dateApplied'];


    $db = new Database();
    $db->connect();
    $db->insert('users',array('fullName'=>$fullName,'email'=>$email, 'mobile'=>$mobile,
                'dob'=>$dob, 'degree'=>$degree, 'expYears'=>$expYears, 'position'=>$position,
                'whyHire'=>$whyHire, 'uploadCV'=>$uploadCV, 'dateApplied'=>$dateApplied));  // Table name, column names and respective values
    $res = $db->getResult();
    print_r($res);

    if($res) {
        echo "Sent to DB";
        die();
    } else {
        echo "query error";
    }
}

I am expecting the code to upload the selected file by the user to the "uploads" folder but sadly no luck and I don't understand why. but I have a feeling it is the way I wrote the upload directory might be wrong.

Abdulrahman Mushref
  • 1,012
  • 2
  • 18
  • 40
  • The way you are using `uploads\` means it will be trying to put the files in a directory of that name below where the script that is running is. It won't be off of the root. Change it to `/uploads/` and make sure that the web server process has write access to the directory to be able to move the folders there. – Dave Feb 11 '19 at 13:13
  • Possible duplicate of [Move\_uploaded\_file() function is not working](https://stackoverflow.com/questions/18929178/move-uploaded-file-function-is-not-working) – mitkosoft Feb 11 '19 at 13:34
  • I changed it, and made sure the folder is writable by the web server...still nothing – Abdulrahman Mushref Feb 11 '19 at 14:13

2 Answers2

0

Mate, you should change if condition as below:

<?php

if(isset($_FILES['uploadCV'])) {
    $file = $_FILES['uploadCV'];
    $fileName = $_FILES['uploadCV']['name'];
    $fileTmpName = $_FILES['uploadCV']['tmp_name'];
    $fileSize = $_FILES['uploadCV']['size'];
    $fileType = $_FILES['uploadCV']['type'];

    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array('jpg', 'jpeg', 'png', 'doc', 'docs', 'pdf');
    if(in_array($fileActualExt, $allowed) ) {
            if($fileSize < 50000 ) {
                $fileNameNew = uniqid('', true). '.' . $fileActualExt;
                $fileDestination = 'uploads/' . $fileNameNew;
                print_r($fileDestination);
                move_uploaded_file($fileTmpName, $fileDestination);
                echo "File uploaded!";
            } else {
                echo "File is too large...minimum size is 50MB";
            }
    } else {
        echo "You can't upload this file type!";
    }
}
?>

<html>
    <body>
        <form action="" enctype="multipart/form-data" method="post">
            <input type="file" name="uploadCV"/>            
            <input type="submit" name="subkit" value="submit"/>            
        </form>
    </body>
</html>

You should check $_FILES instead of $_POST. Also, you can remove error from $_FILES. Hope it will helps you.

Rohit Mittal
  • 2,064
  • 2
  • 8
  • 18
  • Do you have uploads folder at the same location as this PHP file? if not please create and also set that permission to 777. – Rohit Mittal Feb 11 '19 at 14:16
  • Ok, I did that and it worked. but I see weird thing in the "uploads" folder. Instead of the uploaded file, I got "Desktop.ini" – Abdulrahman Mushref Feb 12 '19 at 10:47
  • debug your code like $moved = move_uploaded_file($fileTmpName, $fileDestination); if( $moved ) { echo "Successfully uploaded"; } else { echo "Not uploaded because of error #".$_FILES["uploadCV"]["error"];}. If there is any issue, it will show that error to you. Also ensure that your php file has 777 permission also. – Rohit Mittal Feb 12 '19 at 11:52
0

Check if the dirrectory is writable

if (is_writable($fileDestination)) {
  // File is writable
}
Pascal Tovohery
  • 888
  • 7
  • 19