Search code examples
phpphp-ziparchive

Add comment to generated zip file before download via php


I hope you can help me on this matter. I tested a very simple script to add a comment to a zip file before downloading it, and it worked fine, so now I decided to try implementing it into a file hosting script, but I'm being unable to find a way to edit the generated file.

At line 59 of this file: https://pastebin.com/i3iBmW8s (which is the file which generates the download link for my file) you can see my edit, which I will also post below:

$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
    $ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
    $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
    $ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
    $ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
    $ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
    $ipaddress = getenv('REMOTE_ADDR');
else
    $ipaddress = 'UNKNOWN';

# Create a new Archive
$zip = new ZipArchive;
# Create the file to archive to
$res = $zip->open($file, ZipArchive::CREATE);
# If create works
if ($res === TRUE) {
    # Create the comment here
    $zip->setArchiveComment(base64_encode($ipaddress));
    $zip->close();
}

Basically it works to append the IP of who downloads it into the zip file, as comment.

As standalone script it works fine, but I'm having a hard time implementing it into the file hosting script, as when the file gets downloaded, it's unmodified.

I suppose the problem is with the variable $file which might not be correct, because as stand-alone script it's enough to specify in there the name of the archive in order to append the comment and then download it, while with the file hosting script, if I assign the variable holding the file name (which is $file->originalFilename), still nothing happens.

Hope you can assist me into properly implementing my script, thank you.


Solution

  • Turned out I was right, the problem was the variable $file which wasn't correct as the script wasn't able to define it as object

    I digged around and found the right variable, holding the full path to the file in the file hosting script which turned out to be:

    $file->getFullFilePath()