doushadu0901 2015-07-17 05:53
浏览 25

AWS S3 PhPdolphin集成

So my goal was it to implement Amazon S3 image uploads to the PhPDolphin script, unfortunately I've run into a few Issues, if I add the code in the script just doesn't load and since the script doesn't have an error log I'm clueless as to what went wrong, for licensing reasons I'm unable to publish the entire script here but I will post a snipped of the affected area.

/includes/classes.php [Default (Just a small snippet of the 4000 lines of code within this file)]

function validateMessage($message, $image, $type, $value, $privacy) {
    // If message is longer than admitted
    if(strlen($message) > $this->message_length) {
        $error = array('message_too_long', $this->message_length);
    }
    // Define the switch variable
    $x = 0;
    if($image['name'][0]) {
        // Set the variable value to 1 if at least one image name exists
        $x = 1;
    }
    if($x == 1) {
        // If the user selects more images than allowed
        if(count($image['name']) > $this->max_images) {
            $error = array('too_many_images', count($image['name']), $this->max_images);
        } else {
            // Define the array which holds the value names
            $value = array();
            $tmp_value = array();
            foreach($image['error'] as $key => $err) {
                $allowedExt = explode(',', $this->image_format);
                $ext = pathinfo($image['name'][$key], PATHINFO_EXTENSION);
                if(!empty($image['size'][$key]) && $image['size'][$key] > $this->max_size) {
                    $error = array('file_too_big', fsize($this->max_size), $image['name'][$key]); // Error Code #004
                    break;
                } elseif(!empty($ext) && !in_array(strtolower($ext), $allowedExt)) {
                    $error = array('format_not_exist', $this->image_format, $image['name'][$key]); // Error Code #005
                    break;
                } else {
                    if(isset($image['name'][$key]) && $image['name'][$key] !== '' && $image['size'][$key] > 0) {
                        $rand = mt_rand();
                        $tmp_name = $image['tmp_name'][$key];
                        $name = pathinfo($image['name'][$key], PATHINFO_FILENAME);
                        $fullname = $image['name'][$key];
                        $size = $image['size'][$key];
                        $ext = pathinfo($image['name'][$key], PATHINFO_EXTENSION);
                        // $finalName = str_replace(',', '', $rand.'.'.$this->db->real_escape_string($name).'.'.$this->db->real_escape_string($ext));
                        $finalName = mt_rand().'_'.mt_rand().'_'.mt_rand().'.'.$this->db->real_escape_string($ext);

                        // Define the type for picture
                        $type = 'picture';

                        // Store the values into arrays
                        $tmp_value[] = $tmp_name;
                        $value[] = $finalName;

                        // Fix the image orientation if possible
                        imageOrientation($tmp_name);
                    }
                }
            }
            if(empty($error)) {
                foreach($value as $key => $finalName) {
                    move_uploaded_file($tmp_value[$key], '../uploads/media/'.$finalName);
                }
            }
            // Implode the values
            $value = implode(',', $value);
        }

And then my edited version that is supposed to upload the images automatically to Amazon S3.

/includes/classes.php [edited] (the s3 code is on the far bottom of the snippet)

function validateMessage($message, $image, $type, $value, $privacy) {
    // If message is longer than admitted
    if(strlen($message) > $this->message_length) {
        $error = array('message_too_long', $this->message_length);
    }
    // Define the switch variable
    $x = 0;
    if($image['name'][0]) {
        // Set the variable value to 1 if at least one image name exists
        $x = 1;
    }
    if($x == 1) {
        // If the user selects more images than allowed
        if(count($image['name']) > $this->max_images) {
            $error = array('too_many_images', count($image['name']), $this->max_images);
        } else {
            // Define the array which holds the value names
            $value = array();
            $tmp_value = array();
            foreach($image['error'] as $key => $err) {
                $allowedExt = explode(',', $this->image_format);
                $ext = pathinfo($image['name'][$key], PATHINFO_EXTENSION);
                if(!empty($image['size'][$key]) && $image['size'][$key] > $this->max_size) {
                    $error = array('file_too_big', fsize($this->max_size), $image['name'][$key]); // Error Code #004
                    break;
                } elseif(!empty($ext) && !in_array(strtolower($ext), $allowedExt)) {
                    $error = array('format_not_exist', $this->image_format, $image['name'][$key]); // Error Code #005
                    break;
                } else {
                    if(isset($image['name'][$key]) && $image['name'][$key] !== '' && $image['size'][$key] > 0) {
                        $rand = mt_rand();
                        $tmp_name = $image['tmp_name'][$key];
                        $name = pathinfo($image['name'][$key], PATHINFO_FILENAME);
                        $fullname = $image['name'][$key];
                        $size = $image['size'][$key];
                        $ext = pathinfo($image['name'][$key], PATHINFO_EXTENSION);
                        // $finalName = str_replace(',', '', $rand.'.'.$this->db->real_escape_string($name).'.'.$this->db->real_escape_string($ext));
                        $finalName = mt_rand().'_'.mt_rand().'_'.mt_rand().'.'.$this->db->real_escape_string($ext);

                        // Define the type for picture
                        $type = 'picture';

                        // Store the values into arrays
                        $tmp_value[] = $tmp_name;
                        $value[] = $finalName;

                        // Fix the image orientation if possible
                        imageOrientation($tmp_name);
                    }
                }
            }
            if(empty($error)) {


                foreach($value as $key => $finalName) {
                if (!class_exists('S3'))require_once('S3.php');

                //AWS access info
                if (!defined('awsAccessKey')) define('awsAccessKey', 'myaccesskey');
                if (!defined('awsSecretKey')) define('awsSecretKey', 'mysecretkey');


                //instantiate the class
                $s3 = new S3(awsAccessKey, awsSecretKey);
                    S3::outObject(
                    '$tmp_value[$key]',
                    'zepstrca',
                    '.$finalName);',
                    S3::ACL_PUBLIC_READ
                    array(),
                    array(),
                    S3::STORAGE_CLASS_STANDARD
                    );

            }
            // Implode the values
            $value = implode(',', $value);
        }

And yes I did add my own access and secret key :)

Any help would be greatly appreciated and will be credited!

Links to the products and API used in this:

[PhPDolphin] [S3.php API on Github]

  • 写回答

1条回答 默认 最新

  • douqihua6212 2015-07-17 06:53
    关注

    This is how you can use the library i am using, i hope it will fix your problems (make sure the folder on the bucket actually exists otherwise just upload something to the root of the bucket)

    require_once 'S3.php'; 
    $s3 = new S3(awsAccessKey, awsSecretKey);
    $s3->putObjectFile('/folderOnServer/picture.jpg', awsBucket, '/folderOnBucket/newName.jpg');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?