How to debug a PHP file that is being called by AJ

2019-08-10 10:08发布

问题:

I have a php file for uploading a picture (it uploads the picture, creates its thumbnail and also adds watermark to the picture). The file is being called by AJAX.

It was working on on my localhost on Windows XP with WAMPSERVER 2.0. Now I have installed Windows Vista and suddenly it doesn't work properly (the picture gets uploaded but the thumbnail and watermark parts don't work).

I'm using exactly the same software to test the application on my local machine (WAMPSERVER 2.0) yet it doesn't work.

How to debug this file? Here's how it looks:

<?php

define('BASE_PATH', substr(dirname(dirname(__FILE__)), 0, -22));

// set the include path
set_include_path(BASE_PATH
                 . '/../../library'
                 . PATH_SEPARATOR
                 . BASE_PATH
                 . '/library'
                 . PATH_SEPARATOR
                 . get_include_path());

// autoload classes from the library
function __autoload($class) {
    include str_replace('_', '/', $class) . '.php';
}

$configuration = new Zend_Config_Ini(BASE_PATH
                                     . '/application'
                                     . '/configs/application.ini',
                                     'development');
$dbAdapter = Zend_Db::factory($configuration->database);
Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);

function _getTable($table)
{
    include BASE_PATH
    . '/application/modules/default/models/'
    . $table
    . '.php';
    return new $table();
}

$albums = _getTable('Albums');
$media = _getTable('Media');

if (false === empty($_FILES)) {

    $tempFile = $_FILES['Filedata']['tmp_name'];
    $extension = end(explode('.', $_FILES['Filedata']['name']));

    // insert temporary row into the database
    $data = array();
    $data['type'] = 'photo';
    $data['type2'] = 'public';
    $data['status'] = 'temporary';
    $data['user_id'] = $_REQUEST['user_id'];
    $paths = $media->add($data, $extension, $dbAdapter);

    // save the photo
    move_uploaded_file($tempFile,
                       BASE_PATH . '/public/' . $paths[0]);

    // create a thumbnail
    include BASE_PATH . '/library/My/PHPThumbnailer/ThumbLib.inc.php';
    $thumb = PhpThumbFactory::create('/' . $paths[0]);
    $thumb->adaptiveResize(85, 85);
    $thumb->save(BASE_PATH . '/public/' . $paths[1]);

    // add watermark to the bottom right corner
    $pathToFullImage = BASE_PATH . '/public/' . $paths[0];
    $size = getimagesize($pathToFullImage);
    switch ($extension) {
        case 'gif':
            $im = imagecreatefromgif($pathToFullImage);
            break;
        case 'jpg':
            $im = imagecreatefromjpeg($pathToFullImage);
            break;
        case 'png':
            $im = imagecreatefrompng($pathToFullImage);
            break;
    }
    if (false !== $im) {
        $white = imagecolorallocate($im, 255, 255, 255);
        $font = BASE_PATH . '/public/fonts/arial.ttf';
        imagefttext($im,
                    13, // font size
                    0, // angle
                    $size[0] - 132, // x axis (top left is [0, 0])
                    $size[1] - 13, // y axis
                    $white,
                    $font,
                    'HunnyHive.com');
        switch ($extension) {
            case 'gif':
                imagegif($im, $pathToFullImage);
                break;
            case 'jpg':
                imagejpeg($im, $pathToFullImage, 100);
                break;
            case 'png':
                imagepng($im, $pathToFullImage, 0);
                break;
        }
        imagedestroy($im);
    }

    echo "1";

}

回答1:

When debugging anything that is related to AJAX I would recommend the following:

  • Ensure that the file is returning the correct data without any AJAX wrapper around it. Call the file directly with some sample data. Does it return what you require? Does it have syntax errors? This is the first thing you want to check and it will save you a ton of headache.

  • Ensure your Javascript is parsing the data correctly. Your JS may be expecting JSON but you're returning XML, or your returned data is not formatted the way you think, or you may need to evaluate your returned data in Javascript so it can actually be used. Try some console.log()'s to test what your data looks like.

  • Try something like Postbin which lets you send POST data. Ensure your POST data is correct and you're sending the right data to your script.



回答2:

For generally debugging this, you should break the steps down into functions and test each part in turn.

With regard to Ajax debugging, you might like to try out Zend_Log_Writer_FirePHP. It's an extension for Firebug which reads extra data in headers sent by PHP, which means that the data in your response body doesn't contain debug output, and for things like images, can still be rendered.

Once you've installed FirePHP for Firebug, you can just write the following few lines:

$wFirebug = new Zend_Log_Writer_Firebug();

$firebug = new Zend_Log($wFirebug);

$firebug->info($myArray);

$firebug->info('Got to line 10');


回答3:

You could just create a simple form with a file input and just use that for easy testing.

That is the simplest way that I see, someone else may have something better in mind.



回答4:

I find that when working with an action being called via AJAX in ZF, it's always a good idea to make sure that the action works without AJAX first. That is, make your first iteration of developing the feature result in going to a new page to represent the action you're making.

Once you know that your PHP code works, you can then begin to worry about making the AJAX stuff work. In my opinion, at least, being able to output Zend_Debug::dump() on variables when you view another page is a lot easier for initial development.

AJAX by design creates a layer of opacity which can makes it difficult to do this. It gets even harder if you're interfacing with, for instance, a jQuery plugin that requires formatted data that you're just not getting for some reason. So again, PHP first, AJAX second. It takes roughly two seconds in ZF to go from a PHP to an AJAX call, and vice versa.



回答5:

The easiest solution would be to use FirePHP; install firebug + firephp addon for firefox and include the classes in your project.

(I keep the FirePHP library in /usr/share/php/ so I can include it easily in any project)

Then just do this:

require_once('/path/to/FirePHPCore/FirePHP.class.php');
$fp = FirePHP::getInstance(true);
$fp->log('you can put anything here, vars, objects, arrays, etc');

It will output the response in the FireBug console and is much better than polluting your code with echos and var_dumps when debugging ajax!



回答6:

Make the AJAX request with a callback function, which checks the data returned (echo'd) from the PHP function. If the data echo'd is some pre-determined success string ("success"?) then all is well, if it's not that, have the callback function output whatever is output by the function in an alert or something.