Remote image file to sqlite blob in PHP?

前端 未结 2 1634
有刺的猬
有刺的猬 2020-12-11 08:08

I have an image file stored on a remote server. I only have HTTP access to the server, so I\'m getting its content using file_get_contents(URL)

I need to store this

相关标签:
2条回答
  • 2020-12-11 08:50

    Don't do it. Every time you insert binary data into a database, God kills a kitten.
    Instead, store that image somewhere in the file system and save the path in your db.

    Remember to think of the kittens!

    alt text

    0 讨论(0)
  • 2020-12-11 08:54

    Concatenating data you have no control over in an SQL statement is a very bad idea. For instance the image data may contain a quotation mark that will terminate the string or a backslash that will be interpreted as a control character. Worst someone could build a fake image to injects malicious SQL code in your application.

    I suggest you use a prepared statement instead:

    $query = $db->prepare("INSERT INTO myTable (myImageBlob) VALUES (?)");
    $query->bindParam(1, fopen($filePath, "rb"), PDO::PARAM_LOB);
    $query->execute();
    

    Note that by passing PDO::PARAM_LOB to bindParam() you insert the blob's data from a stream. That's why I'm using fopen() instead of file_get_contents()

    0 讨论(0)
提交回复
热议问题