如何在 WordPress 中自动化上传最新软件的构建和下载链接创建过程?
根据您的逻辑。
您正在尝试自动执行最新软件版本的下载过程。
您不想手动更新内容,只想将最新版本上传到/download/ 文件夹。 (仅使用 FTP 删除您的最新版本;仅此而已)
我会这样做:
参考这些问题:
Get the latest file addition in a directory
How to force file download with PHP
我提出了两个解决方案:第一个是两个单独的代码,第二个是内联代码。
仅用于教育目的
第一个解决方案:快速而简短的使用:
(您可能需要一种方法或插件来激活在 Widget 中运行的 PHP;这个插件有帮助 PHP Code Widget)
<?php
$path = "download/";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
echo '<a href="http://www.example.com/'. $path .$latest_filename.'">Download '. $latest_filename . '</a>';
?>
第二种解决方案:
(同样,您可能需要一种方法或插件来激活在 Widget 中运行的 PHP;这个插件有助于 PHP Code Widget)
A)在http://www.example.com/download.php中创建download.php
添加以下代码:
<?php
$path = "download";
$latest_ctime = 0; //ctime stands for creation time.
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
// echo $latest_filename; un-comment to debug
$file_url = 'http://www.example.com/download/'.$latest_filename;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url); // do the double-download-dance (dirty but worky)
?>
B) 在您的 WordPress HTML Widget 中添加以下代码
<?php
$path = "download";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
echo '<a href="http://www.example.com/download.php">Download '. $latest_filename . '</a>';
?>
进一步解释:
A) 负责自动下载最新的软件版本。
B) 负责显示最新版本名称和创建链接。
现在,您只需将一个文件上传到您的最新版本的 /download/ 文件夹(setup_1_0.zip、setup_1_1.zip、setup_1_2.zip ...等。建议的解决方案将检查创建日期,而不管文件的名字。)
重要提示:可以看到最新的文件检查器功能重复了两次;一次在download.php 和一次在 WordPress 小部件中。因为如果我们合并在一个文件中,我们会得到 header 已经发送的错误。
请回答您的问题?欢迎反馈。