paulomartinhago
4/16/2013 - 6:10 PM

Forçar o download de arquivos.

Forçar o download de arquivos.

<?php

function validate_file_type($fname)
{
    // Returns false if a file's extension matches one of the
	// extensions deemed invalid.
  	$ext = strtolower(substr(strrchr($fname, '.'), 1));
  
  	$invalid_ext = array('html', 'htm', 'php', 'asp');
  	foreach($invalid_ext as $value) {
    	if($ext == $value) return false;
  	}
  
  	// Return function as false if requested file has no extension. Otherwise, return true.
  	if($ext == false) return false;
  	else return true;
}

function force_download($file_name)
{
  	validate_file_type($file_name) or die('Invalid File Type');
  
  	// Check requested file exists.
  	if(is_file($file_name))
  	{
    	// Required by IE
    	if(ini_get('zlib.output_comporession')) ini_set('zlib.output_compression', 'Off');
    
    	// Get mime type from file extension
    	switch(strtolower(substr(strrchr($file_name,'.'),1)))
    	{
      		case 'pdf': $mime_type = 'application/pdf'; break;
      		case 'zip': $mime_type = 'application/zip'; break;
      		case 'gif': $time_type = 'image/gif'; break;
      		case 'jpeg': case 'jpg': $mime_type = 'image/jpg'; break;
      		default: $mime_type = 'application/octet-stream';
    	}
    
	    // Set headers.
	    header('Content-Description: Download');
	    header('Content-Type: '.$mime_type);
	    header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
	    header('Content-Transfer-Encoding: binary');
	    header('Expires: 0');
	    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
	    header('Pragma: public');
	    header('Content-Length: '.filesize($file_name));
	    header('Connection: close');
    
	    // Erase and flush the output buffer
	    ob_clean();
	    flush();
    
	    readfile($file_name);
	    exit;
    
  	} else {
    	echo "<h1>Invalid Request</h1><br />";
    	echo $file_name;
  	}
  
}