This topic is locked

Download Selected Zip File

12/10/2025 01:52:44
PHPRunner General questions
W
walid author

I executed the code

$filesArray = array();
while($record = $button->getNextSelectedRecord()){
$files = my_json_decode($record["files"]);
foreach($files as $f){
$filesArray[] = $f;
}
}
$zip = new ZipArchive();
$filename = "templates_c\DownloadSelected".date("YmsHis").".zip";
$result["error"] = "";
$result["name"] = $filename;
if($zip->open(getabspath($filename), ZipArchive::CREATE)!==TRUE) {
$result["error"] = "Cannot create zip file";
}

foreach($filesArray as $v)
$zip->addFile(getabspath($v["name"]),$v["usrName"]);

but it gives the following error
http://localhost/bar/templates_c/DownloadSelected20251210075153.zip
Not Found
The requested URL was not found on this server.

Apache/2.4.51 (Win64) OpenSSL/1.1.1l PHP/7.3.33 Server at localhost Port 80

ChatGPT 12/10/2025

The error you're encountering indicates that the ZIP file is not being created successfully in the specified directory. Here are some steps to troubleshoot and resolve the issue:

  1. Check Directory Permissions: Ensure that the directory templates_c where you're trying to create the ZIP file has the proper write permissions. The web server (Apache) needs permission to write files to that directory.


  2. Check the Path: Make sure that the path you're using for $filename is correct. The backslash \ in the path might cause issues in some contexts, especially on different operating systems. Consider using a forward slash / or PHP's DIRECTORY_SEPARATOR constant for better compatibility:

    $filename = "templates_c/DownloadSelected" . date("YmsHis") . ".zip";
  3. Debugging the Zip Creation: Before trying to add files to the ZIP, check if the ZIP file was created successfully or if an error occurred. After the $zip->open() call, you can add an error check:

    if ($zip->open(getabspath($filename), ZipArchive::CREATE) !== TRUE) {
    $result["error"] = "Cannot create zip file: " . $zip->getStatusString();
    }

  4. Check if Files Exist: Before adding files to the ZIP, ensure that the files you're trying to add actually exist. You can add a check before the addFile call:

    foreach($filesArray as $v) {
    $filePath = getabspath($v["name"]);
    if (file_exists($filePath)) {
    $zip->addFile($filePath, $v["usrName"]);
    } else {
    $result["error"] .= "File does not exist: " . $filePath . "\n";
    }
    }

  5. Close the Zip File: After adding files, make sure to close the ZIP file:

    $zip->close();
  6. Output the Full Path: To help with debugging, you can output the full path of the ZIP file being created:

    $result["full_path"] = getabspath($filename);

After checking all these points, try running your code again. If you still encounter issues, please provide additional details about the context in which this code is running, and any additional error messages you may receive.

Sergey Kornilov admin 12/10/2025

I assume that this code was added to the button. You need to follow instructions in this article and see if there are any error messages behind the scene.
https://xlinesoft.com/phprunner/docs/troubleshooting_custom_buttons.htm

W
walid author 12/10/2025

I want to correct the code