簡體   English   中英

使用Symfony 3.4映射到圖片的網址

[英]Url to image using Symfony 3.4

我正在嘗試從URL下載圖像。 我想將其放入數據庫中並保存路徑。

我是針對用戶上傳但未從URL下載的圖像執行此操作的。

我所有的代碼如下:

控制器( $array_photo是具有很多URL的數組):

    $file = file_get_contents($array_photos[0]);
    if ($file != NULL) {
        $fileName = md5(uniqid()) . '.' . $file->guessExtension();

        $file->move(
            $this->getParameter('image_directory'), $fileName
        );
    }


var_dump($array_photos[0]) -> string(23) "http://photos..."

image_directory是Web中的目錄。

當我使用表單執行此操作時(我使用$file = $form->get('avatarPath')->getData();而不是$file = file_get_contents($array_photos[0]); ,但出現此錯誤:

在字符串上調用成員函數guessExtension()

如果我用'.png'替換guessExtension,則會出現此錯誤:

在字符串上調用成員函數move()

文件確實是一個字符串:

$file = file_get_contents($array_photos[0]); // returns string of image data

您只需要使用file_put_contents() :-)

http://php.net/manual/zh/function.file-put-contents.php

file_put_contents($file, '/path/to/save');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM