-1
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Download.csv");

this code helps me to download the csv file when i run my script in the browser.

But now instead of downloading the file , is it possible to save a file directly in folder ,

I mean how can i modify the code so that when my scripts run it should safe the csv file instead of prompting the user to download it .

For example: This code will create file in the directory.

$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");

likewise how can i modify the header Content-Disposition so that it will create csv file in folder.

any help will be appriciated.

Thanks

Afsar
  • 3,104
  • 2
  • 25
  • 35
  • I am not sure I understood what you want, are you saying you want your download PHP file to tell the user in what folder the file should be downloaded to ? – Prix Feb 02 '13 at 12:04
  • What do you expect to happen after you open script? Remember, that you can't control where file will be saved after download. – Viktor S. Feb 02 '13 at 12:06
  • How about: http://stackoverflow.com/questions/217424/create-a-csv-file-for-a-user-in-php ? – Jon Feb 02 '13 at 12:21
  • @Jon i want create the file not as an attachment , but as a generated file in the directory – Afsar Feb 02 '13 at 12:28
  • Setting the header has nothing to do with saving it as a file, the header is what it sent to the browser, so if you are trying to save it on the server, the `fopen` should work, if you want to save it on the client's side - you should look in to the client storage options newly available in JS. – Jon Feb 02 '13 at 12:31

2 Answers2

1

You can't control where a downloaded file will be saved on a client's computer. And you should not be able to do so; Imagine a website that will automatically save and replace a file on your local computer when opening the website? E.g. it downloads 'malicious_file.exe' to

C:\Documents and Settings\MyName\Start Menu\Programs\Startup\malicious_file.exe

This file will automatically be executed on log-in!

thaJeztah
  • 27,738
  • 9
  • 73
  • 92
-3

If I understand you correctly you need a .htaccess file with this line

Options +Indexes

So you can see all files in folder

Alexander_F
  • 2,831
  • 3
  • 28
  • 61
  • If the file already existed on the server, this wouldn't be an issue. Please read at least the title, or if nothing else, attempt to understand the contents of the question. – Jon Feb 02 '13 at 12:35