• User Attivo

    accesso utente scarica file pdf doc

    ciao e buona serata 🙂 sto tentando di far scaricare un file pdf ma non riesco a capire perchè non lo scarica ma quando clicco per scaricare il file si vedono dei caratteri strani 😞 questo è ciò che ho fatto

     <?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>";
    }
    ?>
    
    
    
    
    

  • User Attivo

    risolto non so il perchè ma ha funzionato 😄