[英]PHP script downloads fine on Localhost but on live site outputs file
請原諒我是PHP的完全處女,我正在寫一個wordpress插件,允許某人向客戶出售代碼,然后讓該客戶訪問該網站,然后將該代碼輸入文本框並點擊提交。 然后,PHP腳本會檢查mysql中是否存在該代碼,它將啟動下載,因為它的銷售下載(zip中的照片)會獲取服務器上給定的文件名(不帶擴展名),然后將其輸出為該文件名保存為下載框,就像我完成代碼的功能一樣,然后我先在本地對其進行測試之后再在實時站點上對其進行測試...現在這是本地主機(xampp)上的問題,它開始下載並運行正常在直播網站上,它會執行以下操作:
http://www.ctwo12.com/output.png
這是我的代碼:
$fileonS = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/plugins/photo_dwn_man/downloads/" . $codeRResult;
//download file (NEEDS MORE LOOKING INTO THIS IS JUST THE BASICS)
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $codeOResult . '.zip');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($fileonS));
ob_clean();
flush();
readfile($fileonS);
exit;
希望你們能為您提供幫助或為我指明正確的方向,請也解釋一下,因為我在這里學習不要復制!
問候,亞當
這可能是由許多原因引起的。 最常見的兩個原因是:
您的服務器未啟用PHP
您的代碼正在使用php短標簽,並且服務器已將其關閉<?
vs <?php
檢查服務器的內部設置並查看是否啟用了PHP。 如果已啟用,請嘗試重新配置服務器和PHP。 如果問題仍然存在,則應在朋友的服務器上進行檢查,以檢查問題是否在服務器上。
正確,這就是我的解決方法...我必須將標頭部分添加到一個單獨的PHP文件中,並且在輸入並提交正確的代碼后,我調用了一些JavaScript來加載PHP並傳遞一些GET變量...
我的單獨文件包含:
<?php $getcodeOResult = $_GET['gcor']; $getcodeRResult = $_GET['gcsr']; $fileonS = $_SERVER['DOCUMENT_ROOT'] . "/wp-content/plugins/photo_dwn_man/downloads/" . $getcodeRResult; header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . $getcodeOResult . '.zip'); header('Content-Transfer-Encoding: binary'); header('Pragma: public'); header('Content-Length: ' . filesize($fileonS)); ob_clean(); flush(); readfile($fileonS); ?>
然后開始下載的功能如下:
function startDownload() { // This function handles the download start // Get filename you want user to download by getting the contents of dB row that matches the user input $theCodeOfile = $GLOBALS['wpdb']->get_col( "SELECT dwn_file FROM wp_photodwnman WHERE dwn_code='" . $GLOBALS['userCode'] . "'"); // Get the actual servers filename by getting the contents of dB row that matches the user input $theCodeRfile = $GLOBALS['wpdb']->get_col( "SELECT dwn_pseu FROM wp_photodwnman WHERE dwn_code='" . $GLOBALS['userCode'] . "'"); // join both results into a string and not an array $codeOResult = join("", $theCodeOfile); $codeRResult = join("", $theCodeRfile); $GLOBALS['wpdb']->query( "UPDATE wp_photodwnman SET dwn_count=dwn_count+1 WHERE dwn_code='" . $GLOBALS['userCode'] . "'"); // adds to variable the location and filename echo "<script>window.onload = function(){window.location.href='http://ctwo12photography.co.uk/wp-content/plugins/photo_dwn_man/dwnload.php?gcor=" . $codeOResult . "&gcsr=" . $codeRResult . "'}; </script>"; }
因此,我從不了解其為何如此運行,但這提供了解決方案!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.