title: Downloading Files from FTP server - Ftp_fget Warning No such file or directory
body: <p>Ok, Recently I have been working on code for my website to enable users that have logged in to download files that are on the server. I have got the users logging in fine. My problem is getting the ftp_fget() function to work. I have not only tried ftp_fget() but also ftp_get() amd ftp_nb_get(). </p>
  
  <p>How I have got it set up at the moment is on one page all the files in a certain directory are being displayed. I then added this </p>
  
  <pre><code>&lt;a href="Replays/sc2_replays/ftp_download.php?file=&lt;? echo "$filename" ?&gt;"&gt;&lt;input type="image" src="images/dl_icon.png" width="41" height="41"/&gt;&lt;/a&gt;
  </code></pre>
  
  <p>When clicked it takes the user to the ftp_download.php page which is meant to download the chosen file.</p>
  
  <p>Here is what I have got for the ftp_download page.</p>
  
  <pre><code>&lt;?=
   $conn_id = ftp_connect("thomassawkins.hostoi.com","21") or die("could not connect");
   $ftp_login = ftp_login($conn_id,"USERNAME", "PASSWORD");
   $remote_file = $_GET['file'];
   $local_file = fopen("$remote_file",'w');
  
   ftp_pasv($conn_id, true); 
      if(!$ftp_login)
      {
          echo "could not login";
      }
      else
      {
  
          if (ftp_fget($conn_id, $local_file, $remote_file, FTP_BINARY)){
              echo "Successfully written to $local_file\n"; 
              } else {
              echo "There was a problem\n"; 
              }
      }
  
      ftp_close($conn);
       ?&gt;
  </code></pre>
  
  <p>When I click on the download button for the file I get this error after being directed to the download page </p>
  
  <pre><code>Warning: ftp_fget() [function.ftp-fget]: Can't open sc2 test - 2.txt: No such file or directory in /home/a5015247/public_html/Replays/sc2_replays/ftp_download.php on line 15
  </code></pre>
  
  <p>sc2 test - 2.txt is the test file I am trying to download. It is saved in the directory /home/a5015247/public_html/Replays/sc2_replays/</p>
  
  <p>What I am trying to achieve overall is for the user to click on the desired file to download and then have it prompt the user where they are wanting to save the file on their machine. </p>
  
  <p>Any help the solves my problems will be greatly appreciated.</p>
  
  <p>Regards,</p>
  
  <p>Thomas</p>
  
  <p>--edit--
  This is the code that displays all the files in the specified directory.</p>
  
  <pre><code>&lt;?php
     $startdir = 'Replays/sc2_replays';
     $showthumbnails = false; 
     $showdirs = true;
     $forcedownloads = false;
     $hide = array(
              'dlf',
              'public_html',              
              'index.php',
              'Thumbs',
              '.htaccess',
              '.htpasswd'
          );
     $displayindex = false;
     $allowuploads = false;
     $overwrite = false;
  
     $indexfiles = array (
              'index.html',
              'index.htm',
              'default.htm',
              'default.html'
          );
  
            $filetypes = array (
              'png' =&gt; 'jpg.gif',
              'jpeg' =&gt; 'jpg.gif',
              'bmp' =&gt; 'jpg.gif',
              'jpg' =&gt; 'jpg.gif', 
              'gif' =&gt; 'gif.gif',
              'zip' =&gt; 'archive.png',
              'rar' =&gt; 'archive.png',
              'exe' =&gt; 'exe.gif',
              'setup' =&gt; 'setup.gif',
              'txt' =&gt; 'text.png',
              'htm' =&gt; 'html.gif',
              'html' =&gt; 'html.gif',
              'php' =&gt; 'php.gif',             
              'fla' =&gt; 'fla.gif',
              'swf' =&gt; 'swf.gif',
              'xls' =&gt; 'xls.gif',
              'doc' =&gt; 'doc.gif',
              'sig' =&gt; 'sig.gif',
              'fh10' =&gt; 'fh10.gif',
              'pdf' =&gt; 'pdf.gif',
              'psd' =&gt; 'psd.gif',
              'rm' =&gt; 'real.gif',
              'mpg' =&gt; 'video.gif',
              'mpeg' =&gt; 'video.gif',
              'mov' =&gt; 'video2.gif',
              'avi' =&gt; 'video.gif',
              'eps' =&gt; 'eps.gif',
              'gz' =&gt; 'archive.png',
              'asc' =&gt; 'sig.gif',
          );
  
           error_reporting(0);
           if(!function_exists('imagecreatetruecolor')) $showthumbnails = false;
           $leadon = $startdir;
           if($leadon=='Replays/sc2_replays') $leadon = '';
           if((substr($leadon, -1, 1)!='/') &amp;&amp; $leadon!='') $leadon = $leadon . '/';
           $startdir = $leadon;
  
  
      if($_GET['dir']) {
  //check this is okay.
  
  if(substr($_GET['dir'], -1, 1)!='/') {
      $_GET['dir'] = $_GET['dir'] . '/';
  }
  
  
  $dirok = true;
  $dirnames = split('/', $_GET['dir']);
  for($di=0; $di&lt;sizeof($dirnames); $di++) {
  
  
      if($di&lt;(sizeof($dirnames)-2)) {
          $dotdotdir = $dotdotdir . $dirnames[$di] . '/';
      }
  
  
      if($dirnames[$di] == '..') {
          $dirok = false;
      }
  }
  
  
  if(substr($_GET['dir'], 0, 1)=='/') {
      $dirok = false;
  }
  
  
  if($dirok) {
       $leadon = $leadon . $_GET['dir'];
  }
      }
  
  
      $opendir = $leadon;
      if(!$leadon) $opendir = 'Replays/sc2_replays/';
      if(!file_exists($opendir)) {
  $opendir = 'Replays/sc2_replays/';
  $leadon = $startdir;
      }
  
      clearstatcache();
      if ($handle = opendir($opendir)) {
  while (false !== ($file = readdir($handle))) { 
      //first see if this file is required in the listing
      if ($file == "." || $file == "..")  continue;
      $discard = false;
      for($hi=0;$hi&lt;sizeof($hide);$hi++) {
          if(strpos($file, $hide[$hi])!==false) {
              $discard = true;
          }
      }
  
      if($discard) continue;
      if (@filetype($leadon.$file) == "dir") {
          if(!$showdirs) continue;    
  
          $n++;
          if($_GET['sort']=="date") {
              $key = @filemtime($leadon.$file) . ".$n";
          }
          else {
              $key = $n;
          }
          $dirs[$key] = $file . "/";
      }
      else {
          $n++;
          if($_GET['sort']=="date") {
              $key = @filemtime($leadon.$file) . ".$n";
          }
          elseif($_GET['sort']=="size") {
              $key = @filesize($leadon.$file) . ".$n";
          }
          else {
              $key = $n;
          }
          $files[$key] = $file;           
  
          if($displayindex) {
              if(in_array(strtolower($file), $indexfiles)) {
                  header("Location: $file");
                  die();
              }
          }
      }
  }
  closedir($handle); 
      }
  
      //sort our files
      if($_GET['sort']=="date") {
  @ksort($dirs, SORT_NUMERIC);
  @ksort($files, SORT_NUMERIC);
       }
      elseif($_GET['sort']=="size") {
  @natcasesort($dirs); 
  @ksort($files, SORT_NUMERIC);
     }
      else {
  @natcasesort($dirs); 
  @natcasesort($files);
      }
  
     //order correctly
     if($_GET['order']=="desc" &amp;&amp; $_GET['sort']!="size") {$dirs = @array_reverse($dirs);}
     if($_GET['order']=="desc") {$files = @array_reverse($files);}
     $dirs = @array_values($dirs); $files = @array_values($files);
  
     ?&gt;
     &lt;div id="listingcontainer"&gt;
      &lt;div id="listingheader"&gt; 
  &lt;div id="headerfile"&gt;&lt;/div&gt; 
  &lt;div id="headersize"&gt;&lt;/div&gt;
  &lt;div id="headermodified"&gt;&lt;/div&gt;
      &lt;/div&gt;
         &lt;div id="listing"&gt;
          &lt;?
         $class = 'b';
         if($dirok) {
  ?&gt;
    &lt;div&gt;&lt;a href="&lt;?=$dotdotdir;?&gt;" class="&lt;?=$class;?&gt;"&gt;&lt;img src="http://www.000webhost.com/images/index/dirup.png" alt="Folder" /&gt;&lt;strong&gt;..&lt;/strong&gt; &lt;em&gt;-&lt;/em&gt; &lt;?=date ("M d Y h:i:s A", filemtime($dotdotdir));?&gt;&lt;/a&gt;&lt;/div&gt;
  
  &lt;?
      if($class=='b') $class='w';
      else $class = 'b';
  }
  $arsize = sizeof($dirs);
  for($i=0;$i&lt;$arsize;$i++) {
  ?&gt;
  &lt;div&gt;&lt;a href="&lt;?=$leadon.$dirs[$i];?&gt;" class="&lt;?=$class;?&gt;"&gt;&lt;img             src="http://www.000webhost.com/images/index/folder.png" alt="&lt;?=$dirs[$i];?&gt;" /&gt;&lt;strong&gt;&lt;?=$dirs[$i];?&gt;&lt;/strong&gt; &lt;em&gt;-&lt;/em&gt; &lt;?=date ("M d Y h:i:s A", filemtime($leadon.$dirs[$i]));?&gt;&lt;/a&gt;&lt;/div&gt;
  &lt;?
      if($class=='b') $class='w';
      else $class = 'b';  
  }
  
  
  $arsize = sizeof($files);
  for($i=0;$i&lt;$arsize;$i++) {
      $icon = 'unknown.png';
      $ext = strtolower(substr($files[$i], strrpos($files[$i], '.')+1));
      $supportedimages = array('gif', 'png', 'jpeg', 'jpg');
      $thumb = '';
  
  
      if($filetypes[$ext]) {
          $icon = $filetypes[$ext];
      }
  
      $filename = $files[$i];
      if(strlen($filename)&gt;43) {
          $filename = substr($files[$i], 0, 40) . '...';
      }
  
      $fileurl = $leadon . $files[$i];
  ?&gt;
  &lt;div&gt;
    &lt;table width="574" border="0.5" align="center"&gt;
      &lt;tr&gt;
        &lt;th width="59" align="center" valign="middle" scope="col"&gt;&amp;nbsp;&lt;/th&gt;
        &lt;th width="136" align="center" valign="middle" scope="col"&gt;&lt;img src="http://www.000webhost.com/images/index/&lt;?=$icon;?&gt;" alt="&lt;?=$files[$i];?&gt;" /&gt;&lt;strong&gt;&lt;?=$filename;?&gt;&lt;/strong&gt;
        &lt;/a&gt;&lt;/th&gt;
        &lt;th width="101" align="center" valign="middle" scope="col"&gt;&lt;em&gt;
          &lt;?=round(filesize($leadon.$files[$i])/1024);?&gt;
          KB&lt;/em&gt;&lt;/a&gt;&lt;/th&gt;
        &lt;th width="186" align="center" valign="middle" scope="col"&gt;
          &lt;?=date ("M d Y h:i:s A", filemtime($leadon.$files[$i]));?&gt;
          &lt;/a&gt;&lt;/th&gt;
          &lt;th width="70" align="right" valign="middle" scope="col"&gt;
        &lt;a href="ftp_download.php?filedir=&lt;? echo "./$opendir"?&gt;"&gt;&lt;input type="image" src="images/dl_icon.png" width="41" height="41"/&gt;&lt;/a&gt;
  
          &lt;/a&gt;&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/table&gt; 
  &lt;/div&gt;
  &lt;?
      if($class=='b') $class='w';
      else $class = 'b';  
  }   
  ?&gt;&lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>What happens is when the user clicks the icon here</p>
  
  <pre><code>&lt;a href="ftp_download.php?filedir=&lt;? echo "./$opendir"?&gt;"&gt;&lt;input type="image" src="images/dl_icon.png" width="41" height="41"/&gt;&lt;/a&gt;
  </code></pre>
  
  <p>They are then redirected to download.php which handles the download part of it. At the moment I am not even able to successfully change the directory using chdir for some reason. the error that I get is </p>
  
  <pre><code>Warning: ftp_chdir() [function.ftp-chdir]: Can't change directory to /Replays/sc2_replays/: No such file or directory in /home/a5015247/public_html/ftp_download.php on line 15
  </code></pre>
  
  <p>the file dumby file that I am trying to download is in the directory public_html/Replays/sc2_replay/dumby.txt. </p>
  
  <p>I also user ftp_pwd to find out what directory I am currently in when i get this error and it outputs "/". Im not sure what that means</p>
  
  <pre><code>ftp_chdir($conn_id, "/Replays/sc2_replays/");
          echo ftp_pwd($conn_id);
  </code></pre>
  
  <p>Regards,</p>
  
  <p>Thomas</p>