简体   繁体   中英

Setting a variable upload path in jQuery File Upload plugin

I'm using the blueimp jQuery File Upload plugin ( documentation here ) but I'm having trouble including a config file (that indicates the location of the upload directory on various machines).

The reason this is necessary is the relative path to the uploads folder on our local development server is different than on our production server (which utilizes Capistrano's /shared/ folder).

So the way we've handled this is by setting a constant to represent the path to the upload folder on various machines, in a file called constants.php that reads from a machine-specific config file called config.php .

I then include constants.php in the uploader handler ( UploaderHandler.php ).

The problem is including the file in UploaderHandler.php causes the uploader to output SyntaxError: Unexpected token < after starting the upload ( here's a screenshot ).

Any idea what's going on here? Any help is appreciated! I can clarify the problem if needed :)

Relevant code excerpts below:

/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
?>            

Developer Console Output:

<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"}]}

Sounds like the returned json string from UploaderHandler isn't valid json. Maybe you get some warnings into the output.

You could check this with the browsers developer tools. The output should direct you into the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM