[英]Setting a variable upload path in jQuery File Upload plugin
我正在使用blueimp jQuery File Upload插件 ( 在此处提供文档 ),但是在包含配置文件(指示在各种计算机上的上载目录的位置)时遇到了麻烦。
之所以需要这样做,是因为本地开发服务器上的uploads文件夹的相对路径与生产服务器(使用Capistrano的/shared/
文件夹)上的相对路径不同。
因此,我们处理此问题的方法是在一个名为constants.php
的文件中设置一个常数,以表示各个计算机上的上载文件夹的路径,该文件从一个特定于机器的配置文件config.php
中读取。
然后,我在上载程序处理程序( UploaderHandler.php
)中包含constants.php
。
问题在于在UploaderHandler.php
包含文件会导致上传UploaderHandler.php
在开始上传后输出SyntaxError: Unexpected token <
( 这是屏幕截图 )。
知道这里发生了什么吗? 任何帮助表示赞赏! 如果需要,我可以澄清问题:)
相关代码摘录如下:
/includes/constants.php :
<?php
# Grab machine-specific config file
include('config.php');
# Setup Constants for Steven's Machine
if ($_ENV["machine"] == "steven"){
define('WEB_DIR', "c:/wamp/www/ourproject");
define('SHARED', "c:/wamp/www/ourproject/shared");
# Setup Constants for Andrew's Machine
} elseif($_ENV["machine"] == "andy"){
define('WEB_DIR', "/Applications/MAMP/htdocs/ourproject");
define('SHARED', "/Applications/MAMP/htdocs/ourproject/shared");
# Setup Constants for Other Machines
} else {
define('WEB_DIR', realpath($_SERVER["DOCUMENT_ROOT"]));
define('SHARED', "/srv/www/ourwebsite.com/htdocs/shared");
}
?>
/includes/config.php :
<?php
# Set the name of your machine
$_ENV["machine"] = "andy";
?>
/file-uploader/server/php/UploadHandler.php
<?php
include('../../../includes/constants.php');
class UploadHandler
{
... // Some code omitted
function __construct($options = null, $initialize = true, $error_messages = null) {
$this->options = array(
'script_url' => $this->get_full_url().'/',
'upload_dir' => SHARED.'/uploads/shipping/',
'upload_url' => WEB_DIR.'/uploads/shipping/',
'user_dirs' => false,
'mkdir_mode' => 0755,
'param_name' => 'files',
// Set the following option to 'POST', if your server does not support
// DELETE requests. This is a parameter sent to the client:
'delete_type' => 'DELETE',
'access_control_allow_origin' => '*',
'access_control_allow_credentials' => false,
'access_control_allow_methods' => array(
'OPTIONS',
'HEAD',
'GET',
'POST',
'PUT',
'PATCH',
'DELETE'
),
... // More code omitted
?>
开发者控制台输出:
<br />
<b>Notice</b>: Constant WEB_DIR already defined in <b>/Applications/MAMP/htdocs/ourproject/includes/constants.php</b> on line <b>13</b><br />
<br />
<b>Notice</b>: Constant SHARED already defined in <b>/Applications/MAMP/htdocs/ourproject/includes/constants.php</b> on line <b>14</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ourproject/includes/constants.php:13) in <b>/Applications/MAMP/htdocs/ourproject/file- uploader/server/php/UploadHandler.php</b> on line <b>1074</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ourproject/includes/constants.php:13) in <b>/Applications/MAMP/htdocs/ourproject/file- uploader/server/php/UploadHandler.php</b> on line <b>1074</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ourproject/includes/constants.php:13) in <b>/Applications/MAMP/htdocs/ourproject/file- uploader/server/php/UploadHandler.php</b> on line <b>1074</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ourproject/includes/constants.php:13) in <b>/Applications/MAMP/htdocs/ourproject/file- uploader/server/php/UploadHandler.php</b> on line <b>1074</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ourproject/includes/constants.php:13) in <b>/Applications/MAMP/htdocs/ourproject/file- uploader/server/php/UploadHandler.php</b> on line <b>1074</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ourproject/includes/constants.php:13) in <b>/Applications/MAMP/htdocs/ourproject/file- uploader/server/php/UploadHandler.php</b> on line <b>1074</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ourproject/includes/constants.php:13) in <b>/Applications/MAMP/htdocs/ourproject/file- uploader/server/php/UploadHandler.php</b> on line <b>1074</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ourproject/includes/constants.php:13) in <b>/Applications/MAMP/htdocs/ourproject/file- uploader/server/php/UploadHandler.php</b> on line <b>1074</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ourproject/includes/constants.php:13) in <b>/Applications/MAMP/htdocs/ourproject/file- uploader/server/php/UploadHandler.php</b> on line <b>1074</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/ourproject/includes/constants.php:13) in <b>/Applications/MAMP/htdocs/ourproject/file- uploader/server/php/UploadHandler.php</b> on line <b>1074</b><br />
{"files":[{"name":"photo (1). PNG","size":91006,"type":"image\/png","url":"\/Applications\/MAMP\/htdocs\/ourprojectuploads\/shipping\/photo%20%281%29. PNG","thumbnailUrl":"\/Applications\/MAMP\/htdocs\/ourprojectuploads\/shipping\/thumbnail\/photo%20%281%29. PNG","deleteUrl":"http:\/\/localhost:8888\/ourproject\/file-uploader\/server\/php\/?file=photo%20%281%29. PNG","deleteType":"DELETE"}]}
听起来好像从UploaderHandler返回的json字符串不是有效的json。 也许您会在输出中得到一些警告。
您可以使用浏览器开发人员工具进行检查。 输出将引导您进入正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.