Downloading a file from a database

Im pretty new to php so still trying to learn the basics. Currently trying to download a file from my database that I have previously uploaded successfully. I have my information from the database being displayed on my webpage through a datatable.

Basically for column ‘attachment’ it displays the filename of each file in every row. I want to create a link for each filename so that when it is clicked the attachment is downloaded. However I have got so far following tutorials etc but now I am stuck as to what to do next. The datatable is working fine and im not getting any errors but its obvious that I am missing a few key lines of code which enable me to download the file.

The code I’ve got that is outputting data into my datatable is;

[php]

while ( $aRow = mysql_fetch_array( $rResult ) )
{

	$row = array();
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        if ( $aColumns[$i] == "version" )
        {
            /* Special output formatting for 'version' column */
            $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
        }
        else if ( $aColumns[$i] != ' ' )
        {
            /* General output */
            $row[] = $aRow[ $aColumns[$i] ];
			
        }
    }
	
	
	$file == $row[7];
	
	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));
		readfile($file);
		exit;
	}
	

	
	
	
	 	

    $output['aaData'][] = $row;
}
 
echo json_encode( $output );

[/php]

Aside from using obsolete insecure Mysql code, your code makes no sense. You need to use PDO.

https://phpdelusions.net/pdo

Sponsor our Newsletter | Privacy Policy | Terms of Service