在jQuery File Upload插件中设置变量上传路径

时间:2013-12-02 22:51:41

标签: javascript php jquery ajax file-upload

我正在使用blueimp jQuery File Upload plugindocumentation here),但我遇到了包含配置文件(指示上传目录在各种计算机上的位置)的问题。

这是必要的原因是我们本地开发服务器上的uploads文件夹的相对路径与我们的生产服务器(使用Capistrano的/shared/文件夹)不同。

所以我们处理这个问题的方法是在一个名为constants.php的文件中设置一个常量来表示各种机器上传文件夹的路径,该文件从一个名为{{1的机器特定的配置文件中读取}}

然后我将config.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"}]}

1 个答案:

答案 0 :(得分:0)

听起来像UploaderHandler返回的json字符串并不是有效的json。也许你会在输出中收到一些警告。

您可以使用浏览器开发人员工具进行检查。输出应该引导你走向正确的方向。