Visualizzazione risultati 1 fino 2 di 2

Discussione: visualizza file sottocartella e scarica file

  1. #1
    fcfclean non è connesso Utente
    Data registrazione
    17-09-2014
    Messaggi
    139

    Predefinito visualizza file sottocartella e scarica file

    ciao e buona serata :) non riesco a capire perchè non mi scarica il file pdf e/o doc ma si vedono dei caratteri strani come se il file fosse danneggiato chiedo aiuto grazie
    Codice PHP:
    <?PHP


    // Define a function to list files and subfolders
    function list_files($dir) {
    // Get an array of files and subfolders
    $files = scandir($dir);
    // Loop through the array
    foreach ($files as $file) {
    // Skip the current and parent directories
    if ($file != "." && $file != "..") {
    // Get the full path of the file or folder
    $path = $dir . "/" . $file;
    // Check if it is a file
    if (is_file($path)) {
    // Get the file name
    $name = basename($path);
    // Encode the file path
    $url = urlencode($path);
    // Create a download link
    echo "Documento: <a href='download.php?file=$url'>$name</a><br>";
    }
    // Check if it is a folder
    if (is_dir($path)) {
    // Print the folder name
    echo "Cartella: <b>$file</b>";
    // Call the function recursively
    list_files($path);
    }
    }
    }
    }

    // Call the function with the main directory
    list_files("documenti/".basename($_SESSION['username']));
    ?>
    ------------------------------------
    download.php

    <?php
    if(isset($_GET['file'])){
    $var_1 = $_GET['file'];
    $dir='documenti/';
    }
    ?>
    <?php
    if(isset($_GET['file'])){
    $var_1 = $_GET['file'];
    $file = $var_1;
    if (
    file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
    }
    echo
    "<h1>Content error</h1><p>il file non esiste!</p>";
    }
    ?>

  2. #2
    fcfclean non è connesso Utente
    Data registrazione
    17-09-2014
    Messaggi
    139

    Predefinito

    risolto non so il perchè ma ha funzionato alla grande! :D

Regole di scrittura

  • Non puoi creare nuove discussioni
  • Non puoi rispondere ai messaggi
  • Non puoi inserire allegati.
  • Non puoi modificare i tuoi messaggi
  •