Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; WP_MatchesMapRegex has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/class-wp.php on line 632

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Translation_Entry has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/entry.php on line 14

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; POMO_Reader has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/streams.php on line 12

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; POMO_FileReader has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/streams.php on line 120

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; POMO_StringReader has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/streams.php on line 175

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; POMO_CachedFileReader has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/streams.php on line 221

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; POMO_CachedIntFileReader has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/streams.php on line 236

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; WP_Widget_Factory has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/widgets.php on line 544

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzAdSense has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/easy-adsense-lite.php on line 38

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzAdsWidget has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/easy-adsense-lite.php on line 941

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzAdsSearch has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/easy-adsense-lite.php on line 1003

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzAdsLU has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/easy-adsense-lite.php on line 1078

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzBaseOption has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 22

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzCheckBox has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 174

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzRadioBox has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 199

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzSelect has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 250

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzMessage has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 294

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzHelpTag has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 313

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzHelpPopUp has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 328

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzTextArea has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 346

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzText has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 366

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzSubmit has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 385

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzColorPicker has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 409

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzOneTab has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 432

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzMiniTab has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 476
Auto download image from URL use PHP - PHP Cơ Bản

Auto download image from URL use PHP

Sau đây mình sẽ hướng dẫn các bạn cách tự download ảnh từ một link và lưu vào ổ cứng theo tên mong muốn bằng PHP.

1. Dùng file_get_contents() và file_put_contents()
Đầu tiên chúng ta cần tạo một file php, ở đây mình đặt tên là autodown.php
Ta cần tạo một form nhập dữ liệu bằng HTML.

Form có method=”post” và action=”autodown.php” để khi submit dữ liệu nhập vào sẽ được gửi đến và xử lý ở chính file autodown.php.

download anh tu url 1

Tự download ảnh trong một link và lưu vào ổ cứng theo tên mong muốn bằng PHP

Tiếp theo chúng ta viết code php xử lý việc down và lưu ảnh:

 

Ta dùng 2 hàm trong php đó là file_get_contents() và file_put_contents():
Hàm file_get_contents($url) dùng để lấy nội dung một file, trong đó $url là đường dẫn đến file cần get, nó có thể là đường link đến một trang bất kỳ và nó sẽ trả về nội dung html của trang web đó.

Hàm file_put_contents($path, &content) dùng để ghi nội dung cho một file, trong đó $path là đường dẫn đến file cần ghi, $content là nội dung bạn muốn ghi vào file.

Như ở đoạn code trên thì mình đã lưu ảnh vào một file có tên chính là Title nhập vào từ form và lưu cùng thư mục với file autodown.php.

VD, mình có một đường link có chứa một ảnh

download anh tu url 2

Tự download ảnh trong một link và lưu vào ổ cứng theo tên mong muốn – PHP

Mình copy đường link này và paste vào phần URL trong form nhập dữ liệu, phần Title mình nhập ech.jpg rồi ấn Submit, kết quả ta sẽ được một ảnh có tên ech.jpg lưu cùng thư mục với file autodown.php

tu download anh tu url 3

Tự download ảnh trong một link và lưu vào ổ cứng theo tên mong muốn bằng PHP

Bạn có thể tùy chỉnh $img_path để lưu ảnh vào thư mục mà mình mong muốn.

2. Dùng curl
Về curl các bạn có thể tham khảo chi tiết ở một bài viết khác trong diễn đàn
http://phpcoban.com/curl-trong-php/

getHeader function

download function

Usages

 

 

You may also like...

4 Responses

  1. long says:

    cái này đang không phải dùng Curl nhé

  2. long says:

    giả sử link là 1 bài viết thì làm sao lấy được ảnh trong bài viết. Em nên demo 1 cái như thế

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">