我使用wordpress作爲cms。我需要在每個帖子上提供一個zip文件下載[註冊用戶],其中包含該帖子附加的所有圖片。給定是僅來自主文件「gallery-zip.php」(original code and rest of the files here)的代碼。問題是它只適用於本地主機而不適用於活動服務器。代碼在localhost中運行良好,但不在活動網站上
插件代碼:
<?php
namespace GalleryZip;
add_action('plugins_loaded', __NAMESPACE__ . '\gallery_zip_start', 10, 0);
/**
* Invoke the plugin and load the needed classes
*/
function gallery_zip_start() {
// simple autoloader
$classes = glob(dirname(__FILE__) . '/classes/*.php');
if (! empty($classes)) {
foreach ($classes as $class)
require_once $class;
add_action('init', __NAMESPACE__ . '\add_hooks', 10, 0);
add_action('init', __NAMESPACE__ . '\enqueue_scripts', 10, 0);
if (is_admin())
return;
// this is only needed on the frontend
GalleryZip::get_instance(new GalleryZip_DataContainer());
}
}
/**
* Adding the needed hooks
*/
function add_hooks() {
add_action('wp_ajax_get_galleryzip', __NAMESPACE__ . '\get_gallery_zip', 10, 0);
add_action('wp_ajax_nopriv_get_galleryzip', __NAMESPACE__ . '\get_gallery_zip', 10, 0);
}
/**
* Enqueu the JavaScript
*/
function enqueue_scripts() {
// load minified version if SCRIPT_DEBUG is true
$min = (defined('SCRIPT_DEBUG') && true == SCRIPT_DEBUG) ? '' : '.min';
wp_enqueue_script(
'gallery-zip',
plugins_url(
sprintf('js/gallery_zip%s.js', $min),
__FILE__
),
array('jquery'),
false,
true
);
// set JS object with params
wp_localize_script('gallery-zip', 'GalleryZip', array('ajaxurl' => admin_url('admin-ajax.php')));
}
/**
* Ajax callback for creating the zip-file and sending the url to zip-file
*/
function get_gallery_zip() {
$send_result = function($result = '') {
if (is_array($result))
$result = var_export($result, true);
header('Content-type: application/json');
die(json_encode(array('result' => $result)));
};
$post_id = (int) filter_input(INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT);
$gallery_id = (int) filter_input(INPUT_POST, 'gallery_id', FILTER_SANITIZE_NUMBER_INT);
if (0 >= $post_id)
$send_result(var_export($_POST, true));
$images = GalleryZip::get_images_ajax_callback($post_id, $gallery_id);
$send_result($images);
}
測試環境:
- LOCALHOST:在PHP 5.3.10,並與最新的WordPress 3.6.1上運行。
- LIVE SITE:運行在PHP 5.3.27上,再次使用最新的wordpress 3.6.1。
我試過並做過的事情。 我使用最新的wordpress 3.6.1和PHP 5.3.10測試了本地主機上的插件,並在single.php模板<?php echo do_shortcode('[gallery]'); ?>
中使用了這行代碼。它提供了一個很好的zip下載鏈接「Get as Zip」,一切都完美無缺。
只要我在我的實時站點上安裝插件,它就會拋出一個錯誤,因爲該站點使用的是PHP 5.2的舊版本,特別是由於插件代碼第一行中的命名空間語法 - namespace GalleryZip;
,因此第一個我升級了PHP版本。
現在,我已經將它運行在實時站點上,它會按預期方式顯示「GalleryZip」鏈接,但鏈接不會提示zip下載,並且會在單擊時刷新頁面。
爲了排除我在最新的firefox,chrome和safari瀏覽器中測試的基本功能,按照預期在localhost上運行,而不是在live site上運行。相關的WordPress:我將主題改爲默認的二十三(在循環內外嘗試),並禁用所有其他插件,結果沒有變化。
螢火蟲控制檯顯示http://website.com/wp-admin/admin-ajax.php 200 OK
點擊鏈接,即使在現場。幾個星期前,我還向原始開發者尋求幫助,但尚未收到任何回覆。所以,如果你願意的話,請看看代碼,並告訴我是否可以解決這個問題,或者如果我在任何地方都是錯的,那麼我可以解決這個問題。另外請讓我知道如果我應該明確地問別的地方。儘管我一直在不斷地進行測試,並且盡我所能,但總是會感激不盡。
你的PHP errog日誌是否說有用?你的應用程序是否有權創建緩存目錄?內存不足嗎? –
@Ken Y-N關於錯誤 - 沒有發現任何有用或相關的東西。該插件在安裝「galleryzip-cache」時創建一個文件夾,目前設置的權限爲001,目錄中沒有任何內容。我猜想什麼都沒有用完,因爲我看不到包括錯誤日誌在內的任何通知。 – gurung
@ KenY-N我將權限更改爲777,並且完美運行。謝謝你指點我。問題 - 你會建議它建立什麼樣的許可來確保它的安全性。也請將它寫成答案,以便我可以接受。 – gurung