'PHP files not being uploaded

I am trying to upload files to my PHP web page, it works on my local machine but it doesn't work on my ec2 server. The directory exists and $_FILES['userfile']['error'] returns 0 which means there is no error, but my file isn't being moved to the specified directory.

if (isset($_POST['upload'])) {
    $filename = $_FILES['userfile']['name'];
    $filesize = $_FILES['userfile']['size'];
    $tmpName = $_FILES['userfile']['tmpName'];
    $target_dir = "/equipment/files/";
    $target_file = $target_dir . basename($_FILES["userfile"]["name"]);
    $error = 0;
    // Check if file already exists
    if (file_exists($target_file)) {
        $_SESSION['message'] = "File already exists";
        $error = 1;
    }

    // Check file size max size of 50 mb
    if ($filesize > 50000000) {
        $_SESSION['message'] .= " File size is too large";
        $error = 1;
    }
    if ($error == 1) {
        header("location: ../index.php");
        exit();

        // upload file 
    } else {
        move_uploaded_file($tmpName, $target_file);
        exit();
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source