Csv mime type php

Php – How to use the CSV MIME-type

In a web application I am working on, the user can click on a link to a CSV file. There is no header set for the mime-type, so the browser just renders it as text. I would like for this file to be sent as a .csv file, so the user can directly open it with calc, excel, gnumeric, etc.

header('Content-Type: text/csv'); echo "cell 1, cell 2"; 

This code works as expected on my computer (Isn’t that how it always is?) but does not work on another computer.

Читайте также:  Datetime python django model

My browser is a nightly build of FF 3.0.1 (on linux). The browsers it did not work in were IE 7 and FF 3.0 (on windows)

Are there any quirks I am unaware of?

Best Solution

You could try to force the browser to open a «Save As. » dialog by doing something like:

header('Content-type: text/csv'); header('Content-disposition: attachment;filename=MyVerySpecial.csv'); echo "cell 1, cell 2"; 

Which should work across most major browsers.

Python – How to find the mime type of a file in python

The python-magic method suggested by toivotuo is outdated. Python-magic’s current trunk is at Github and based on the readme there, finding the MIME-type, is done like this.

# For MIME types import magic mime = magic.Magic(mime=True) mime.from_file("testdata/test.pdf") # 'application/pdf' 
Proper MIME media type for PDF files

The standard Media Type (formerly known as MIME types) is application/pdf . The assignment is defined in RFC 3778, The application/pdf Media Type, referenced from the Media Types registry.

Media Types are controlled by a standards body, The Internet Assigned Numbers Authority (IANA). This is the same organization that manages the root name servers and the IP address space.

The use of x-pdf predates the standardization of the Media Type for PDF. Media Types in the x- namespace are considered experimental, just as those in the vnd. namespace are considered vendor-specific. x-pdf might be used for compatibility with old software.

Related Question

Источник

How to Use the CSV Mime-Type

There is an RFC which covers it and says to use text/csv .

This RFC updates RFC 4180.

Excel

Recently I discovered an explicit mimetype for Excel application/vnd.ms-excel. It was registered with IANA in ’96. Note the concerns raised about being at the mercy of the sender and having your machine violated.

Media Type: application/vnd.ms-excel

Name Microsoft Excel ™

Required parameters: None

Optional parameters: name

Encoding considerations: base64 preferred

Security considerations: As with most application types this data is
intended for interpretation by a program that understands the data on
the recipient’s system. Recipients need to understand that they are at
the «mercy» of the sender, when receiving this type of data, since
data will be executed on their system, and the security of their
machines can be violated.

OID

Object type spreadsheet

Comments This Media Type/OID is used to identify Microsoft
Excel generically (i.e., independent of version, subtype, or platform format).

I wasn’t aware that vendor extensions were allowed. Check out this answer to find out more — thanks starbeamrainbowlabs for the reference.

How to use the CSV MIME-type?

You could try to force the browser to open a «Save As. » dialog by doing something like:

header('Content-type: text/csv');
header('Content-disposition: attachment;filename=MyVerySpecial.csv');
echo "cell 1, cell 2";

Which should work across most major browsers.

What mime type should I use for CSV ZIP files?

A zip file is a zip file, no matter what it contains.

It should be application/zip .

It is not a CSV file, if you tried to parse it as CSV it would fail. It is not text/csv .

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

In times like these, the official HTTP specification is always helpful. From RFC 2616 7.2.1 (my emphasis added):

Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body. If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource. If the media type remains unknown, the recipient SHOULD treat it as type «application/octet-stream».

The cause of your issue is that the server accepting the file upload does not itself know what type of file has been uploaded. Why? Because it relies on the the HTTP message which sent the file to specify a Content-Type header to determine the exact mime-type. The browser has likely not sent a Content-Type header and the server has assumed application/octet-stream as per the official HTTP specification excerpt above. It’s also possible that the client uploading the file opted not to determine the mime type of the file it was uploading and sent the Content-Type: application/octet-stream header itself.

Now, when we consider this in conjunction with the PHP manual entry regarding POST file uploads docs , we see the following:

$_FILES[‘userfile’][‘type’]

The mime type of the file, if the browser provided this information. An example would be «image/gif». This mime type is however not checked on the PHP side and therefore don’t take its value for granted.

So as you can see, even if $_FILES[‘userfile’][‘type’] is specified, it only corresponds to the Content-Type header sent by the client. This information can easily be faked and should not be relied upon. If you need to be sure that the uploaded file is of a specific type, you’ll have to verify that yourself.

Swift — Create CSV file with text/csv MIME type

Thank you everyone for your input!

I received some help from the Apple Developer Forums:
https://developers.apple.com/forums/thread/710757?page=1#721303022

