I have a PHP function for letting a user download files. (There are a few custom functions in the code, maybe_redirect
, validate_document
, headers_no_cache
, etc., but you get the idea from the names, and what they do precisely isn't germane to the issue.)
When this runs, the resulting file's create date is set to the current date and time. Is there any way for PHP to set that attribute for the resulting file?
Here's the function:
function fetch_file( $file, $force_download = false ) {
$file = maybe_redirect( $file );
if ( $DLfile = validate_document( $file ) ) {
global $pconfig;
$DLfile = htmlspecialchars( $DLfile );
$type = mime_content_type( $DLfile );
$chunksize = ( (int) $pconfig['misc']['download_chunk_size_mb'] ) * (1024 * 1024);
$size = intval( sprintf( "%u", filesize($DLfile) ) );
set_time_limit(300);
headers_no_cache();
header( 'Content-Type: ' . $type );
header( 'Content-Length: ' . $size );
if ( $force_download || $size > $chunksize ) {
header( 'Content-Disposition: attachment; filename="' . basename( $DLfile ) . '"' );
} else {
header( 'Content-Disposition: filename="' . basename( $DLfile ) . '"' );
}
if( $size > $chunksize ) {
// https://stackoverflow.com/a/21936954
header( 'Content-Type: application/octet-stream' );
header( 'Content-Transfer-Encoding: binary' );
$handle = fopen( $DLfile, 'rb' );
while ( ! feof($handle) ) {
echo @fread($handle, $chunksize);
ob_flush();
flush();
}
fclose($handle);
} else {
readfile( $DLfile );
}
exit;
} else {
http404();
}
}
There are two possible uses I might have to implement: get the dates from the actual file on the server, or get them from a database.