我想把两个文件,一个是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)
个
将创建压缩文件,但当我使用动态地址时,不会创建任何压缩文件