如何从数据库中插入,检索多个图像

时间:2015-12-22 10:08:48

标签: php

这是给出图像路径的php代码, 我从另一个ios应用程序获取的多个图像,如何将图像插入到具有不同名称的数据库中并显示它?

<?php

$myparam = $_POST['userfile'];     //getting multiple image Here

//$mytextLabel= $_POST['filenames']   //getting textLabe Here
//echo $myparam;
//echo $mytextLabel;
$target_path  = "uploads/";
$target_path  = $target_path . basename( $_FILES['userfile']['name']);

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) {
    echo "Success ! The file has been uploaded";
    //insert query will run here right?
   //but how to get those multiple images and save it m not getting

} else{
    echo "There was an error uploading the file, please try again!";
}
?>

2 个答案:

答案 0 :(得分:0)

您可以使用class.upload.php 此类为您管理文件上载。简而言之,它管理上传的文件,并允许您对文件执行任何操作,特别是如果它是图像,并且可以根据需要进行多次操作。

这是在您的网站中快速集成文件上传的理想课程。如果文件是图像,您可以通过多种方式转换,调整大小,裁剪它。您还可以应用过滤器,添加边框,文本,水印等...例如,这就是图库脚本所需的全部内容。支持的格式为PNG,JPG,GIF和BMP。

这是一个示例类:

class UploadFile
{

    protected $uploadFileModel;

    public function __construct()
    {
        $this->uploadFileModel = new UploadFileModel();
    }

    public function insert($params)
    {
        $handle = new upload($params['file'], 'fa_IR');
        $handle->file_max_size = '20000000';
        $handle->allowed = array(
            'image/png',
            'image/jpeg',
            'image/gif');
        $handle->mime_check = TRUE;
        if ($handle->uploaded) {
            $handle->process($params['file_path']);
            if ($handle->processed) {
                $params['file_name'] = $handle->file_dst_name;
                // insertToDB is a function that insert data to database
                $insToDB = $this->insertToDB(array(
                    'title' => $params['title'],
                    'file_name' => $params['file_name'],
                    'path' => $params['path'],
                    'created_at' => $params['created_at']
                ));
                if ($insToDB !== FALSE) {
                    return intval($insToDB);
                } else {
                    unlink($params['file_path'] . $params['file_name']);
                    return FALSE;
                }
            }
        }
        return $handle->error;
    }

    public function insertToDB($params)
    {
        return $this->uploadFileModel->insert($params);
    }

    public function showForm()
    {
        $str = '<input type="file" id="fileToUpload" name="fileToUpload">';
        return $str;
    }
}

这是型号:

public function insert($params)
{
    $query = "INSERT INTO `upload` ( `title`, `file_name`, `path`, `status`, `created_at`, `updated_at` )
              VALUES( :title, :file_name, :path, :status, :created_at, :updated_at )";
    try{
        $stmt = $this->pdo->prepare($query);
        $stmt->bindParam(':title', $title, PDO::PARAM_STR);
        $stmt->bindParam(':file_name', $file_name, PDO::PARAM_STR);
        $stmt->bindParam(':path', $path, PDO::PARAM_STR);
        $stmt->bindParam(':status', $status, PDO::PARAM_INT);
        $stmt->bindParam(':created_at', $created_at, PDO::PARAM_STR);
        $stmt->bindParam(':updated_at', $updated_at, PDO::PARAM_STR);
        extract($params);
        $stmt->execute();
        return $this->pdo->lastInsertId();
    }catch(PDOException $e){
        echo $e->getMessage();
        return false;
    }catch(Exception $e){
        echo $e->getMessage();
        return false;
    }
}

答案 1 :(得分:0)

因为Ashish提供的数据/代码不多。我在我身边假装。

<强> ImageUpload.php

首先,您需要public class BuddyChatViewModel : INotifyPropertyChanged, BaseViewModel { private string chat; public string Chat { get { return chat; } set { chat = value; NotifyPropertyChanged("Chat"); } } //INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } } 上传图片。 其次,您需要将enctype="multipart/form-data"声明为数组类型。 (因为您要上传多张图片)

name

<强> SubmitImageUpload.php

<form action="SubmitImageUpload.php" method='POST' enctype="multipart/form-data" >
    <input type='file' name='userfile[]' multiple>
    <input type='submit' value="Submit Image">
</form>

并且,由于您没有向我们提供您的数据库结构,因此我不能假设您的数据库结构并编写用于检索的代码。