How to check if directory is correct for uploading files in php?

44
February 12, 2019, at 10:10 AM

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.

Answer 1

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.

Answer 2

Check if the dirrectory is writable

if (is_writable($fileDestination)) {
  // File is writable
}
READ ALSO
Creating a Pivot (crosstab) query for a golf league [on hold]

Creating a Pivot (crosstab) query for a golf league [on hold]

I’m trying to create a pivot query (Cross Tab)for a golf league with about 20 playersIn my database I place the playerID, Name, Date, Round#(1,2,3, etc) and score

36
Access denied for user root@localhost on Ubuntu and OS X

Access denied for user root@localhost on Ubuntu and OS X

I'm developing a web application and it is working just fine in Windows, but now I'm trying to run it in Ubuntu and in a Macbook and it's giving me a Access denied error

30
Question marks returned instead of Arabic characters ONLY when xampp servers are launched from within program

Question marks returned instead of Arabic characters ONLY when xampp servers are launched from within program

As the title says, question marks are returned only when I run mysqldexe and httpd

49
MySQL - sub query on the same table [duplicate]

MySQL - sub query on the same table [duplicate]

This question already has an answer here:

33