سؤال

I have a PHP file whose output I want to add to a zip archive (using PHP's ZipArchive class). However, when I try to add it, it just gives me the PHP source code, unprocessed. How do I ensure the PHP is run and processed before adding it to the zip?

Here's what I've tried so far:

<?PHP
$z = $_GET['z']; // something like 'css-js-Type', etc.
$cols = $_GET['cols']; // a number
if (!$z)
{
    header("Location: /husk/get/");
    exit();
}
if ($cols)
    $cols = intval($cols);
else
    $cols = 12;

$zname = 'Husk_' . $z . "_$cols".'col' . '_' . getHuskVersion() . '.zip';
$here = $_SERVER['DOCUMENT_ROOT'] . '/prog/husk';
$filename = "$here/get/$zname";
if (!file_exists ($filename))
{
    $z = explode ('-', $z);

    $zip = new ZipArchive();

    if ($zip->open($filename, ZipArchive::CREATE) !== TRUE)
        exit("cannot open <$filename>\n");

    $_GET['cols'] = $cols;
    if (in_array('css', $z))
        $zip->addFile("$here\\_css\\HuskGen.php", "Husk_$cols.css");
    if (in_array('js', $z))
        $zip->addFile("$here\\_js\\Husk.jQuery.js", 'Husk.jQuery.js');
    if (in_array('Flex', $z))
        $zip->addFile("$here\\_css\\Flex.css", 'Flex.css');
    if (in_array('Type', $z))
        $zip->addFile("$here\\_css\\Type.css", 'Type.css');
    if (in_array('Print', $z))
        $zip->addFile("$here\\_css\\Print.css", 'Print.css');

    echo "numfiles: " . $zip->numFiles . "\n";
    echo "status:" . $zip->status . "\n";
    $zip->close();
}

header("Location: /husk/get/$zname");
?>
هل كانت مفيدة؟

المحلول

A file is a file. The "extension" does not matter when reading it.

If you need a PHP script to be interpreted, you'll have to do this yourself (this does not work for anything but PHP scripts):

 ob_start();
 require "myscript.php";
 $contents = ob_get_clean();

Then, $contents can be added to the ZIP file.

This approach, however, is naive, since it relies on the required PHP script to execute cleanly and deliver all the required output to STDOUT. Which PHP scripts tend not to do.

Depending on your php.ini (or other settings), warnings, errors or other things produced while executing the script that are not meant for public release will be included in your ZIP archive as well.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top