1

I have the below code to open a CSV file, but for some reason it looks to be converting shopkeeper/89137.csv to shopkeeper_89137.csv. It keeps opening a blank csv file obviously because its looking for a file that isnt there. What is the proper way to reference a file in order to open it?

            header("Content-type: text/csv");
            header("Content-Disposition: attachment; filename=shopkeeper/89137.csv");
            header("Pragma: no-cache");
            header("Expires: 0");
4
  • hmm, you talk about "open a CSV file", but you actually stream it to the client's browser... Commented Dec 1, 2011 at 16:56
  • As far as I know, those are the headers that are sent to the browser indicating that content of a file is being sent rather than the content itself. Are you outputting the content in some way? The filename is simply the name of the file the browser will use when presenting the download. Commented Dec 1, 2011 at 16:57
  • @BrendanBullen "indicating that content of a file is being sent rather than the content itself" - had to read this a couple of times ;-) Commented Dec 1, 2011 at 16:59
  • @Leon After coming back to it after 16 hours, so did I! I think "indicating that content of a file is being sent rather than producing the content itself" would have been better. :) Commented Dec 2, 2011 at 9:03

3 Answers 3

2

Referencing file paths is not ideal in the HTTP header. You must reference only "89137.csv" for the filename, and then process your file by appending "shopkeeper/" to the filename in your file opening function. Remember that you can still use variables in the header string parameter.

Sign up to request clarification or add additional context in comments.

1 Comment

according to the spec you cannot reference file paths at all. Besides, these headers will open a SaveAs dialog at the user's browser, so how should the OP using script "append shopkeeper/ to the filename in your file opening function"?
1

The headers should only include the base name of the file being sent. On the receiving end of things, it thinks you're trying to pass it a file with the name "shopkeeper/89137.csv" which, on most operating systems, contains an illegal character (/) for a file name, so it gets converted to an underscore.

You should probably tweak the line to read:

header("Content-Disposition: attachment; filename=89137.csv");

Comments

1

Actually, the / character is reserved and cannot be used as a filename!!

I would reconsider your naming scheme...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.