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

Setting a variable upload path in jQuery File Upload plugin

本文关键字:变量 路径 设置 jQuery File upload 插件      更新时间:2024-01-23

我正在使用blueimp jQuery File Upload插件(此处为文档),但在包含配置文件(指示上传目录在各种机器上的位置)时遇到了问题。

之所以需要这样做,是因为我们本地开发服务器上的上传文件夹的相对路径与生产服务器上的不同(生产服务器使用Capistrano的/shared/文件夹)。

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

然后,我将constants.php包括在上传器处理程序(UploaderHandler.php)中。

将文件包含在UploaderHandler.php中的问题导致上传器在开始上传后输出SyntaxError: Unexpected token <(这里是屏幕截图)

知道这里发生了什么吗?感谢您的帮助!如果需要,我可以澄清问题:)

相关代码摘录如下:

/included/contents.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");
}
?>

/included/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。也许你在输出中得到了一些警告。

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