Why can not I download a file in PHP?

0

I have a problem when I want to download a file, in this case Excel. I found the code on the Internet, when I paste it I get an error in the header .

I've searched for other codes, but apparently they still have problems in the header. It works that by clicking on a button you must download an Excel template so that it can be filled and then loaded. When loading the file I have no problems. My Excel file has locked cells, I do not know if that influences.

This is the post code of the button

if ( isset( $_POST[ 'descargar' ] ) ) {
			$file=	'plantilla_proyectosLuces.xlsx';
			if(is_file($file))
			{
				$filename="Plantilla_Productos.xlsx";//nombre que se la dara al descargar
				
				//header("Content-disposition: attachment; filename='descarga.xlsx'");
				//header("Content-type: application/vnd.ms-excel");
				header("Content-Type: application/force-download");
				//header("Content-type: application/vnd.ms-excel"); 
				header("Content-Disposition: attachment; filename=\"$filename\"\n");

				readfile($file);
			}
			else
		{
				die("Error: no se encontro el archivo a descargar");
			}
 }

and this is the screen where I get the errors although I do not know why I get those characters

    
asked by EngelDragoner 06.09.2018 в 19:38
source

1 answer

1

Look at this code can help

$file = "location of file to download"
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
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));
ob_clean();
flush();
readfile($file);

Pay attention to MIME header('Content-Type: application/octet-stream');

Here is a list of the most common link

When you use header("Content-Type: application/force-download"); imagine that the server will respond like this:

I'm going to send a mysterious file so the client can save it on his hard drive as he can, but if he does not save it?

Greetings:)

    
answered by 06.09.2018 / 20:20
source