Are there specific PHP functions or configurations that can enhance security when downloading files from a server?
When downloading files from a server, it is important to ensure that the process is secure to prevent malicious files from being downloaded or executed on the client's machine. One way to enhance security is by using PHP functions like `basename()` to sanitize file names and `header()` to set appropriate content-type headers. Additionally, you can restrict access to certain file types or directories using server configurations.
$file = 'path/to/file.pdf';
// Sanitize the file name
$filename = basename($file);
// Set appropriate content-type header
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $filename . '"');
// Output the file
readfile($file);
Related questions
-
type casting string manipulation regular expressions
-
POST data PHP security
-
PHP class function link click