function __construct() { wpfb_loadclass('Download', 'Admin'); $dir = WPFB_Core::UploadDir() . '/.tmp/'; WPFB_Admin::Mkdir($dir); $test_files = array('banner.png' => 'https://wpfilebase.com/wp-content/blogs.dir/2/files/2015/03/banner_023.png', 'small.txt' => 'https://wpfilebase.com/robots.txt'); $this->local_files = array(); foreach ($test_files as $f => $u) { $fn = $dir . $f; $this->local_files[$f] = $fn; if (file_exists($fn)) { continue; } echo "Downloading test file {$u}\n"; WPFB_Download::SideloadFile($u, $fn); } }
public static function SideloadFile($url, $dest_file = null, $size_for_progress = 0) { //WARNING: The file is not automatically deleted, The script must unlink() the file. @ini_set('max_execution_time', '0'); @set_time_limit(0); require_once ABSPATH . 'wp-admin/includes/file.php'; if (!$url) { return array('error' => __('Invalid URL Provided.')); } if (empty($dest_file)) { // if no dest file set, create temp file $fi = self::GetRemoteFileInfo($url); if (empty($fi)) { return array('error' => sprintf(__('Could not get file information from %s!', WPFB), $url)); } if (!($dest_file = self::GetTmpFile($fi['name']))) { return array('error' => __('Could not create Temporary file.')); } } if ($size_for_progress >= self::$MIN_SIZE_FOR_PROGRESSBAR) { if (!class_exists('progressbar')) { include_once WPFB_PLUGIN_ROOT . 'extras/progressbar.class.php'; } $progress_bar = new progressbar(0, $size_for_progress, 300, 30, '#aaa'); echo "<p><code>" . esc_html($url) . "</code> ...</p>"; $progress_bar->print_code(); } else { $progress_bar = null; } wpfb_loadclass('Download'); $result = WPFB_Download::SideloadFile($url, $dest_file, $progress_bar); if (is_array($result) && !empty($result['error'])) { return $result; } return array('error' => false, 'file' => $dest_file); }