2015-08-02 11 views
6

Sto provando a scaricare file tramite le intestazioni dal mio database. Non sono sicuro del motivo per cui i miei file scaricati sono tutti corrotti quando cambio il mio codice di download in uno che utilizza OOP, ma sono perfetti quando il mio codice non è OOP.Il file di download dell'intestazione è corrotto

Questo è dove ho il file ID e chiamare la funzione di download: (handleDownload.php)

if (isset($_GET['id'])) { 
     $id = $_GET['id']; 
     //pump id into function getDBFiles to pull file with matching id 
     $fileData = $Download->getDBFiles($id); 
     header('Content-Type:"' . $fileData[2]. '"'); 
     header('Content-Disposition: attachment; filename="' . $fileData[1]. '"'); 
     echo $fileData[0]; 
     exit; 
     } 

Questa è la funzione che estrae il file dal database (download.php)

public function getDBFiles($id) { 
      global $database; 
      $sql = "SELECT * FROM ".self::$table_name." WHERE resume_id ='" . $id . "'"; 
      $result = $database->query($sql); 
      if ($row = $result->fetch_array(MYSQLI_ASSOC)) { 
       $name = $row['resume_title']; 
       $type = $row['file_type']; 
       $content = $row['resume_data']; //content of file 
       //$size = $row['file_size']; //file size 
       return array($content, $name, $type); 
      } 
     } 
$Download = new Download(); 
$download =& $Download; 

Il codice funziona bene se è tutto in una pagina come illustrato di seguito se:

if (isset($_GET['id'])) { 
    $id = $_GET['id']; 
    mysqli_select_db($con, "apples"); 

    $query = "SELECT * FROM resume where resume_id ='" . $id . "'"; 
    $result = mysqli_query($con, $query) or die('Error, query failed'); 


    if ($row = $result->fetch_array(MYSQLI_ASSOC)) { 
     $name = $row['resume_title']; 
     $type = $row['file_type']; 
     $content = $row['resume_data']; //content of file 
     $size = $row['file_size']; //file size 
     header('Content-Type:"' . $type . '"'); 
     //header('Content-length:"' . $size . '"'); 
     header('Content-Disposition: attachment; filename="' . $name . '"'); 
     //var_dump($row); 
     echo $content; 
    } 
} 

UPDATE: Ora sto ottenendo un file di download è danneggiato invece di un file vuoto. Questo è il modo in cui lo stesso file viene emesso dai diversi codici di download. Quello in cima è dal codice OOP mentre l'altro è dalla versione non-OOP funzionante. hex comparison

Questo è il mio codice di download nella sua interezza.

try { 
    //execute retrieval of files from database 
    $Download-> showDBFiles(); 
    //pass results to output array 
    $output = $Download->getMessages(); 
    //if id is set then get file from database 
    if (isset($_GET['id'])) { 
     $id = $_GET['id']; 
     //pump id into function getDBFiles to pull file with matching id 
     $fileData = $Download->getDBFiles($id); 
     header('Content-Type:"' . $fileData[2]. '"'); 
     header('Content-Disposition: attachment; filename="' . $fileData[1]. '"'); 
     echo $fileData[0]; 
     die(); 
     } 
} catch (Exception $e) { 
    $result[] = $e->getMessages(); 
} 

Dopo aver chiamato le funzioni, vorrei then echo fuori l'uscita (i link per il download) con un ciclo foreach

<h2>Output</h2> 
<?php if ($output) { ?> 
<ul class="result"> 
    <?php 
    foreach ($output as $message) { 
     $id = $message['id']; 
     $name = $message['name']; 
     ?> 
    <li><a href="handleDownload.php?id=<?php echo $id; ?>"><?php echo $name; ?></a></li> 

    <?php } 
?> 
</ul> 
+0

La prima riga $ è NULL? – SuperBear

+0

Intendi quello con OOP? Se è così, no non è NULL. Sono in grado di ottenere il seguente vardump da esso: array (8) {["resume_id"] => string (2) "83" ["individual_id"] => string (1) "7" ["resume_title"] => string (15) "hoppingBoy.docx" ["file_type"] => string (30) "application/vnd.openxmlformats" ["file_size"] => string (6) "215908" ["upload_date"] => string (10) "2015-08-02" ["stato"] => stringa (1) "1" ["resume_data"] => stringa (215908) ...} –

+2

** Pericolo **: Sei * * vulnerabile a [attacchi SQL injection] (http://bobby-tables.com/) ** che è necessario [difendere] (http://stackoverflow.com/questions/60174/best-way-to-prevent- sql-injection-in-php) da soli. – Quentin

risposta

0

Nel vostro check soluzione non OOP per i principali spazi bianchi all'interno del php file.

Il seguente codice produce un file danneggiato a causa di uno spazio vuoto iniziale.

<?php 
if (isset($_GET['id'])) {... 

Questo vale anche per gli spazi bianchi dopo il tag di chiusura php (che non si dovrebbe usare).

Tali caratteri verranno inviati dal browser, inclusi nel flusso di download e corrompono l'intero file.