Thứ Năm, 26 tháng 12, 2024

Download file website khác về bằng url về host - code php

Link: https://php101.net/how-to/download-file-from-url-using-php/

$fileUrl = 'https://php101.net/wp-content/uploads/2021/12/php101-logo-horizontal-250x74-1.png';

// Initialize CURL
$ch = curl_init( $url );

// Get the file name and extension using basename()
$fileName = basename( $fileUrl );

// Specify file save path
$savePath = '/dir/' . $fileName;

// Initialize fopen pointer to store the file.
// 'w' is write mode, and 'b' is binary mode, can be excluded if the file is a text-based file
$fp = fopen( $savePath, 'wb' );

// Set the file transfer option and header to CURL
curl_setopt( $ch, CURLOPT_FILE, $fp );
curl_setopt( $ch, CURLOPT_HEADER, 0 );

// Execute the CURL session
curl_exec( $ch );

// Close the session after running to free resources
curl_close( $ch );

// Close file pointer
fclose( $fp ); 

Không có nhận xét nào:

Đăng nhận xét