產生包含可變地址和文件的動態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>