生成包含可变地址和文件的动态zip文件,并获取可下载链接
<p>我想将2个文件放入一个zip文件中,一个是thanks.txt,另一个是file.dat,使用以下代码,并且链接到这个zip文件,但是我的代码运行不正确,我需要帮助来纠正和优化这段代码。<br /><br />在thanks.txt文件中,将以下文本与客户的用户电子邮件一起放置:<br /><br />Hi, Dear '.$email_address.' Thanks for using it!<br /><br />我的代码:</p><p><strong></strong></p>
<pre class="brush:php;toolbar:false;">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;
}
}</pre>
<p><strong>并且我通过以下代码调用zip文件:</strong></p>
<p><code><a href="<?php echo create_zip_file();"> Download zip file </a></code></p>
<p>如果我在以下代码中使用静态地址($zip_file):</p>
<p><code>if ($zip->open($zip_file , ZipArchive::CREATE) === TRUE)</code></p>
<p>zip文件已创建,但是当我使用动态地址时,没有创建zip文件。</p>