简体   繁体   English

GAE PHP提供云存储中的图像

[英]Gae php serve images from cloud storage

I am developing an application with php in app engine application. 我正在应用引擎应用程序中使用php开发应用程序。 This app is an image gallery. 这个程序是一个图片库。 I have my images to cloud storage and i want to retrieve them with a dynamic way. 我将图像保存到云存储中,并且希望以一种动态方式检索它们。 In general i upload photos to cloud storage from my app but i want to take them back. 通常,我从我的应用程序将照片上传到云存储中,但我想将其取回。 in my localhost i have my image name to a database with is connected with a file in my localhost (typical way). 在我的本地主机中,我将我的映像名称保存到数据库,并与本地主机中的文件连接(典型方式)。

How can i do a similar thing with cloud storage?how can i approach this solution? 如何使用云存储执行类似的操作?如何解决此问题?

Thank you! 谢谢!

I used this tutorial for users upload to cloud storage and this one for serving the images . 我用这个教程对用户上传到云存储和这一个服务于图像。 They worked and the pictures are in cloud. 他们工作了,照片在云端。 Now i want to create queries to the cloud storage to retrieve them eg.by id ,by name, all, sort them and more and associate them 现在我想创建对云存储的查询以检索它们,例如按ID,按名称,全部,对其进行排序以及更多并将其关联

here is my Photo Class in localhost. 这是我在本地主机的照片类。

class Photo extends Db_object {


    protected static $db_table = "photos";
    protected static $db_table_fields = array('id', 'title','caption', 'description','filename', 'alternate_text','type','size' );
    public $id;
    public $title;
    public $caption;
    public $description;
    public $alternate_text;
    public $filename;
    public $type;
    public $size;

    public $tmp_path;
    public $upload_directory = "images";
    public $errors = array();
    public $upload_errors_array = array(

    UPLOAD_ERR_OK           => "There is no error",
    UPLOAD_ERR_INI_SIZE     => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
    UPLOAD_ERR_FORM_SIZE    => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
    UPLOAD_ERR_PARTIAL      => "The uploaded file was only partially uploaded.",
    UPLOAD_ERR_NO_FILE      => "No file was uploaded.",               
    UPLOAD_ERR_NO_TMP_DIR   => "Missing a temporary folder.",
    UPLOAD_ERR_CANT_WRITE   => "Failed to write file to disk.",
    UPLOAD_ERR_EXTENSION    => "A PHP extension stopped the file upload."                   


);


// This is passing $_FILES['uploaded_file'] as an argument

    public function set_file($file) { 

        if(empty($file) || !$file || !is_array($file)) {
        $this->errors[] = "There was no file uploaded here";
        return false;

        }elseif($file['error'] !=0) {

        $this->errors[] = $this->upload_errors_array[$file['error']];
        return false;

        } else {


        $this->filename =  basename($file['name']);
        $this->tmp_path = $file['tmp_name'];
        $this->type     = $file['type'];
        $this->size     = $file['size'];

        }


}


    public function picture_path() {

        return $this->upload_directory.DS.$this->filename;
    }

    public function save() {

        if($this->id) {
            $this->update();

        } else {
            if(!empty($this->errors)) {
                return false;

            }

            if(empty($this->filename) || empty($this->tmp_path)){
                $this->errors[] = "the file was not available";
                return false;
            }

            $target_path = SITE_ROOT . DS . 'admin' . DS . $this->upload_directory . DS . $this->filename;


            if(file_exists($target_path)) {
                $this->errors[] = "The file {$this->filename} already exists";
                return false;


            }

            if(move_uploaded_file($this->tmp_path, $target_path)) {

                if( $this->create()) {

                    unset($this->tmp_path);
                    return true;

                }


            } else {

                $this->errors[] = "the file directory probably does not have permission";
                return false;

            }

        }

    }

    public function delete_photo() {

        if($this->delete()) {

            $target_path = SITE_ROOT.DS. 'admin' . DS . $this->picture_path();

            return unlink($target_path) ? true : false;


        } else {

            return false;


        }

    }

        public function comments() {

        return Comment::find_the_comments($this->id);

    }


    public static function display_sidebar_data($photo_id) {


        $photo = Photo::find_by_id($photo_id);


        $output = "<a class='thumbnail' href='#'><img width='100' src='{$photo->picture_path()}' ></a> ";
        $output .= "<p>{$photo->filename}</p>";
        $output .= "<p>{$photo->type}</p>";
        $output .= "<p>{$photo->size}</p>";

        echo $output;


    }


} // End of Class 

 ?>

My Db_object class In my localhost i use them and something similar i am searching! 我的Db_object类在我的本地主机中,我使用它们以及正在搜索的类似内容!

<?php 

class Db_object {


public $errors = array();
public $upload_errors_array = array(


    UPLOAD_ERR_OK           => "There is no error",
    UPLOAD_ERR_INI_SIZE     => "The uploaded file exceeds the upload_max_filesize directive in php.ini",
    UPLOAD_ERR_FORM_SIZE    => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
    UPLOAD_ERR_PARTIAL      => "The uploaded file was only partially uploaded.",
    UPLOAD_ERR_NO_FILE      => "No file was uploaded.",               
    UPLOAD_ERR_NO_TMP_DIR   => "Missing a temporary folder.",
    UPLOAD_ERR_CANT_WRITE   => "Failed to write file to disk.",
    UPLOAD_ERR_EXTENSION    => "A PHP extension stopped the file upload."                   


);

