Disappearing download


I’m trying to create a csv file, write it to my server, and then download it to a user’s local computer. The first part of my code successfully creates the file:

$fp = fopen('filetest.csv', 'w') or die("can't open file"); //write to a file
foreach ($list as $fields) {
    fputcsv($fp, $fields);

At this point, I’ve looked on my server and found filetest.csv, safe and sound. However, if I continued to try and download it, running the full script:

// define some variables
$local_file = 'filetest.csv';
$server_file = '/Instructors/filetest.csv';

// set up basic connection
$ftp_server = "myalgebrabook.com"; 
$conn_id = ftp_connect($ftp_server);

// login with username and password
$ftp_user_name = "mydatabase"; 
$ftp_user_pass = "fakepassword"; 

$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to download $server_file and save to $local_file
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
    echo "Successfully written to $local_file\
} else {
    echo "There was a problem\

// close the connection

I get the following error: Can’t open /Instructors/filetest.csv: No such file or directory in /home/myaglebr/public_html/Instructors/download_assignment_scores.php

AND if I go back on the server, the filetest.csv disappears. In other words, just writing the file to the server works fine, but writing to the file and then trying to download somehow deletes the filetest.csv file, gives me the above error, and doesn’t download anything.

Any help would be appreciated.

Thank you,


/ is not what you think it is.
The error message should give you a clue…

I get the following error: Can’t open /Instructors/filetest.csv: No such file or directory in /home/myaglebr/public_html/Instructors/download_assignment_scores.php

Thank you for replying to the post! I actually also tried “/home/myaglebr/public_html/Instructors/filetest.csv” (I think that this what you’re getting at) but to no avail. Also, it would seem that it’s finding the file, and acting on it — from my original post, you can see that the file is deleted from the server without actually downloading it.

Any other hints would be appreciated! :slight_smile:

If you are downloading it locally, then why don’t use http and cURL to download it, i feel much more easier.

as tamil says, ftp cant do anything to a local computer (unless said computer has an FTP server on it). Output the file (echo file_get_contents) to the browser and give it the standard header for a download (Content-Disposition: attachment; filename=“filenamegoeshere”)

Thank you both. It’s my first time downloading through PHP and with these tips I’m sure that I’ll be able to make it happen.