我有一个PHP函数让用户下载文件。(代码中有一些自定义函数,
maybe_redirect
,
validate_document
,
headers_no_cache
等等,但是你从名字和他们的行为中得到了灵感
准确地说
与问题无关。)
运行此操作时,结果文件的创建日期设置为当前日期和时间。PHP有没有办法为生成的文件设置该属性?
函数如下:
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();
}
}