本文整理汇总了PHP中download_url函数的典型用法代码示例。如果您正苦于以下问题:PHP download_url函数的具体用法?PHP download_url怎么用?PHP download_url使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了download_url函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: media_sideload_image_1
function media_sideload_image_1($file, $post_id, $desc = null, $return = 'html')
{
if (!empty($file)) {
// Set variables for storage, fix file filename for query strings.
preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $file, $matches);
$file_array = array();
$file_array['name'] = basename($matches[0]);
// Download file to temp location.
$file_array['tmp_name'] = download_url($file);
// If error storing temporarily, return the error.
if (is_wp_error($file_array['tmp_name'])) {
return $file_array['tmp_name'];
}
// Do the validation and storage stuff.
$id = media_handle_sideload($file_array, $post_id, $desc);
// If error storing permanently, unlink.
if (is_wp_error($id)) {
@unlink($file_array['tmp_name']);
return $id;
}
/*$fullsize_path = get_attached_file( $id ); // Full path
if (function_exists('ewww_image_optimizer')) {
ewww_image_optimizer($fullsize_path, $gallery_type = 4, $converted = false, $new = true, $fullsize = true);
}*/
$src = wp_get_attachment_url($id);
}
if (!empty($src)) {
//update_post_meta($post_id, 'image_value', $src);
set_post_thumbnail($post_id, $id);
//update_post_meta($post_id, 'Thumbnail', $src);
return get_post_meta($post_id, 'image_value', true);
} else {
return new WP_Error('image_sideload_failed');
}
}
示例2: image
public static function image($post_id)
{
$images = array();
$xml = simplexml_load_file('https://unsplash.com/rss');
foreach ($xml->channel->item as $item) {
foreach ($item->image->url as $url) {
$images[] = (string) $url;
}
}
shuffle($images);
$url = $images[0];
$tmp = download_url($url);
$file_array = array('name' => 'new test image', 'tmp_name' => $tmp);
// Check for download errors
if (is_wp_error($tmp)) {
@unlink($file_array['tmp_name']);
}
$image_id = media_handle_sideload($file_array, $post_id);
// Check for handle sideload errors.
if (is_wp_error($image_id)) {
@unlink($file_array['tmp_name']);
}
if (is_wp_error($image_id)) {
return $image_id;
}
}
示例3: geoip_detect_update
function geoip_detect_update()
{
// TODO: Currently cron is scheduled but not executed. Remove scheduling.
if (get_option('geoip-detect-source') != 'auto') {
return;
}
$download_url = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz';
$download_url = apply_filters('geoip_detect2_download_url', $download_url);
$outFile = geoip_detect_get_database_upload_filename();
// Download
$tmpFile = download_url($download_url);
if (is_wp_error($tmpFile)) {
return $tmpFile->get_error_message();
}
// Ungzip File
$zh = gzopen($tmpFile, 'r');
$h = fopen($outFile, 'w');
if (!$zh) {
return __('Downloaded file could not be opened for reading.', 'geoip-detect');
}
if (!$h) {
return sprintf(__('Database could not be written (%s).', 'geoip-detect'), $outFile);
}
while (($string = gzread($zh, 4096)) != false) {
fwrite($h, $string, strlen($string));
}
gzclose($zh);
fclose($h);
unlink($tmpFile);
return true;
}
示例4: maxmindUpdate
public function maxmindUpdate()
{
require_once ABSPATH . '/wp-admin/includes/file.php';
$download_url = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz';
$download_url = apply_filters('geoip_detect2_download_url', $download_url);
$outFile = $this->maxmindGetUploadFilename();
// Download
$tmpFile = download_url($download_url);
if (is_wp_error($tmpFile)) {
return $tmpFile->get_error_message();
}
// Ungzip File
$zh = gzopen($tmpFile, 'r');
$h = fopen($outFile, 'w');
if (!$zh) {
return __('Downloaded file could not be opened for reading.', 'geoip-detect');
}
if (!$h) {
return sprintf(__('Database could not be written (%s).', 'geoip-detect'), $outFile);
}
while (($string = gzread($zh, 4096)) != false) {
fwrite($h, $string, strlen($string));
}
gzclose($zh);
fclose($h);
unlink($tmpFile);
return true;
}
示例5: getDownloadUrl
protected function getDownloadUrl()
{
global $wp_filesystem;
$this->skin->feedback('download_envato');
$package_filename = 'js_composer.zip';
$res = $this->fs_connect(array(WP_CONTENT_DIR));
if (!$res) {
return new WP_Error('no_credentials', __("Error! Can't connect to filesystem", 'js_composer'));
}
$username = WPBakeryVisualComposerSettings::get('envato_username');
$api_key = WPBakeryVisualComposerSettings::get('envato_api_key');
$purchase_code = WPBakeryVisualComposerSettings::get('js_composer_purchase_code');
if (empty($username) || empty($api_key) || empty($purchase_code)) {
return new WP_Error('no_credentials', __('Error! Envato username, api key and your purchase code are required for downloading updates from Envato marketplace for the Visual Composer. Visit <a href="' . admin_url('options-general.php?page=wpb_vc_settings&tab=updater') . '' . '">Settings</a> to fix.', 'js_composer'));
}
$json = wp_remote_get($this->envatoDownloadPurchaseUrl($username, $api_key, $purchase_code));
$result = json_decode($json['body'], true);
if (!isset($result['download-purchase']['download_url'])) {
return new WP_Error('no_credentials', __('Error! Envato API error' . (isset($result['error']) ? ': ' . $result['error'] : '.'), 'js_composer'));
}
$result['download-purchase']['download_url'];
$download_file = download_url($result['download-purchase']['download_url']);
if (is_wp_error($download_file)) {
return $download_file;
}
$upgrade_folder = $wp_filesystem->wp_content_dir() . 'upgrade_tmp/js_composer_envato_package';
if (is_dir($upgrade_folder)) {
$wp_filesystem->delete($upgrade_folder);
}
$result = unzip_file($download_file, $upgrade_folder);
if ($result && is_file($upgrade_folder . '/' . $package_filename)) {
return $upgrade_folder . '/' . $package_filename;
}
return new WP_Error('no_credentials', __('Error on unzipping package', 'js_composer'));
}
示例6: load_thumbnail
public function load_thumbnail($post_id, $url, $desc = null)
{
if (!empty($url)) {
// Download file to temp location
$tmp = download_url($url);
// Set variables for storage
// fix file filename for query strings
preg_match('/[^\\?]+\\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $url, $matches);
if (isset($matches[0])) {
$file_array['name'] = basename($matches[0]);
}
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if (is_wp_error($tmp)) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}
// do the validation and storage stuff
$id = media_handle_sideload($file_array, $post_id, $desc);
if (!is_wp_error($id)) {
return $id;
}
// If error storing permanently, unlink
@unlink($file_array['tmp_name']);
}
return false;
}
示例7: largo_media_sideload_image
/**
* Similar to `media_sideload_image` except that it simply returns the attachment's ID on success
*
* @param (string) $file the url of the image to download and attach to the post
* @param (integer) $post_id the post ID to attach the image to
* @param (string) $desc an optional description for the image
*
* @since 0.5.2
*/
function largo_media_sideload_image($file, $post_id, $desc = null)
{
if (!empty($file)) {
include_once ABSPATH . 'wp-admin/includes/image.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/media.php';
// Set variables for storage, fix file filename for query strings.
preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $file, $matches);
$file_array = array();
$file_array['name'] = basename($matches[0]);
// Download file to temp location.
$file_array['tmp_name'] = download_url($file);
// If error storing temporarily, return the error.
if (is_wp_error($file_array['tmp_name'])) {
return $file_array['tmp_name'];
}
// Do the validation and storage stuff.
$id = media_handle_sideload($file_array, $post_id, $desc);
// If error storing permanently, unlink.
if (is_wp_error($id)) {
@unlink($file_array['tmp_name']);
}
return $id;
}
}
示例8: import_settings
public function import_settings()
{
$file_url = base64_decode($_POST['file_url']);
$temporary_file = "";
try {
include_once ABSPATH . 'wp-admin/includes/file.php';
//Contains download_url
$temporary_file = download_url($file_url);
if (is_wp_error($temporary_file)) {
throw new Exception('Error: ' . $temporary_file->get_error_message());
} else {
if ($this->import_seo_settings($temporary_file)) {
$information['success'] = true;
} else {
throw new Exception(__('Settings could not be imported:', 'wordpress-seo'));
}
}
} catch (Exception $e) {
$information['error'] = $e->getMessage();
}
if (file_exists($temporary_file)) {
unlink($temporary_file);
}
MainWPHelper::write($information);
}
示例9: update_database
public static function update_database()
{
set_time_limit(0);
$outFile = self::get_upload_file_path();
// for download_url()
require_once ABSPATH . 'wp-admin/includes/file.php';
$tmpFile = \download_url(self::SOURCE_URL);
if (is_wp_error($tmpFile)) {
die($tmpFile->get_error_message());
}
$zh = gzopen($tmpFile, 'rb');
$h = fopen($outFile, 'wb');
if (!$zh) {
die('Downloaded file could not be opened for reading.');
}
if (!$h) {
die(sprintf('Database could not be written (%s).', $outFile));
}
while (!gzeof($zh)) {
fwrite($h, gzread($zh, 4096));
}
gzclose($zh);
fclose($h);
unlink($tmpFile);
if (!self::is_db_valid()) {
die(sprintf('Checksum does not match (%s).', $outFile));
}
}
示例10: create_file
private function create_file($name, $vararray)
{
$string = $this->generate_csv_string($vararray);
$upload_dir = wp_upload_dir();
$file = $upload_dir['path'] . "/" . $name . ".csv";
file_put_contents($file, $string);
$url = $upload_dir['url'] . "/" . $name . ".csv";
$tmp = download_url($url);
$post_id = $this->options[$this->page_id_field];
$file_array = array();
// Set variables for storage
// fix file filename for query strings
preg_match('/[^\\?]+\\.(jpg|jpe|jpeg|gif|png|csv)/i', $url, $matches);
$file_array['name'] = basename($matches[0]);
$file_array['tmp_name'] = $tmp;
// If error storing temporarily, unlink
if (is_wp_error($tmp)) {
print_r($tmp);
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}
// do the validation and storage stuff
$id = media_handle_sideload($file_array, $post_id, $desc);
// If error storing permanently, unlink
if (is_wp_error($id)) {
print_r($id);
@unlink($file_array['tmp_name']);
return $id;
}
$src = wp_get_attachment_url($id);
echo $src;
}
示例11: __invoke
/**
* Import images from Unsplash into your Media Library.
*
* ## OPTIONS
*
* [--count=<number>]
* : How many media items to generate. Default: 10
*
* [--media_author=<login>]
* : The author of the generated media. Default: none
*
* [--media_date=<yyyy-mm-dd|random>]
* : The date of the generated media. Default: current date
*
* [--media_dimensions=<dimensions>]
* : The dimensions of the generated media. Default: none
*
* ## EXAMPLES
*
* wp unsplash --count=10
* wp unsplash --media_date=random
* wp unsplash --media_dimensions=1080x720
*/
public function __invoke($args, $assoc_args = array())
{
$defaults = array('count' => 10, 'media_author' => false, 'media_date' => current_time('mysql'), 'media_dimensions' => false);
extract(array_merge($defaults, $assoc_args), EXTR_SKIP);
if ($media_author) {
$user_fetcher = new \WP_CLI\Fetchers\User();
$media_author = $user_fetcher->get_check($media_author)->ID;
}
$url = 'https://source.unsplash.com/random/';
if ($media_dimensions) {
$url .= $media_dimensions;
}
$notify = \WP_CLI\Utils\make_progress_bar('Generating media', $count);
for ($i = 0; $i < $count; $i++) {
$tmp_file = download_url($url);
if (!is_wp_error($tmp_file)) {
$this->_process_downloaded_image($tmp_file, $media_author, $media_date);
} else {
WP_CLI::warning('Could not download image from Unsplash API.');
}
if (file_exists($tmp_file)) {
unlink($tmp_file);
}
$notify->tick();
}
$notify->finish();
}
示例12: uploadImage
static function uploadImage($img_url)
{
include_once ABSPATH . 'wp-admin/includes/file.php';
//Contains download_url
//Download $img_url
$temporary_file = download_url($img_url);
if (is_wp_error($temporary_file)) {
throw new Exception('Error: ' . $temporary_file->get_error_message());
} else {
$upload_dir = wp_upload_dir();
$local_img_path = $upload_dir['path'] . DIRECTORY_SEPARATOR . basename($img_url);
//Local name
$local_img_url = $upload_dir['url'] . '/' . basename($img_url);
$moved = @rename($temporary_file, $local_img_path);
if ($moved) {
$wp_filetype = wp_check_filetype(basename($img_url), null);
//Get the filetype to set the mimetype
$attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($img_url)), 'post_content' => '', 'post_status' => 'inherit');
$attach_id = wp_insert_attachment($attachment, $local_img_path);
//Insert the image in the database
require_once ABSPATH . 'wp-admin/includes/image.php';
$attach_data = wp_generate_attachment_metadata($attach_id, $local_img_path);
wp_update_attachment_metadata($attach_id, $attach_data);
//Update generated metadata
return array('id' => $attach_id, 'url' => $local_img_url);
}
}
if (file_exists($temporary_file)) {
unlink($temporary_file);
}
return null;
}
示例13: wp_rp_upload_attachment
/**
* Cron - Thumbnail extraction
*/
function wp_rp_upload_attachment($url, $post_id)
{
/* Parts copied from wp-admin/includes/media.php:media_sideload_image */
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/media.php';
include_once ABSPATH . 'wp-admin/includes/image.php';
$tmp = download_url($url);
preg_match('/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i', $url, $matches);
$file_array['name'] = sanitize_file_name(urldecode(basename($matches[0])));
$file_array['tmp_name'] = $tmp;
if (is_wp_error($tmp)) {
@unlink($file_array['tmp_name']);
return false;
}
$post_data = array('guid' => $url, 'post_title' => 'rp_' . $file_array['name']);
$attachment_id = media_handle_sideload($file_array, $post_id, null, $post_data);
if (is_wp_error($attachment_id)) {
@unlink($file_array['tmp_name']);
return false;
}
$attach_data = wp_get_attachment_metadata($attachment_id);
$platform_options = wp_rp_get_platform_options();
$min_width = $platform_options['custom_size_thumbnail_enabled'] ? WP_RP_CUSTOM_THUMBNAILS_WIDTH : WP_RP_THUMBNAILS_WIDTH;
$min_height = $platform_options['custom_size_thumbnail_enabled'] ? WP_RP_CUSTOM_THUMBNAILS_HEIGHT : WP_RP_THUMBNAILS_HEIGHT;
if (!$attach_data || $attach_data['width'] < $min_width || $attach_data['height'] < $min_height) {
wp_delete_attachment($attachment_id);
return false;
}
return $attachment_id;
}
示例14: install
public function install($name)
{
if (!defined('FS_METHOD')) {
define('FS_METHOD', 'direct');
}
WP_Filesystem();
$params = array('apikey' => self::API_KEY, 'name' => $name, 'site' => get_site_url(), 'version' => BACKUP_TO_DROPBOX_VERSION);
$download_file = download_url("{$this->get_url(true)}/download?" . http_build_query($params));
if (is_wp_error($download_file)) {
$errorMsg = $download_file->get_error_message();
if ($errorMsg == 'Forbidden') {
$errorMsg = __('access is deined, this could be because your payment has expired.', 'wpbtd');
} else {
$errorMsg = __('you have exceeded your download limit for this extension on this site.', 'wpbtd');
}
throw new Exception(__('There was an error downloading your premium extension because', 'wpbtd') . " {$errorMsg}");
}
$result = unzip_file($download_file, EXTENSIONS_DIR);
if (is_wp_error($result)) {
$errorCode = $result->get_error_code();
$errorMsg = strtolower($result->get_error_message());
if (in_array($errorCode, array('copy_failed', 'incompatible_archive'))) {
$errorMsg = sprintf(__("'%s' is not writeable.", 'wpbtd'), EXTENSIONS_DIR);
}
throw new Exception(__('There was an error installing your premium extension because', 'wpbtd') . " {$errorMsg}");
}
unlink($download_file);
$extensions = get_option('wpb2d-premium-extensions');
$extensions[$name] = EXTENSIONS_DIR . 'class-' . str_replace(' ', '-', strtolower($name)) . '.php';
update_option('wpb2d-premium-extensions', $extensions);
include_once $extensions[$name];
return $this->get_instance($name);
}
示例15: setFeaturedImage
function setFeaturedImage($post_id, $url, $featuredImageTitle)
{
// Download file to temp location and setup a fake $_FILE handler
// with a new name based on the post_id
$tmp_name = download_url($url);
// echo $tmp_name;
$file_array['name'] = $post_id . '-thumb.jpg';
// new filename based on slug
$file_array['tmp_name'] = $tmp_name;
// If error storing temporarily, unlink
if (is_wp_error($tmp_name)) {
@unlink($file_array['tmp_name']);
$file_array['tmp_name'] = '';
}
// do validation and storage . Make a description based on the Post_ID
$attachment_id = media_handle_sideload($file_array, $post_id, 'Thumbnail for ' . $post_id);
// If error storing permanently, unlink
if (is_wp_error($attachment_id)) {
$error_string = $attachment_id->get_error_message();
@unlink($file_array['tmp_name']);
return;
}
// Set as the post attachment
$post_result = add_post_meta($post_id, '_thumbnail_id', $attachment_id, true);
// echo $post_result);
}