Question

I'm letting my users crop & upload their image with jQuery FileAPI. I'm calling this PHP file with jQuery from another page.

Everything works good on my local server, but when uploading it to my production (shared - cPanel) server, it does not create the file.

Do you know if there is something that I need to change on my cPanel or call my hosting company for?

I tried tweeking with header access but nothing works.

Here is the PHP file:

<?php include 'init.php'; ?>
<?php
if(logged_in() === false) {
 header('Location: login.php');
 exit();
} ?>
<?php
/**
 * FileAPI upload controller (example)
 */


include    'FileAPI.class.php';



if( $_SERVER['REQUEST_METHOD'] == 'OPTIONS' ){
    exit;
}


if( strtoupper($_SERVER['REQUEST_METHOD']) == 'POST' ){
    $files  = FileAPI::getFiles(); // Retrieve File List
    $images = array();


    // Fetch all image-info from files list
    fetchImages($files, $images);


    // JSONP callback name
    $jsonp  = isset($_REQUEST['callback']) ? trim($_REQUEST['callback']) : null;


    // JSON-data for server response
    $json   = array(
          'images'  => $images
        , 'data'    => array('_REQUEST' => $_REQUEST, '_FILES' => $files)
    );


    // Server response: "HTTP/1.1 200 OK"
    FileAPI::makeResponse(array(
          'status' => FileAPI::OK
        , 'statusText' => 'OK'
        , 'body' => $json
    ), $jsonp);
    exit;
}

function fetchImages($files, &$images, $name = 'file'){
    if( isset($files['tmp_name']) ){
        $filename = $files['tmp_name'];
        list($mime) = explode(';', @mime_content_type($filename));

        if( strpos($mime, 'image') !== false ){
            $size = getimagesize($filename);
            $base64 = base64_encode(file_get_contents($filename));

            $images[$name] = array(
                  'width'   => $size[0]
                , 'height'  => $size[1]
                , 'mime'    => $mime
                , 'size'    => filesize($filename)
                , 'dataURL' => 'data:'. $mime .';base64,'. $base64
            );

            $iWidth = $iHeight = 330; // desired image result dimensions
            $iJpgQuality = 100;

            // new unique filename
            $sTempFileName = 'userpics/' . md5(time().rand());

             // move uploaded file into cache folder
             move_uploaded_file($filename, $sTempFileName);

              // change file permission to 644
             @chmod($sTempFileName, 0644);

                    if (file_exists($sTempFileName) && filesize($sTempFileName) > 0) {
                        $aSize = getimagesize($sTempFileName); // try to obtain image info
                        if (!$aSize) {
                            @unlink($sTempFileName);
                            return;
                        }

                        // check for image type
                        switch($aSize[2]) {
                            case IMAGETYPE_JPEG:
                                $sExt = '.jpg';

                                // create a new image from file 
                                $vImg = @imagecreatefromjpeg($sTempFileName);
                                break;
                            case IMAGETYPE_GIF:
                                $sExt = '.gif';

                                // create a new image from file 
                                $vImg = @imagecreatefromgif($sTempFileName);
                                break;
                            case IMAGETYPE_PNG:
                                $sExt = '.png';

                                // create a new image from file 
                                $vImg = @imagecreatefrompng($sTempFileName);
                                break;
                            default:
                                @unlink($sTempFileName);
                                return;
                        }
                        $data = getimagesize($sTempFileName);
                        $width = $data[0];
                        $height = $data[1];
                        // create a new true color image
                        $vDstImg = @imagecreatetruecolor( $iWidth, $iHeight );

                        // copy and resize part of an image with resampling
                        imagecopyresampled($vDstImg, $vImg, 0, 0, 0, 0, $iWidth, $iHeight, $width, $height);
                        // define a result image filename
                        $sResultFileName = $sTempFileName . $sExt;

                        // output image to file
                        imagejpeg($vDstImg, $sResultFileName, $iJpgQuality);           


                        @unlink($sTempFileName);

                        $user_id = $_SESSION['user_id'];
                        add_guest_picture($user_id, $sResultFileName);

                       // return $sResultFileName;
                    }  


        }
    }
    else {
        foreach( $files as $name => $file ){
            fetchImages($file, $images, $name);
        }
    }
}
?>
Was it helpful?

Solution

Ok issue resolved!

Apparently mime_content_type was not support by my host. after removing error suppression recommended by Musa I could catch the error.

I asked for my host to enable my mime php handling and now everything works.

Cheers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top