PHP Gets a remote picture class instance

Source: Internet
Author: User

Example, a PHP get remote picture class. Code:

  1. if (! defined (' BasePath ')) exit (' No direct script access allowed ');
  2. /*
  3. * Remote access to picture class
  4. *
  5. * Requires the curl extension to be turned on
  6. * Simulates the PHP upload principle, creates a cache directory, and stores the remotely acquired files in the cache directory.
  7. */
  8. Class url_pic{
  9. protected $cache; Cache path
  10. Public function __construct ($cache = ")
  11. {
  12. if (!emptyempty ($cache))
  13. {
  14. $this->cache = $cache;
  15. }
  16. Else
  17. {
  18. $this->cache = ' uploads/cache/';
  19. }
  20. }
  21. Set the cache directory
  22. Public Function Set_cache ($cache = ")
  23. {
  24. if (!emptyempty ($cache))
  25. {
  26. $this->cache = $cache;
  27. }
  28. }
  29. /*
  30. * Get remote pictures to save files to the cache folder
  31. *
  32. * $url get a link to a remote file
  33. * $error
  34. * @return 777 Returns the folder cannot be created
  35. * @return file name in cache
  36. */
  37. Public Function Get_file ($url, $error =777)
  38. {
  39. $path = $this->build_folder ($this->cache);
  40. if ($path ==false) return $error;
  41. $curl = Curl_init ();
  42. Set the URL you need to crawl
  43. curl_setopt ($curl, Curlopt_url, $url);
  44. Set Header
  45. curl_setopt ($curl, Curlopt_header, 0);
  46. Sets the curl parameter, which requires the result to be saved to a string or output to the screen.
  47. curl_setopt ($curl, Curlopt_returntransfer, 1);
  48. Run Curl, request a Web page
  49. $file = curl_exec ($curl);
  50. Close URL Request
  51. Curl_close ($curl);
  52. Write a file to the obtained data
  53. $filename = $this->cache.date ("Ymdhis");
  54. if (Self::build_file ($file, $filename) ==false)
  55. {
  56. return false;
  57. }
  58. return $filename;
  59. }
  60. Create a folder
  61. Public Function Build_folder ($dir)
  62. {
  63. if (!is_dir ($dir))
  64. {
  65. if (!mkdir ($dir, 0777,true) | |!chmod ($DIR, 0777))
  66. {
  67. return false;
  68. }
  69. }
  70. return true;
  71. }
  72. /*
  73. * Move_uploaded_file method for moving files to simulate PHP
  74. *
  75. * $cache Cache file path
  76. * $filename the absolute path of the file name that needs to be generated
  77. *
  78. * @return $filename
  79. */
  80. Public Function Move_file ($cache, $filename)
  81. {
  82. $file = @file_get_contents ($cache);
  83. if (Self::build_file ($file, $filename) ==false)
  84. {
  85. return false;
  86. }
  87. Unlink ($cache); Clear Cached pictures
  88. return $filename;
  89. }
  90. /*
  91. * Generate Files
  92. * $file files or binary streams that need to be written
  93. * $newname the absolute path of the file name that needs to be generated
  94. */
  95. protected static function Build_file ($file, $filename)
  96. {
  97. $write = @fopen ($filename, "w");
  98. if ($write ==false)
  99. {
  100. return false;
  101. }
  102. if (fwrite ($write, $file) ==false)
  103. {
  104. return false;
  105. }
  106. if (fclose ($write) ==false)
  107. {
  108. return false;
  109. }
  110. return true;
  111. }
  112. }
Copy Code
  • 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.