Changing the line-break characters from \n to \r\n did the trick! The file command now shows the file’s MIME type as text\csv.

What to set as mimetype for CSV files to open in spreadsheet applications

Content-Type: text/csv; name="filename.csv"
Content-Disposition: attachment; filename="filename.csv"

In Windows browsers the MIME type is ignored after the file is downloaded and only file name extension is used to determine the corresponding application.

Also, making .CSV file open in Excel correctly is hard, because the default separator is taken from Regional settings on the machine on which the CSV is opened. Furthermore, the encoding is not determined from the file content or MIME type, so any non-ASCII characters are hard to get working.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload.php getting wrong MIME Type for .CSV file. #2169

Upload.php getting wrong MIME Type for .CSV file. #2169

Comments

My computers setup:
Mac OS 10.8.2
Chrome 24.0.1312.52
MAMP 2.1.1
PHP 5.4.4

The MIME Type that the upload class is returning when I try to upload a csv file created by Mac Excel is text/plain

I had a friend upload the same file from his Windows 7 machine and he didn’t have any problems.

I don’t know much about mime types but after reading this article it looks like the issue I am having is based on my local computer configuration (Browser, OS, etc)

The text was updated successfully, but these errors were encountered:

I have been getting text/plain as result for uploading .css and .js files.
I narrowed it down to the function _file_mime_type($file) in the Upload.php system library.

Since my install didn’t support finfo_file(), it defaulted to using the exec call to ‘file —brief —mime’

This call does not work on my system, because for some reason the server I am on returns

for .css, and the same for most .js files I try it on.. forcing me to use * for the file type specification.

I couldn’t figure out a way to seemlessly bypass the file —brief —mime call, so I ended up using * for file type.

I considered it a server issue since the server isn’t returning the right mime type.. which is why I didn’t open an issue on it..

This may not be the OP’s issue, but it was definately a frustrating bit of time before I figured out the issue.. luckily for the server I am working on, I am not relying on john q. public to upload files.. but only myself and a couple other employees, so I am not too worried about malicious files getting uploaded (since I can’t validate they are the right type). but I wish there was a better solution.

Источник

Get Mime-Type by file extension in PHP

Recently I build a PHP script for dynamically downloading files, and I came across the need to provide the right Mime-Type for each file I set to download. I wrote a function in PHP that gets file extension (with or without the dot prefix) and returns the correct Mime-Type.

This function also returns the general purpose mime-type for missing or unknown file types.

General purpose MIME-TYPE

Use: application / octet — stream

case ‘.arc’ : $mime = ‘application/octet-stream’ ; break ; // Archive document (multiple files embedded)

case ‘.docx’ : $mime = ‘application/vnd.openxmlformats-officedocument.wordprocessingml.document’ ; break ; // Microsoft Word (OpenXML)

case ‘.js’ : $mime = ‘application/javascript’ ; break ; // JavaScript (IANA Specification) (RFC 4329 Section 8.2)

case ‘.mid’ : $mime = ‘audio/midi audio/x-midi’ ; break ; // Musical Instrument Digital Interface (MIDI)

case ‘.midi’ : $mime = ‘audio/midi audio/x-midi’ ; break ; // Musical Instrument Digital Interface (MIDI)

case ‘.odp’ : $mime = ‘application/vnd.oasis.opendocument.presentation’ ; break ; // OpenDocument presentation document

case ‘.ods’ : $mime = ‘application/vnd.oasis.opendocument.spreadsheet’ ; break ; // OpenDocument spreadsheet document

case ‘.odt’ : $mime = ‘application/vnd.oasis.opendocument.text’ ; break ; // OpenDocument text document

case ‘.pptx’ : $mime = ‘application/vnd.openxmlformats-officedocument.presentationml.presentation’ ; break ; // Microsoft PowerPoint (OpenXML)

case ‘.swf’ : $mime = ‘application/x-shockwave-flash’ ; break ; // Small web format (SWF) or Adobe Flash document

case ‘.xlsx’ : $mime = ‘application/vnd.openxmlformats-officedocument.spreadsheetml.sheet’ ; break ; // Microsoft Excel (OpenXML)

1 Comment

maya.kimhi

Leave a comment Cancel reply

You must be logged in to post a comment.

cute amateur anal and big cum facial in porn debut most beautiful pussy in the world gay my hero academia sex big ass trany porn clips

High definition xxx video of Vira doing anal Horny amateur teen peeing and masturbating on cam

This website uses cookies to improve your experience. We’ll assume you’re ok with this, but you can opt-out if you wish. Cookie settingsACCEPT

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are as essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.

Источник

Оцените статью