2

我一直在创建 HTML5 拖放图像上传器。Javascript 方面的一切都很好,但是 PHP 让我发疯了!

我已经能够创建一个脚本,该脚本在放置图像时成功地将图像放置在文件夹中,但是一旦它尝试为图像创建缩略图并将图像链接放入用户数据库表中,它就会全部转到锅。我已经在这里坐了好几个小时了,尝试并尝试无济于事,所以我相信现在是格林威治标准时间凌晨 3 点左右,我应该承认失败并寻求一点帮助。

JavaScript:

$(function(){

var dropbox = $('#dropbox'),
    message = $('.message', dropbox);

dropbox.filedrop({
    paramname:'pic',

    maxfiles: 5,
    maxfilesize: 200,
    url: 'uploadCore.php',

    uploadFinished:function(i,file,response){
        $.data(file).addClass('done');
    },

    error: function(err, file) {
        switch(err) {
            case 'BrowserNotSupported':
                showMessage('Your browser does not support HTML5 file uploads!');
                break;
            case 'TooManyFiles':
                alert('Too many files!');
                break;
            case 'FileTooLarge':
                alert(file.name+' is too large! Please upload files up to 200mb.');
                break;
            default:
                break;
        }
    },

    beforeEach: function(file){
        if(!file.type.match(/^image\//)){
            alert('Only images are allowed!');

            return false;
        }
    },

    uploadStarted:function(i, file, len){
        createImage(file);
    },

    progressUpdated: function(i, file, progress) {
        $.data(file).find('.progress').width(progress);
    }

});

var template = '<div class="preview">'+
                    '<span class="imageHolder">'+
                        '<img />'+
                        '<span class="uploaded"></span>'+
                    '</span>'+
                    '<div class="progressHolder">'+
                        '<div class="progress"></div>'+
                    '</div>'+
                '</div>'; 


function createImage(file){

    var preview = $(template), 
        image = $('img', preview);

    var reader = new FileReader();

    image.width = 100;
    image.height = 100;

    reader.onload = function(e){            
        image.attr('src',e.target.result);
    };

    reader.readAsDataURL(file);

    message.hide();
    preview.appendTo(dropbox);

    $.data(file,preview);
}

function showMessage(msg){
    message.html(msg);
}

});

现在对于 PHP:

<?php

// db connection
 include("db-info.php");
    $link = mysql_connect($server, $user, $pass);
        if(!mysql_select_db($database)) die(mysql_error());

 include("loadsettings.inc.php");


//$upload_dir = 'pictures/';
$allowed_ext = array('jpg','jpeg','png','gif');

if(strtolower($_SERVER['REQUEST_METHOD']) != 'post'){
    exit_status('Error! Wrong HTTP method!');
}


if(array_key_exists('pic',$_FILES) && $_FILES['pic']['error'] == 0 ){

if (isset($_SESSION["imagehost-user"]))
   { 
      $session = true;
      $username = $_SESSION["imagehost-user"];
      $password = $_SESSION["imagehost-pass"];

      $q = "SELECT id FROM `members` WHERE (username = '$username') and (password = '$password')";
      if(!($result_set = mysql_query($q))) die(mysql_error());
      $number = mysql_num_rows($result_set);

      if (!$number) {
         session_destroy();
         $session = false;
      }else {
         $row = mysql_fetch_row($result_set); 
         $loggedId = $row[0];
    }
}


    $date = date("d-m-y");
    $lastaccess = date("y-m-d");
    $ip = $_SERVER['REMOTE_ADDR'];
    $type = "public";

    $pic = $_FILES['pic'];
        $n = $pic;
            $rndName = md5($n . date("d-m-y") . time()) . "." . get_extension($pic['name']);
                $upload_dir = "pictures/" . $rndName;
                    move_uploaded_file($pic['tmp_name'], $upload_dir.$pic['name']);

                    // issues starts here

                     $imagePath = $upload_dir;

                     $img = imagecreatefromunknown($imagePath);
                        $mainWidth = imagesx($img);
                        $mainHeight = imagesy($img);

                        $a = ($mainWidth >= $mainHeight) ? $mainWidth : $mainHeight; 

                         $div = $a / 150;
                         $thumbWidth = intval($mainWidth / $div);
                         $thumbHeight = intval($mainHeight / $div);

                            $myThumb = imagecreatetruecolor($thumbWidth, $thumbHeight);
                             imagecopyresampled($myThumb, $img, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $mainWidth, $mainHeight);
                             $thumbPath = "thumbnails/" . basename($imagePath);
                             imagejpeg($myThumb, $thumbPath);

                             $details = intval(filesize($imagePath) / 1024) . " kb (" . $mainWidth . " x " . $mainHeight . ")" ; 
                             $id = md5($thumbPath . date("d-m-y") . time());


                             $q = "INSERT INTO `images`(id, userid, image, thumb, tags, details, date, access, type, ip)
                             VALUES('$id', '$loggedId', '$imagePath', '$thumbPath', '$tags', '$details', '$date', '$lastaccess', 'member-{$type}', '$ip')";
                                 if(!($result_set = mysql_query($q))) die(mysql_error());*/

                                    exit_status('File was uploaded successfuly!');

                            // to here


    $result = mysql_query("SELECT id FROM `blockedip` WHERE ip = '$ip'");
    $number = mysql_num_rows($result);
        if ($number) die(""); // blocked IP message

    function imagecreatefromunknown($path) {

        $exten = get_extension($path);

       switch ($exten) {
          case "jpg":
            $img = imagecreatefromjpeg($path);
            break;
          case "gif":
            $img = imagecreatefromgif($path);
            break;
          case "png":
            $img = imagecreatefrompng($path);
            break;
  }

  return $img;
}


}

exit_status('Something went wrong with your upload!');

// Helper functions

function exit_status($str){
    echo json_encode(array('status'=>$str));
    exit;
}

function get_extension($file_name){
    $ext = explode('.', $file_name);
    $ext = array_pop($ext);
    return strtolower($ext);
}   

?>
4

1 回答 1

1

似乎您将错误的路径传递给该imagecreatefromunknown()函数。你通过$imagePath等于$upload_dir,但你的图像目的地是$upload_dir.$pic['name']

于 2012-10-29T12:59:57.660 回答