    public function set_file($file) { 

        if(empty($file) || !$file || !is_array($file)) {
        $this->errors[] = "There was no file uploaded here";
        return false;

        }elseif($file['error'] !=0) {

        $this->errors[] = $this->upload_errors_array[$file['error']];
        return false;

        } else {


        $this->user_image =  basename($file['name']);
        $this->tmp_path = $file['tmp_name'];
        $this->type     = $file['type'];
        $this->size     = $file['size'];


        }

}

    public static function find_all() {

        return static::find_by_query("SELECT * FROM " . static::$db_table . " ");


        }


    public static function find_by_id($id) {
        global $database;
        $the_result_array = static::find_by_query("SELECT * FROM " . static::$db_table . " WHERE id = $id LIMIT 1");

        return !empty($the_result_array) ? array_shift($the_result_array) : false;


        }

    public static function find_by_query($sql) {
        global $database;
        $result_set = $database->query($sql);
        $the_object_array = array();
        while($row = mysqli_fetch_array($result_set)) {

        $the_object_array[] = static::instantation($row);

        }

        return $the_object_array;

        }


    public static function instantation($the_record){

        $calling_class = get_called_class();


        $the_object = new $calling_class;


        foreach ($the_record as $the_attribute => $value) {

        if($the_object->has_the_attribute($the_attribute)) {

            $the_object->$the_attribute = $value;


            }


        }

        return $the_object;
    } 


    private function has_the_attribute($the_attribute) {

        // $object_properties = get_object_vars($this);

        // return array_key_exists($the_attribute, $object_properties);

        return property_exists($this, $the_attribute);


    }

    protected function properties() {

        $properties = array();

        foreach (static::$db_table_fields  as $db_field) {

            if(property_exists($this, $db_field)) {

                $properties[$db_field] = $this->$db_field;

            }

        }

        return $properties;

    }

        protected function clean_properties() {
        global $database;


        $clean_properties = array();


        foreach ($this->properties() as $key => $value) {

            $clean_properties[$key] = $database->escape_string($value);


        }

        return $clean_properties ;

    }

public function save() {

    return isset($this->id) ? $this->update() : $this->create();

    }

    public function create() {
        global $database;

        $properties = $this->clean_properties();

        $sql = "INSERT INTO " . static::$db_table . "(" . implode(",", array_keys($properties)) . ")";
        $sql .= "VALUES ('". implode("','", array_values($properties)) ."')";


        if($database->query($sql)) {

            $this->id = $database->the_insert_id();

            return true;

        } else {

            return false;


        }


    } // Create Method



    public function update() {
        global $database;


        $properties = $this->clean_properties();

        $properties_pairs = array();

        foreach ($properties as $key => $value) {
            $properties_pairs[] = "{$key}='{$value}'";
        }

        $sql = "UPDATE  " .static::$db_table . "  SET ";
        $sql .= implode(", ", $properties_pairs);
        $sql .= " WHERE id= " . $database->escape_string($this->id);

        $database->query($sql);

        return (mysqli_affected_rows($database->connection) == 1) ? true : false;



    } // end of the update method




        public function delete() { 
            global $database;


            $sql = "DELETE FROM  " .static::$db_table . "  ";
            $sql .= "WHERE id=" . $database->escape_string($this->id);
            $sql .= " LIMIT 1";

            $database->query($sql);

            return (mysqli_affected_rows($database->connection) == 1) ? true : false;


        }


        public static function count_all() {

            global $database;

            $sql = "SELECT COUNT(*) FROM " . static::$db_table;
            $result_set = $database->query($sql);
            $row = mysqli_fetch_array($result_set);

            return array_shift($row);


        }


        public function delete_photo() {


        if($this->delete()) {

            $target_path = SITE_ROOT.DS. 'admin' . DS . $this->picture_path();

            return unlink($target_path) ? true : false;


        } else {

            return false;


        }




    }


}

Seems like you would need to follow this guide and would involve doing SQL queries to get the data you put up there back. 似乎您需要遵循本指南,并且需要执行SQL查询以获取存放在该处的数据。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google App Engine:如何在 PHP 循环中从 Google 云存储提供上传的图像 - Google App Engine: How to serve uploaded images in a PHP loop from google cloud storage 通过 Google Cloud Storage 中的 PHP 服务器提供文件的最有效方法? - Most efficient way to serve files through a PHP server from Google Cloud Storage? 如何使用PHP将图像上传到谷歌云存储与GAE - how to upload a image to google cloud storage with GAE using php 如何从Google Cloud Storage提供SVG图像? - How can I serve an SVG image from Google Cloud Storage? 您如何通过本地GAE开发服务器访问云存储映像? - How do you access cloud storage images via local GAE development server? 如何从 PHP 表单将图像上传到 Google Cloud Storage? - How do I upload images to the Google Cloud Storage from PHP form? 如何从Google Cloud Storage获取图像并将其显示在 <img> 使用Laravel / PHP标记 - How to get images from Google Cloud Storage and show them in <img> tag using Laravel/PHP 在GAE PHP网站上提供Google Cloud Store中的文件 - Serving files from Google Cloud Store on a GAE PHP site PhP 与 GAE 和谷歌云 SQL - PhP with GAE and Google Cloud SQL 使用 Laravel 从 Google Cloud Storage 获取所有文件夹和图像 - Get all Folders and Images from Google Cloud Storage with Laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM