PHP图像上传的问题

时间:2012-10-24 01:57:55

标签: php javascript jquery html5

我一直在创建一个HTML5 Drag& amp;丢弃图像上传器。一切都很好用Javascript方面的东西,但PHP让我疯了!

我已经能够创建一个脚本,可以在图像丢失时成功将图像放置在文件夹中,但是一旦它尝试为图像创建缩略图并将图像链接放入用户db表中所有人都去了锅。我已经连续几个小时坐在这里,尝试并试图无济于事,所以我相信现在格林尼治标准时间凌晨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);
}   

?>

1 个答案:

答案 0 :(得分:1)

您似乎向imagecreatefromunknown()函数传递了错误的路径。您传递的$imagePath等于$upload_dir,但您的图片目的地为$upload_dir.$pic['name']