2

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.

Stephen R
  • 3,512
  • 1
  • 28
  • 45
  • 1
    Of course, a web server cannot fiddle with the local file system attributes of the computer where the browser runs. I suppose you mean if there's an HTTP header you can use to provide a hint and I'm not aware of any. – Álvaro González Jun 22 '18 at 16:43
  • No, but shouldn't it be capable of affecting the file it's in the process of handing over...? – Stephen R Jun 22 '18 at 16:44
  • 1
    Well, HTTP is a network protocol, it does not even have the concept of "files". It only becomes a file when the browser saves it to disk ;-) – Álvaro González Jun 22 '18 at 16:48
  • Yeah... that makes sense. The browser would have to be willing to accept date info from the website, which itself could probably be a security issue. Darn. Thanks @ÁlvaroGonzález – Stephen R Jun 22 '18 at 17:54
  • I'm unsure about security (not my area of expertise) but it probably has a fairly limited set of use cases and nobody has pushed to make it happen. Yet I've learnt to be cautious before saying *can't be done*—the web moves faster than me. – Álvaro González Jun 22 '18 at 18:14

0 Answers0