Uploaded file not detected in POST - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Saturday, June 5, 2021

Uploaded file not detected in POST

I've read a lot of similar issues but the solutions have not worked for me. I'd appreciate the help!

I have an index.php file which includes my HTML code and php code that moves the user's uploaded file to s3. My HTML form calls a js function sendEmails() which makes an AJAX request to another php script to validate the emails input and add it to a database. Everything is working except that the php code in my index.php file (at the very bottom) does not execute. It doesn't even go into the if statement and I don't understand why since it's supposed to execute when the user uploads a file and presses submit. If I put the $fileName = basename($_FILES["fileName"]["name"]) statements before the if statement then I get an undefined index error.

This is my HTML code in index.php:

<form action="javascript:void(0)" method="POST" id="files" enctype="multipart/form-data">
    <label class="col-md-4 col-form-label text-md-right">Select File:  <span class="text-danger">*</span></label>
    <input type="file" id="userFile" name="fileName" style="cursor: pointer; max-width: 170px;" onchange="enableBtn()">

    <label class="col-md-4 col-form-label text-md-right">Authorized Users:  <span class="text-danger">*</span></label>
    <input placeholder="Enter e-mail(s) here..." id="req" autocomplete="off"/>

    <button id="submitBtn" name="submitBtn" class="<?php echo SUBMIT_BUTTON_STYLE; ?>" onclick="return sendEmails()" disabled>Submit</button>
</form>

This is my php code in index.php:

<?php
$conn = new mysqli($servername, $username, $password, $db);
$sql = "SELECT sender_id, sender_email, receiver_emails, receiver_ids, file_name from filedrop_logs";
$result = mysqli_query($conn, $sql);

if ($result) {
    echo "<div class='outputDiv'>";
    echo "<table id='sharedOthers'>";
    echo "<thead><tr class='headings'>";
    echo "<th class='files'>Files</th>";
    echo "<th class='users'>Users</th>";
    echo "</tr></thead>";

    while ($row = mysqli_fetch_assoc($result)) {
        $receiverEmails = $row['receiver_emails'];
        $fileName = $row['file_name'];

        echo "<tbody id='bodyOthers'>";
        echo "<tr id='rowOthers'>";
        echo "<td>$fileName<br>";

        $objects = getListofObjects('FileDrop');
        foreach ($objects as $object) {
            $file = $object['Key'];
            $splits = explode('/', $file);
            if (end($splits) !== '') {
                $presignedUrl = getPresignedUrlForPrivateFile($object['Key'], '+20 minutes');
                $link = '<a href="'.$presignedUrl.'">Download</a>';
                echo $link;
            }
        }

        echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=''>Delete</a></td>";
        echo "<td>$receiverEmails</td>";
        echo "</tr></tbody>";
    }
    echo "</table></div>";
}
?>
<?php
//the if statement below doesn't execute

if(isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST') {
$fileName = basename($_FILES["fileName"]["name"]);
$error = $_FILES["fileName"]["error"];
$tmpName = $_FILES["fileName"]["tmp_name"];

if (isset($_FILES["fileName"]) && $fileName != '' && $tmpName != '' && sys_get_temp_dir()) {
    $separator = DIRECTORY_SEPARATOR;
    $newDir = sys_get_temp_dir() . $separator . "FileDrop" . microtime(true);

    if (!file_exists($newDir)) {
        mkdir($newDir, 0777, true); // creates temp FileDrop directory
        $tempFilePath = $newDir . $separator . $fileName; // creates temp file inside FileDrop directory

        if (move_uploaded_file($tmpName, $tempFilePath)) { // moves file to tmp folder
            $s3FileName = "FileDrop" . substr($newDir, 4) . $separator . $fileName;
            $result = putFileToS3($s3FileName, $tempFilePath, 'public-read');
            deleteDir($newDir);
        }
    }
}
}
?>

This is my js code in case you want to see it:

function sendEmails() {
var fileData = $('#userFile').prop('files')[0];
var formData = new FormData();
formData.append('tags', JSON.stringify(tags));
formData.append('fileName', fileData);
$.ajax({
    type: "POST",
    url: "../FileDrop/dbSystem.php",
    processData: false,
    contentType: false,
    data: formData,
    success: function(result) {
        result = JSON.parse(result);
        if (result.validity === "valid emails") {
            location.reload();
            resetInputs(); //IMPORTANT
            $(".outputDiv").show();
        }
        else {
            var tagsBrackets = result.emails.toString().replace(/[\[\]']+/g,'');
            var tagsQuotes = tagsBrackets.replace(/['"]+/g, '');
            var tagsInvalid = tagsQuotes.replace(/,/g, ", ");
            $('#alertModal').modal({show:true});
            document.getElementById('invalid').textContent = tagsInvalid;
        }
    }
});
return false;
}


source https://stackoverflow.com/questions/67842488/uploaded-file-not-detected-in-post

No comments:

Post a Comment