NOVINKA: Získej 40 hodin praktických dovedností s AI – ZDARMA ke každému akreditovanému kurzu!
S účinností od 26. 3. jsme aktualizovali Zásady zpracování osobních údajů – doplnili jsme informace o monitorování telefonických hovorů se zájemci o studium. Ostatní části zůstávají beze změn.

Diskuze: Download souborů ne/funguje

V předchozím kvízu, Online test znalostí PHP, jsme si ověřili nabyté zkušenosti z kurzu.

Jak se ti líbí článek?
Před uložením hodnocení, popiš prosím autorovi, co je špatněZnaků 0 z 50-500
Jak se ti kurz líbí?
Tvé hodnocení kurzuZnaků 0 z 50-500
Aktivity
Avatar
John Smith
Člen
Avatar
John Smith:12.12.2022 23:02

Zkoušel jsem tento download souborů bohužel při stažení obrázku.png nebo souborů.rar nebo čehokoliv se soubory tváří jako poškozené obrázek se stáhne ale při otevření to píše nepodporovaný formát a uvnitř raru to vypadá podivně zpřeházeně. Jestli někoho něco napadne budu rád zkoušel jsem do na webhostingu zdarma na endora.cz kód je zde:

<?php

// Set the path to the folder containing the files
$folder_path = "./images/";

// Get a list of all files in the folder
$files = scandir($folder_path);

// Exclude the "." and ".." entries from the list
$files = array_diff($files, array('.', '..'));

// Check if the user has submitted the form
if (isset($_POST['download'])) {
  // Check if any files were selected
  if (isset($_POST['files'])) {
    // Loop through the selected files
    foreach ($_POST['files'] as $file) {
      // Generate the download link for the file
      $download_link = $folder_path . $file;

      // Send the appropriate headers to initiate a download
      header("Content-Type: application/octet-stream");
      header("Content-Disposition: attachment; filename=" . urlencode($file));
      header("Content-Type: application/download");
      header("Content-Description: File Transfer");
      header("Content-Length: " . filesize($download_link));

      // Output the file contents
      flush();
      readfile($download_link);
    }
  }
}

?>

<h1>Select files to download:</h1>
  <form method="post">
    <ul>
      <?php
      // Loop through the files and display a checkbox for each one
      foreach ($files as $file) {
        echo "<li><input type='checkbox' name='files[]' value='" . $file . "'>" . $file . "</li>";
      }
      ?>
    </ul>
    <input type="submit" name="download" value="Download">
  </form>
Editováno 12.12.2022 23:05
 
Odpovědět
12.12.2022 23:02
Avatar
Peter Mlich
Člen
Avatar
Peter Mlich:13.12.2022 8:14

Mas tam primy vstup post, coz by profik nikdy neudelal.
Nemas tam obclean, start, to tam zanasi kazdou chybu. Coz odstrani nezadouci vypsane texty, znaky pred vypisem souboru.
Mas tam funkce v okamziku, kdy precujes s header. Pokud u nektere nastane chyba, tak se ti muze cela hlavicka rozdrbat.
Neukoncujes program pri stazeni, takze, za souborem mas mozna vypsany i htm formular.
Pozn: Zruseni kesovani, ktere tam mam ja, neni dulezite. U mne jo, ja potrebuji, aby se mi to nekesovalo, ikdyz je nazev souboru stejny, protoze jde o exporty z programu.
Pozn2. Mas tam pouziti funkce pri vypisu html kodu. To ti muze delat pri kusy. Lepsi by bylo zpracovat php predem a vypisovat jen data, promenou. Myslim ten for-cyklus pro vypis souboru.
Pozn3. Nemel bys vypisovat seznam vsech souboru na disku. Muzes tam omylem nahrat nejaky php kod a hacker si pak ten soubor muze stahnout, kdyz vi jeho nazev. Nebo mu to tvuj program dovoli.

Tohle mam v programech ja.

        ob_clean();
        header('Content-Description: File Transfer');
        header('Content-Transfer-Encoding: binary');
        header('Content-Encoding: '.$charset);
        header('Content-Type: '.$type.'; charset='.$charset);
        header('Content-Disposition: attachment; filename="'.$file.'"');
        header('Content-Length: '.$size);
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        //header("Cache-Control: no-store, no-cache");
        header('Pragma: public');
        header('Expires: 0');
        echo $content;
        exit();

---
        $path    = $this->path;
        $charset = isset($arg['charset']) ? $arg['charset'] : "UTF-8";
        $file    = isset($arg['file'])    ? basename($arg['file']) : '';
        $type    = !isset($arg['type'])   ? 'application/octet-stream' : $type; // text/csv text/plain application/vnd.ms-excel application/download


        ob_clean();
        header('Content-Description: File Transfer');
        header('Content-Transfer-Encoding: binary');
        header('Content-Encoding: '.$charset);
        header('Content-Type: '.$type.'; charset='.$charset);
        header('Content-Disposition: attachment; filename="'.$file.'"');
        header('Content-Length: '.$size);
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        //header("Cache-Control: no-store, no-cache");
        header("Pragma: no-cache");
//      header('Pragma: public');
        header('Expires: 0');
        echo $content;
        exit();

---

        if ($download==true)
                {
                ob_clean();
                header("Content-Description: File Transfer");
                header('Content-type: application/zip');
                header("Content-Length: " . filesize($filename));
                header('Content-Disposition: attachment; filename="'.$zipFilename.'"');
                header("Content-Transfer-Encoding: binary");
                header("Pragma: no-cache");
                header("Expires: 0");
                echo file_get_contents($filename);
                unlink($filename);
                exit;
                }
Editováno 13.12.2022 8:17
 
Nahoru Odpovědět
13.12.2022 8:14
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 2 zpráv z 2.