How to get Uploaded CSV sheet header/Title in Magento2:

This script will helpful to identify whether your uploaded CSV sheet header matched with DB headers or not.
We can add this script for server side validation in magento2.

if (isset($_FILES[‘uploadcsv’]) && isset($_FILES[‘uploadcsv’][‘tmp_name’]) && ($_FILES[‘uploadcsv’][‘size’] > 0 ))
{
$UploadedHeader = array_map(“str_getcsv”, file($_FILES[‘uploadcsv’][‘tmp_name’],FILE_SKIP_EMPTY_LINES));
$UploadedHeaderKey = array_shift($UploadedHeader);
$csvActualHeader = array(‘PID’,’First Name’,’Second Name’,’Email’,’Mobile’);
$csvActualHeaderResult = array_diff($UploadedHeaderKey, $csvActualHeader);
if(empty($csvActualHeaderResult))
{
echo “Uploaded CSV header is match with configured headers”;
}else{
echo “Uploaded CSV header not match with configured headers”;
}

Leave a Reply