【问题标题】:PHP & Jquery FileAPI - shared PHP server configurationPHP & Jquery FileAPI - 共享 PHP 服务器配置
【发布时间】:2013-12-29 00:47:13
【问题描述】:

我让我的用户使用jQuery FileAPI 裁剪和上传他们的图片。我从另一个页面用 jQuery 调用这个 PHP 文件。

在我的本地服务器上一切正常,但是在将其上传到我的生产(共享 - cPanel)服务器时,它不会创建文件。

您知道我是否需要在我的 cPanel 上进行更改或致电我的托管公司吗?

我尝试使用标题访问进行 tweeking,但没有任何效果。

这里是 PHP 文件:

<?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);
        }
    }
}
?>

【问题讨论】:

  • 您是否收到任何错误消息?此外,您应该删除错误抑制并适当地处理错误。
  • 嗨 Musa,感谢您的回答,我没有收到任何错误:/ 如何删除错误抑制?
  • 删除函数调用前的所有@
  • 文件有多大...典型的主机上传限制为 2 兆。有些让你超越,也可以使用分块上传来绕过它
  • 限制是 24mb,所以没问题。

标签: php jquery cpanel image-manipulation fileapi


【解决方案1】:

好的问题解决了!

显然我的主机不支持 mime_content_type。删除 Musa 推荐的错误抑制后,我可以捕捉到错误。

我要求我的主机启用我的 mime php 处理,现在一切正常。

干杯。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多