PHP uses file_get_contents to download images using variables the input link is normal.

Source: Internet
Author: User
Keywords Php
(....用正则提取与拼凑链接的代码...)$urlimg='http://xxx.com';$img_file = file_get_contents($urlimg);file_put_contents('test.jpg', $img_file);

Re-write the question.
I use the above code to download pictures.
But file_get_contents ($URLIMG) wrote this directly and found that the download image failed.
But I use echo $urlimg output address, and then manually write the address file_get_contents (' http://xxx.com '), but normal.
The same is out of the $urlimg, but I wrote it manually, but no problem. is also a variable of plain text.
There is no place to find the problem.

----Update

I have been trying for several days on and off, and when I have just described the problem, I think there will be a coding problem. Various outputs look at what is in the variable. When the content of the $URLIMG variable is saved to the TXT file, it is found that the & symbol in the link is written when it is collected & , so direct access leads to an error. However, this output does not display a problem on the Web page. Once you have replaced it again, it will be fine.

The problem has been solved how to end the problem?

Reply content:

(....用正则提取与拼凑链接的代码...)$urlimg='http://xxx.com';$img_file = file_get_contents($urlimg);file_put_contents('test.jpg', $img_file);

Re-write the question.
I use the above code to download pictures.
But file_get_contents ($URLIMG) wrote this directly and found that the download image failed.
But I use echo $urlimg output address, and then manually write the address file_get_contents (' http://xxx.com '), but normal.
The same is out of the $urlimg, but I wrote it manually, but no problem. is also a variable of plain text.
There is no place to find the problem.

----Update

I have been trying for several days on and off, and when I have just described the problem, I think there will be a coding problem. Various outputs look at what is in the variable. When the content of the $URLIMG variable is saved to the TXT file, it is found that the & symbol in the link is written when it is collected & , so direct access leads to an error. However, this output does not display a problem on the Web page. Once you have replaced it again, it will be fine.

The problem has been solved how to end the problem?

Variable assignment in the buffer to see the contents of the PHP related OB
Paste a recently written QR code, please refer to

//开启缓存ob_start();$url = base64_encode($value);$imgPath ="http://pan.baidu.com/share/qrcode?w=280&h=280&url="$url; //png图片$origin = ImageCreateFromPNG($imgPath);//生成图片imagepng($origin);//存入变量$img = ob_get_contents(); $value = iconv("UTF-8", "GBK", $value);//写入文件$handle = fopen($dir_path.'/'.$value.'.png','w'); fwrite($handle, $img);fclose($handle);//关闭缓冲区并不输出,这里看具体需求调整ob_end_clean();

Using Curl

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.