Why am I getting mime-type of .csv file as "application/octet-stream"?

Why am I getting mime-type of .csv file as "application/octet-stream"?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why am I getting mime-type of .csv file as "application/octet-stream"?

When processing file uploads in PHP, relying solely on $_FILES['file']['type'] to determine a file's nature is often a recipe for misidentification. If you are attempting to import an Excel file converted to CSV and find your script reporting application/octet-stream, the issue isn't necessarily a bug in your logic or a broken conversion process; it is a fundamental misunderstanding of how MIME types are generated on the server versus what the client sends.

The value returned by $_FILES['file']['type'] is strictly what the HTTP client (the browser, script, or tool) claims the file is. It is rarely inspected by the server for truthfulness unless explicitly validated against magic bytes. When a user uploads a file, their browser looks at the file extension to construct the Content-Type header in the request. If the uploaded filename is data.csv, the browser sends text/csv. However, if the original file was an .xls or .xlsx binary Excel file that has been "converted" on the client side but retains its original binary structure or headers, the client might still misidentify it, or the conversion script running on the client side failed to update the extension while changing the content.

The most critical factor here is that application/octet-stream is the default MIME type for unknown or unguessable file types. When PHP cannot determine a valid type based on the provided information, or when the server explicitly chooses not to trust the user-supplied type, it defaults to this generic binary stream label. This often happens when security-conscious configurations disable automatic guessing based on extensions.

The real problem usually lies in how the "conversion" was performed. A true CSV file is a plain text format where data is separated by commas. An Excel file (.xls or .xlsx) is a proprietary, compressed binary format containing multiple sheets, styles, and formulas. You cannot simply rename an Excel binary file to .csv and expect it to be treated as such. If your application receives a file that is structurally an Excel binary but has a .csv extension, the server's MIME type detection library might flag it as binary (application/octet-stream) because its "magic numbers" (the specific byte sequences at the start of the file) do not match any known text-based CSV signature.

To handle this robustly in PHP, you should never trust $_FILES['file']['type'] blindly. Instead, use finfo_file() to inspect the actual content of the uploaded stream. This function reads the file's magic bytes and returns a reliable MIME type regardless of the filename or the client's claim.

$fileName = $_FILES['excel']['name'];
$fileContent = fopen($_FILES['excel']['tmp_name'], 'rb');

// Create a finfo resource with mime_database.ini
$finfo = new finfo(FILEINFO_MIME_TYPE);

$detectedType = $finfo->file($fileContent);
fclose($fileContent);

if ($detectedType === 'application/octet-stream') {
    // The file is binary, likely an Excel file masquerading as CSV
    throw new Exception("Uploaded file is not a valid text-based CSV.");
}

This approach ensures that even if a malicious actor renames a zip archive to report.csv to bypass filters, your server will correctly identify it as application/zip (or application/octet-stream if the magic bytes are ambiguous) rather than text/csv.

If you need to proceed with an import regardless of the initial detection because you know the source is guaranteed safe, you must still validate the content before parsing. Using libraries like PHPSpreadsheet or PhpSpreadsheet is essential here. These libraries can parse Excel formats directly without needing a client-side conversion step that might break or alter MIME types incorrectly. Attempting to load a binary Excel file as a CSV string using file_get_contents and simple regex splitting will inevitably fail, causing silent data corruption or import errors.

The list of MIME types you gathered (text/comma-separated-values, application/csv, etc.) is technically correct for valid text-based CSVs. However, the presence of application/vnd.ms-excel in your list highlights a specific ambiguity: some servers might misidentify Excel files as generic application streams if the specific vendor MIME type isn't registered or supported by the underlying system's database. The solution remains consistent: inspect the magic bytes, not the extension.

For production environments handling sensitive data, relying on client-side headers is insufficient. Always validate file contents against expected signatures before processing them into a database like MySQL. If you require assistance with robust file handling strategies or need to integrate these validation patterns into your existing stack, resources from https://laravelcompany.com offer further guidance on secure file processing workflows.

Our network