How can the issue of the file name remaining empty during a file upload be resolved in the provided PHP script?

The issue of the file name remaining empty during a file upload can be resolved by checking if the file name is empty before moving the uploaded file to the desired directory. If the file name is empty, a new unique file name can be generated using functions like uniqid() to ensure that each uploaded file has a distinct name.

if ($_FILES['file']['name'] != '') {
    $file_name = $_FILES['file']['name'];
} else {
    $file_name = uniqid() . '_' . $_FILES['file']['name'];
}

$target_dir = "uploads/";
$target_file = $target_dir . basename($file_name);

if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
    echo "The file ". basename($file_name). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}