Skip to content

Instantly share code, notes, and snippets.

@alejandroivan
Last active August 23, 2020 03:09
Show Gist options
  • Save alejandroivan/b583b2a7cbae13281af72f44dca490af to your computer and use it in GitHub Desktop.
Save alejandroivan/b583b2a7cbae13281af72f44dca490af to your computer and use it in GitHub Desktop.
PHP output contents of file
<?php
$file_path = "/full/path/of/the/file.doc";
if ( file_exists($file_path) ) {
$file_name = basename($file_path);
$mime_type = mime_content_type($file_path);
$file_size = filesize($file_path);
header('Content-Description: File Transfer');
header("Content-Type: {$mime_type}");
header("Content-Disposition: attachment; filename=\"{$file_name}\"");
header("Expires: 0");
header("Cache-Control: must-revalidate");
header("Pragma: public");
header("Content-Length: {$file_size}");
readfile($file_name); // Esto imprime el contenido del archivo
}
else {
http_response_code(404);
echo 'File does not exist.';
}
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment