我想把两个文件,一个是thanks.txt,另一个是file.dat,放入一个带有以下代码的压缩文件中,然后链接到这个压缩文件,但我的代码不能正常运行,我需要帮助来纠正和优化这个代码.

thanks.txt文件内,客户的用户邮箱中包含以下文本:

Hi, Dear '.$email_address.' Thanks for using it!

My codes:

funtion create_zip_file() {
    
    // Get Current User Email Address!
    $current_user = wp_get_current_user();
    $email_address = $current_user->user_email;
    
    $md5_address = md5($email_address);

    $directory_path = 'myfiles/cloud/' . $md5_address . '/';
    if (!file_exists($directory_path)) {
        mkdir($directory_path, 0777, true);
    }
    
    $Myfile = file_put_contents($directory_path . 'file.dat' , $email_address);

    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
        $website_url = 'https://' . $_SERVER['HTTP_HOST'] .'/';
    } else {
        $website_url = 'http://' . $_SERVER['HTTP_HOST'] .'/';
    }

    $result = $website_url . $directory_path . 'file.dat'; 

    $zip = new ZipArchive;
    if ($zip->open($zip_file , ZipArchive::CREATE) === TRUE)
    {
        // Add files to the zip file
        $zip->addFile($result);
        
        // Add a file new.txt file to zip using the text specified
        $zip->addFromString('thanks.txt', 'Hi, Dear '.$email_address.' Thanks for use it!');
     
        // All files are added, so close the zip file
        $zip->close();
        
        // Delete file after zip it
        unlink($result);
    }
    
    $zip_file = $website_url . $directory_path . 'file.zip';
    
    if (file_exists($zip_file)) {
        return $zip_file;
    } else {
        return false;
    }
}

And I call the zip file by the following line:

<a href="<?php echo create_zip_file();"> Download zip file </a>

如果我在以下行($zip_file)中使用静态地址:

if ($zip->open($zip_file , ZipArchive::CREATE) === TRUE)

将创建压缩文件,但当我使用动态地址时,不会创建任何压缩文件

推荐答案

$zip->addFile($result);期望有效的path on server,但您却传入了URL to that file.

$zip_file = $website_url . $directory_path . 'file.zip';个也是一样


使用与创建.dat文件相同的路径:$zip->addFile($directory_path . 'file.dat')

Php相关问答推荐

允许Laravel路由指定`withTrashed`,尽管有显式的模型绑定

如何使用URL链接(mdl)切换选项卡

在WooCommerce中禁用客户用户角色的纳税计算

Php解压缩deflate64

从WooCommerce订单状态更改更新用户自定义元数据

用于计算付款费用的WooCommerce管理编辑产品中的自定义复选框

将客户重定向到自定义感谢页后的空白页

不包括WooCommerce中单独销售的产品

当配置的URL为空时禁用Laravel Slack日志(log)记录

这是在可召回的背景下吗?

Laravel Carbon DiffForHumans选项(年,月,天)

Regex:高级删除单行PHP注释

$this->;db->;update();CodIgnitor中的功能不工作

Woocommerce 仅在前端为登录用户提供价格折扣

WooCommerce 中一件免费产品的数量折扣

AWS S3:当对象名称包含 % 时复制对象失败并出现错误 - 无效的复制源编码

PHP简单地将物品装在盒子里会导致未使用的空间,但还有剩余容量

在这个 Laravel 应用中,如何仅显示已批准的 comments 回复?

使用 RSA 2048 公钥验证 RSA PKCS#1v1.5 SHA 256 签名

如何在运行 update_post_meta 后更新 wp_wc_product_attributes_lookup 表?