2017-09-21 2 views
0

Je voudrais télécharger un PDF via PHP depuis mon serveur, qui est également téléchargé via PHP. Bien que cela fonctionne avec des fichiers que je reçois le message d'erreur suivant avec des fichiers individuels:Télécharger avec PHP -> ERR_CONTENT_LENGTH_MISMATCH

ERR_CONTENT_LENGTH_MISMATCH

Ceci est ma fonction de téléchargement:

if($_GET['funktion'] == 'downloadverwaltung') 
{ 


    $filename = basename($dateiname); 
    $path = '/kunden/261105_71522/webseiten/admin/PDF/fern_downloads/'.$rowkurs['courseid'].'/'.$filename.''; // the file made available for download via this PHP file 

    if($_SESSION['xle'][$_GET['idsh']]['letype'] == 'fern' && $_GET['link'] == 'ja') 
     { 
     $path = 'http://ls.gdvz.de/'.$filename.''; 
     } 
    elseif($_SESSION['xle'][$_GET['idsh']]['letype'] == 'fern' && $_GET['link'] != 'ja') 
     { 
     $path = '/kunden/261105_71522/webseiten/dev.delst/admin/PDF/fern_downloads/'.$_GET['kursid'].'/'.$filename.''; 
     } 
    elseif($_SESSION['xle'][$_GET['idsh']]['letype'] == 'kurs' && $_GET['link'] == 'ja') 
     { 
     $path = 'http://ls.gdvz.de/'.$filename.''; 
     } 
    elseif($_SESSION['xle'][$_GET['idsh']]['letype'] == 'kurs' && $_GET['link'] != 'ja') 
     { 
     $path = '/kunden/261105_71522/webseiten/dev.delst/pdf/'.$_GET['kursid'].'/'.$filename.''; 
     }     

    $check = file_exists($path); 
    if($_GET['link'] == 'ja') 
     $check = fopen($path, "r"); 

    //echo $path; 
    if ($check) { 
      $mm_type="application/octet-stream"; // modify accordingly to the file type of $path, but in most cases no need to do so 

      header("Pragma: public"); 
      header("Expires: 0"); 
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
      header("Cache-Control: public"); 
      header("Content-Description: File Transfer"); 
      header("Content-Type: " . $mm_type); 
      header("Content-Length: " .(string)(filesize($path))); 
      header('Content-Disposition: attachment; filename="'.basename($path).'"'); 
      header("Content-Transfer-Encoding: binary\n"); 

      readfile($path); // outputs the content of the file 

      exit(); 
     } 

     else 
      echo'Datei wurde nicht auf dem Server gefunden'; 
} 

Si je retire la tête ("Content-Length : ". (chaîne) (taille de fichier ($ chemin))); le téléchargement fonctionne, mais le fichier PDF est alors cassé et ne peut pas être ouvert.

J'ai essayé également les éléments suivants:

if($_GET['funktion'] == 'downloadverwaltung') 
{ 


    $filename = basename($dateiname); 
    $path = '/kunden/261105_71522/webseiten/admin/PDF/fern_downloads/'.$rowkurs['courseid'].'/'.$filename.''; // the file made available for download via this PHP file 

    if($_SESSION['xle'][$_GET['idsh']]['letype'] == 'fern' && $_GET['link'] == 'ja') 
     { 
     $path = 'http://ls.gdvz.de/'.$filename.''; 
     } 
    elseif($_SESSION['xle'][$_GET['idsh']]['letype'] == 'fern' && $_GET['link'] != 'ja') 
     { 
     $path = '/kunden/261105_71522/webseiten/dev.delst/admin/PDF/fern_downloads/'.$_GET['kursid'].'/'.$filename.''; 
     } 
    elseif($_SESSION['xle'][$_GET['idsh']]['letype'] == 'kurs' && $_GET['link'] == 'ja') 
     { 
     $path = 'http://ls.gdvz.de/'.$filename.''; 
     } 
    elseif($_SESSION['xle'][$_GET['idsh']]['letype'] == 'kurs' && $_GET['link'] != 'ja') 
     { 
     $path = '/kunden/261105_71522/webseiten/dev.delst/pdf/'.$_GET['kursid'].'/'.$filename.''; 
     }     

    //echo $path; 
    if (file_exists($path)) { 
      // $mm_type="application/octet-stream"; // modify accordingly to the file type of $path, but in most cases no need to do so 

      // header("Pragma: public"); 
      // header("Expires: 0"); 
      // header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
      // header("Cache-Control: public"); 
      // header("Content-Description: File Transfer"); 
      // header("Content-Type: " . $mm_type); 
      // header("Content-Length: " .(string)(filesize($path))); 
      // header('Content-Disposition: attachment; filename="'.basename($path).'"'); 
      // header("Content-Transfer-Encoding: binary\n"); 

      set_time_limit(0); 
      output_file($path, basename($path), 'application/octet-stream'); 

      exit(); 
     } 

     else 
      echo'Datei wurde nicht auf dem Server gefunden'; 
} 

Le chemin est correct et le fichier se trouve sur le serveur. Donc, le téléchargement fonctionne ..

Qu'est-ce que je fais mal?

Répondre

0

Vérifiez ici: How to make PDF file downloadable in HTML link?

header("Content-Type: application/octet-stream"); 

$file = $_GET["file"] .".pdf"; 
header("Content-Disposition: attachment; filename=" . urlencode($file)); 
header("Content-Type: application/octet-stream"); 
header("Content-Type: application/download"); 
header("Content-Description: File Transfer");    
header("Content-Length: " . filesize($file)); 
flush(); // this doesn't really matter. 
$fp = fopen($file, "r"); 
while (!feof($fp)) 
{ 
    echo fread($fp, 65536); 
    flush(); // this is essential for large downloads 
} 
fclose($fp); 
+0

... ne fonctionne pas :( – Codehan25

+0

Il ne fonctionne pas ... pourquoi? Quelle est l'erreur? – Ryosaku

1

Vous pouvez essayer d'augmenter la limite de mémoire avec ini_set ('memory_limit', '256M');

if (file_exists ($filepath)) { 

    header ('content-type: application/octet-stream'); 
    header ('content-Transfer-Encoding: Binary'); 
    header ('content-length: ' . filesize ($filepath)); 
    header ('content-disposition: attachment; filename=' . basename ($filepath)); 

    // setzt temporär die Grenze für den zur Vergügung stehenden Arbeitsspeicher hoch 
    ini_set ('memory_limit', '256M'); 

    ob_clean(); 
    ob_flush(); 
    flush(); 

    readfile ($filepath); 
} 
else { 
    Throw new Exception ("Die Datei $filepath konnte nicht gefunden werden"); 
} 
+0

Hmm, maintenant je reçois un écran vide .. – Codehan25

+0

Avez-vous activé error_reporting? – Max

+0

Yup, c'est activé -> toujours rien ... – Codehan25