<?php
/*Skrypt pochodzi z ksi±¿ki "PHP. 101 praktycznych skryptów"
  autor: Marcin Lis                    http://marcinlis.com*/

function getFileName($id)
{
  if(!($fd = fopen("pliki.txt","r"))){
    return false;
  }
  while (!feof ($fd)){
    $line = trim(fgets($fd));
    if(($pos = strpos($line, " ")) === false)
      continue;
    $str_id = substr($line, 0, $pos);
    if($id == $str_id){
      return substr($line, $pos + 1, strlen($line));
    }
  }
  return false;
}
function send($name)
{
  if(!file_exists($name)){
    echo('Nie ma takiego pliku na serwerze!');
    return;
  }
  $name = str_replace("\\", "/", $name);
  if(($fileName = strrchr($name, "/")) === false){
    echo('Nie ma takiego pliku na serwerze!');
    return;
  }
  $fileName = substr($fileName, 1, strlen($fileName));
  $fd = fopen($name, "r");
  $size = filesize($name);
  $contents = fread($fd, filesize($name));

  fclose($fd);

  header("Content-Type: application/octet-stream");
  header("Content-Length: $size;");
  header("Content-Disposition: attachment; filename=$fileName");

  echo $contents;
}
if(isSet($_GET['id'])){
  if(($name = getFileName($_GET['id'])) === false){
    echo('Nie ma takiego pliku na serwerze!');
  }
  else{
    send($name);
  }
}
else{
  echo('Nie ma takiego pliku na serwerze!');
}
?>
