title: PHP: download any file from ftp-server to harddrive?
body: <p>i really really need your help. I'm successfully setting up a connection to my ftp server. However I can't figure out how I can download a dynamic file from my server. I guess this would help a lot of other people as well cause i couldn't find a tutorial or explanation anywhere on the web.</p>
  
  <p>I'm simply listing all of my files on my ftp. If i click on one i call download.php which connects again to the server and should download the file to my harddrive. I was able to auto-prompt a download window and a file gets downloaded to my hdd. however only a damaged file where no headers are set.</p>
  
  <pre><code>/* path to files on ftp server … e.g. /folder/folder/name_of_file.jpg */
  $p_arr = explode("/", $path);
  /* the file name … e.g name_of_file.jpg */
  $file = end($p_arr);
  
  //$finfo = finfo_open(FILEINFO_MIME_TYPE); //returns a fatal error - function not found 
  //$mimetype = finfo_file($finfo, $file);
  //finfo_close($finfo);
  
  //filetype($file)
  
  /* creating a temporyry file to save to */
  $tempFile = tempnam("/tmp", "FOO");
  
  if(ftp_get($conn_id, $tempFile, $file, FTP_BINARY)){ 
      /*header Content-type: must be dynamic*/
      //header('Content-type:' . $mimetype);
  
      /*header to auto_prompt download window*/
      header('Content-Disposition: attachment; filename="'. $file .'"');
      readfile($tempFile);
  } else { 
      echo "There was a problem &lt;br&gt;";
      echo $file . "&lt;br&gt;"; //e.g. image.jpg
  }
  
  /* deleting the file after the process */
  //unlink($tempFile);
  </code></pre>
  
  <p>1.) i can't figure out why ftp_get returns false. The connection gets set up and the file exists in the right directory.
  2.) i don't know how i can find out the mimetype of the file on the server and give it the downloaded file, so it's not damaged.</p>
  
  <p>please help me out here, i'm really stuck. thank you in advance</p>