本文整理汇总了PHP中wp_upload_bits函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_upload_bits函数的具体用法?PHP wp_upload_bits怎么用?PHP wp_upload_bits使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_upload_bits函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: upload_file
/**
* @param strin $file_url
*
* @return int
*/
private function upload_file($file_url)
{
if (!filter_var($file_url, FILTER_VALIDATE_URL)) {
return false;
}
$contents = @file_get_contents($file_url);
if ($contents === false) {
return false;
}
$upload = wp_upload_bits(basename($file_url), null, $contents);
if (isset($upload['error']) && $upload['error']) {
return false;
}
$type = '';
if (!empty($upload['type'])) {
$type = $upload['type'];
} else {
$mime = wp_check_filetype($upload['file']);
if ($mime) {
$type = $mime['type'];
}
}
$attachment = array('post_title' => basename($upload['file']), 'post_content' => '', 'post_type' => 'attachment', 'post_mime_type' => $type, 'guid' => $upload['url']);
$id = wp_insert_attachment($attachment, $upload['file']);
wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $upload['file']));
return $id;
}
示例2: ql_upload
/**
* Receives and processes files uploaded through the personalization panel.
*
* @return void
* @since 1.0
*/
function ql_upload()
{
check_ajax_referer('quicklaunch-upload-file', '_wpnonce');
// Grab file details and content
$filename = $_FILES['file']['name'];
$bits = file_get_contents($_FILES['file']['tmp_name']);
// Make sure we have no errors
if ($_FILES['file']['error'] != 0) {
$data = array('msg' => 'Upload Error. Code ' . $_FILES['file']['error']);
$response = new QL_JSONResponse($data, false);
$response->output();
}
// Do upload
$upload = wp_upload_bits($filename, null, $bits);
// Delete the temp file
@unlink($_FILES['file']['tmp_name']);
// If we have no errors, return the URL of the image as a response
if ($upload['error'] === false) {
$data = array('url' => $upload['url']);
$response = new QL_JSONResponse($data);
$response->output();
} else {
$data = array('msg' => $upload['error']);
$response = new QL_JSONResponse($data, false);
$response->output();
}
}
示例3: do_save
public function do_save($return_val, $data, $module)
{
if (empty($data['attachment_url'])) {
return false;
}
$bits = file_get_contents($data['attachment_url']);
$filename = $this->faker->uuid() . '.jpg';
$upload = wp_upload_bits($filename, null, $bits);
$data['guid'] = $upload['url'];
$data['post_mime_type'] = 'image/jpeg';
// Insert the attachment.
$attach_id = wp_insert_attachment($data, $upload['file'], 0);
if (!is_numeric($attach_id)) {
return false;
}
if (!function_exists('wp_generate_attachment_metadata')) {
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once ABSPATH . 'wp-admin/includes/image.php';
}
// Generate the metadata for the attachment, and update the database record.
update_post_meta($attach_id, '_wp_attachment_metadata', wp_generate_attachment_metadata($attach_id, $upload['file']));
// Flag the Object as FakerPress
update_post_meta($attach_id, self::$flag, 1);
return $attach_id;
}
示例4: dswoddil_site_icon_upload
/**
* Upload theme default site icon
*
* @return void
*/
function dswoddil_site_icon_upload()
{
$image_name = 'site-icon';
if (!function_exists('wp_update_attachment_metadata')) {
require_once ABSPATH . 'wp-admin/includes/image.php';
}
if (!dswoddil_get_attachment_by_post_name('dswoddil-' . $image_name)) {
$site_icon = get_template_directory() . '/img/' . $image_name . '.png';
// create $file array with the indexes show below
$file['name'] = $site_icon;
$file['type'] = 'image/png';
// get image size
$file['size'] = filesize($file['name']);
$file['tmp_name'] = $image_name . '.png';
$file['error'] = 1;
$file_content = file_get_contents($site_icon);
$upload_image = wp_upload_bits($file['tmp_name'], null, $file_content);
// Check the type of tile. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype(basename($upload_image['file']), null);
$attachment = array('guid' => $upload_image['file'], 'post_mime_type' => $filetype['type'], 'post_title' => 'dswoddil-' . $image_name, 'post_content' => '', 'post_status' => 'inherit');
//insert wordpress attachment of uploaded image to get attachmen ID
$attachment_id = wp_insert_attachment($attachment, $upload_image['file']);
//generate attachment thumbnail
wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $upload_image['file']));
add_post_meta($attachment_id, '_wp_attachment_context', $image_name);
}
}
示例5: import
public function import($attachment)
{
$saved_image = $this->_return_saved_image($attachment);
if ($saved_image) {
return $saved_image;
}
// Extract the file name and extension from the url
$filename = basename($attachment['url']);
if (function_exists('file_get_contents')) {
$options = ['http' => ['user_agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux i686 on x86_64; rv:49.0) Gecko/20100101 Firefox/49.0']];
$context = stream_context_create($options);
$file_content = file_get_contents($attachment['url'], false, $context);
} else {
$file_content = wp_remote_retrieve_body(wp_safe_remote_get($attachment['url']));
}
if (empty($file_content)) {
return false;
}
$upload = wp_upload_bits($filename, null, $file_content);
$post = ['post_title' => $filename, 'guid' => $upload['url']];
$info = wp_check_filetype($upload['file']);
if ($info) {
$post['post_mime_type'] = $info['type'];
} else {
// For now just return the origin attachment
return $attachment;
//return new \WP_Error( 'attachment_processing_error', __( 'Invalid file type', 'elementor' ) );
}
$post_id = wp_insert_attachment($post, $upload['file']);
wp_update_attachment_metadata($post_id, wp_generate_attachment_metadata($post_id, $upload['file']));
update_post_meta($post_id, '_elementor_source_image_hash', $this->_get_hash_image($attachment['url']));
$new_attachment = ['id' => $post_id, 'url' => $upload['url']];
$this->_replace_image_ids[$attachment['id']] = $new_attachment;
return $new_attachment;
}
示例6: testImageEditOverwriteConstant
/**
* @ticket 32171
*/
public function testImageEditOverwriteConstant()
{
define('IMAGE_EDIT_OVERWRITE', true);
include_once ABSPATH . 'wp-admin/includes/image-edit.php';
$filename = DIR_TESTDATA . '/images/canola.jpg';
$contents = file_get_contents($filename);
$upload = wp_upload_bits(basename($filename), null, $contents);
$id = $this->_make_attachment($upload);
$_REQUEST['action'] = 'image-editor';
$_REQUEST['context'] = 'edit-attachment';
$_REQUEST['postid'] = $id;
$_REQUEST['target'] = 'all';
$_REQUEST['do'] = 'save';
$_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]';
$ret = wp_save_image($id);
$media_meta = wp_get_attachment_metadata($id);
$sizes1 = $media_meta['sizes'];
$_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":189,"h":322}}]';
$ret = wp_save_image($id);
$media_meta = wp_get_attachment_metadata($id);
$sizes2 = $media_meta['sizes'];
$file_path = dirname(get_attached_file($id));
foreach ($sizes1 as $key => $size) {
if ($sizes2[$key]['file'] !== $size['file']) {
$files_that_shouldnt_exist[] = $file_path . '/' . $size['file'];
}
}
foreach ($files_that_shouldnt_exist as $file) {
$this->assertFileNotExists($file, 'IMAGE_EDIT_OVERWRITE is leaving garbage image files behind.');
}
}
示例7: insert_attachment
/**
* Helper function: insert an attachment to test properties of.
*
* @param int $parent_post_id
* @param str path to image to use
* @param array $post_fields Fields, in the format to be sent to `wp_insert_post()`
* @return int Post ID of inserted attachment
*/
private function insert_attachment($parent_post_id = 0, $image = null, $post_fields = array())
{
$filename = rand_str() . '.jpg';
$contents = rand_str();
if ($image) {
// @codingStandardsIgnoreStart
$filename = basename($image);
$contents = file_get_contents($image);
// @codingStandardsIgnoreEnd
}
$upload = wp_upload_bits($filename, null, $contents);
$this->assertTrue(empty($upload['error']));
$type = '';
if (!empty($upload['type'])) {
$type = $upload['type'];
} else {
$mime = wp_check_filetype($upload['file']);
if ($mime) {
$type = $mime['type'];
}
}
$attachment = wp_parse_args($post_fields, array('post_title' => basename($upload['file']), 'post_content' => 'Test Attachment', 'post_type' => 'attachment', 'post_parent' => $parent_post_id, 'post_mime_type' => $type, 'guid' => $upload['url']));
// Save the data
$id = wp_insert_attachment($attachment, $upload['file'], $parent_post_id);
wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $upload['file']));
return $id;
}
示例8: upload_file
/**
* @param strin $file_url
*
* @return int
*/
private function upload_file($file_url)
{
if (!filter_var($file_url, FILTER_VALIDATE_URL)) {
return false;
}
$contents = @file_get_contents($file_url);
if ($contents === false) {
return false;
}
$upload = wp_upload_bits(basename($file_url), null, $contents);
if (isset($upload['error']) && $upload['error']) {
return false;
}
$type = '';
if (!empty($upload['type'])) {
$type = $upload['type'];
} else {
$mime = wp_check_filetype($upload['file']);
if ($mime) {
$type = $mime['type'];
}
}
$attachment = array('post_title' => basename($upload['file']), 'post_content' => '', 'post_type' => 'attachment', 'post_mime_type' => $type, 'guid' => $upload['url']);
$id = wp_insert_attachment($attachment, $upload['file']);
wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $upload['file']));
update_post_meta($id, '_tribe_importer_original_url', $file_url);
$this->maybe_init_attachment_guids_cache();
$this->maybe_init_attachment_original_urls_cache();
self::$attachment_guids_cache[get_post($id)->guid] = $id;
self::$original_urls_cache[$file_url] = $id;
return $id;
}
示例9: test_upload_directories_after_multiple_wpmu_delete_blog_with_ms_files
/**
* When a site is deleted with wpmu_delete_blog(), only the files associated with
* that site should be removed. When wpmu_delete_blog() is run a second time, nothing
* should change with upload directories.
*/
function test_upload_directories_after_multiple_wpmu_delete_blog_with_ms_files() {
$filename = rand_str().'.jpg';
$contents = rand_str();
// Upload a file to the main site on the network.
$file1 = wp_upload_bits( $filename, null, $contents );
$blog_id = $this->factory->blog->create();
switch_to_blog( $blog_id );
$file2 = wp_upload_bits( $filename, null, $contents );
restore_current_blog();
wpmu_delete_blog( $blog_id, true );
// The file on the main site should still exist. The file on the deleted site should not.
$this->assertTrue( file_exists( $file1['file'] ) );
$this->assertFalse( file_exists( $file2['file'] ) );
wpmu_delete_blog( $blog_id, true );
// The file on the main site should still exist. The file on the deleted site should not.
$this->assertTrue( file_exists( $file1['file'] ) );
$this->assertFalse( file_exists( $file2['file'] ) );
}
示例10: set_ec_fun
function set_ec_fun()
{
echo '<h2>Settings</h2>';
if (isset($_GET['msg']) && $_GET['msg'] == 1) {
echo '<h4>Setttings saved !</h4>';
}
echo '<form method="post" enctype="multipart/form-data">
<ul>
<li><input type="file" name="add_to_cart" /></li>
<li><img src="' . site_url('/') . 'wp-content/' . get_option('ec_cart_img') . '" id="img"/></li>
<li><input type="submit" value="Upload" /></li>
</ul>
</form>';
if (!empty($_FILES['add_to_cart']['name'])) {
global $wpdb;
if (!empty($_FILES)) {
$FName = time . '_' . $_FILES["add_to_cart"]["name"];
$upload = wp_upload_bits($FName, null, file_get_contents($_FILES["add_to_cart"]["tmp_name"]));
$upload_dir = wp_upload_dir();
$img = 'uploads' . $upload_dir['subdir'] . '/' . $FName;
update_option('ec_cart_img', $img);
echo '<script type="text/javascript">window.location="admin.php?page=set_ec&msg=1"</script>';
}
}
}
示例11: save_images
function save_images($image_url, $post_id, $i)
{
//set_time_limit(180); //每个图片最长允许下载时间,秒
$file = file_get_contents($image_url);
$fileext = substr(strrchr($image_url, '.'), 1);
$fileext = strtolower($fileext);
if ($fileext == "" || strlen($fileext) > 4) {
$fileext = "jpg";
}
$savefiletype = array('jpg', 'gif', 'png', 'bmp');
if (!in_array($fileext, $savefiletype)) {
$fileext = "jpg";
}
$im_name = date('YmdHis', time()) . $i . mt_rand(10, 20) . '.' . $fileext;
$res = wp_upload_bits($im_name, '', $file);
if (isset($res['file'])) {
$attach_id = insert_attachment($res['file'], $post_id);
} else {
return;
}
if (ot_get_option('auto_save_image_thumb') == 'on' && $i == 1) {
set_post_thumbnail($post_id, $attach_id);
}
return $res;
}
示例12: test_get_space_used_main_site
/**
* Directories of sub sites on a network should not count against the same spaced used total for
* the main site.
*/
function test_get_space_used_main_site()
{
$space_used = get_space_used();
$blog_id = $this->factory->blog->create();
switch_to_blog($blog_id);
// We don't rely on an initial value of 0 for space used, but should have a clean space available
// so that we can remove any uploaded files and directories without concern of a conflict with
// existing content directories in src.
while (0 != get_space_used()) {
restore_current_blog();
$blog_id = $this->factory->blog->create();
switch_to_blog($blog_id);
}
// Upload a file to the new site.
$filename = rand_str() . '.jpg';
$contents = rand_str();
wp_upload_bits($filename, null, $contents);
restore_current_blog();
delete_transient('dirsize_cache');
$this->assertEquals($space_used, get_space_used());
// Switch back to the new site to remove the uploaded file.
switch_to_blog($blog_id);
$upload_dir = wp_upload_dir();
$this->remove_added_uploads();
$this->delete_folders($upload_dir['basedir']);
restore_current_blog();
}
示例13: fetch_remote_file
function fetch_remote_file($url, $post)
{
global $url_remap;
// extract the file name and extension from the url
$file_name = basename($url);
// get placeholder file in the upload dir with a unique, sanitized filename
$upload = wp_upload_bits($file_name, 0, '', $post['upload_date']);
if ($upload['error']) {
return new WP_Error('upload_dir_error', $upload['error']);
}
// fetch the remote url and write it to the placeholder file
$headers = wp_get_http($url, $upload['file']);
// request failed
if (!$headers) {
@unlink($upload['file']);
return new WP_Error('import_file_error', __('Remote server did not respond', 'wordpress-importer'));
}
// make sure the fetch was successful
if ($headers['response'] != '200') {
@unlink($upload['file']);
return new WP_Error('import_file_error', sprintf(__('Remote server returned error response %1$d %2$s', 'wordpress-importer'), esc_html($headers['response']), get_status_header_desc($headers['response'])));
}
$filesize = filesize($upload['file']);
if (isset($headers['content-length']) && $filesize != $headers['content-length']) {
@unlink($upload['file']);
return new WP_Error('import_file_error', __('Remote file is incorrect size', 'wordpress-importer'));
}
if (0 == $filesize) {
@unlink($upload['file']);
return new WP_Error('import_file_error', __('Zero size file downloaded', 'wordpress-importer'));
}
// keep track of the old and new urls so we can substitute them later
$url_remap[$url] = $upload['url'];
return $upload;
}
示例14: test_wp_ajax_send_attachment_to_editor_should_return_a_link
/**
* @ticket 36578
*/
public function test_wp_ajax_send_attachment_to_editor_should_return_a_link()
{
// Become an administrator
$post = $_POST;
$user_id = self::factory()->user->create(array('role' => 'administrator', 'user_login' => 'user_36578_administrator', 'user_email' => 'user_36578_administrator@example.com'));
wp_set_current_user($user_id);
$_POST = array_merge($_POST, $post);
$filename = DIR_TESTDATA . '/formatting/entities.txt';
$contents = file_get_contents($filename);
$upload = wp_upload_bits(basename($filename), null, $contents);
$attachment = $this->_make_attachment($upload);
// Set up a default request
$_POST['nonce'] = wp_create_nonce('media-send-to-editor');
$_POST['html'] = 'Bar Baz';
$_POST['post_id'] = 0;
$_POST['attachment'] = array('id' => $attachment, 'post_title' => 'Foo bar', 'url' => get_attachment_link($attachment));
// Make the request
try {
$this->_handleAjax('send-attachment-to-editor');
} catch (WPAjaxDieContinueException $e) {
unset($e);
}
// Get the response.
$response = json_decode($this->_last_response, true);
$expected = sprintf('<a href="%s" rel="attachment wp-att-%d">Foo bar</a>', get_attachment_link($attachment), $attachment);
// Ensure everything is correct
$this->assertTrue($response['success']);
$this->assertEquals($expected, $response['data']);
}
示例15: test_insert_image_no_thumb
function test_insert_image_no_thumb()
{
$filename = dirname(__FILE__) . '/../mock/img/cat.jpg';
$contents = file_get_contents($filename);
$upload = wp_upload_bits(basename($filename), null, $contents);
print_r($upload['error']);
$this->assertTrue(empty($upload['error']));
$attachment_id = $this->_make_attachment($upload);
$attachment_url = wp_get_attachment_image_src($attachment_id, "large");
$attachment_url = $attachment_url[0];
$c1 = '<p><img src="' . $attachment_url . '" alt="1559758083_cef4ef63d2_o" width="771" height="475" class="alignnone size-large" /></p>
<h2>Headings</h2>
<p>Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Sed posuere consectetur est at lobortis. Nulla vitae elit libero, a pharetra augue. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Donec sed odio dui.</p>';
$c1final = '<h2>Headings</h2>
<p>Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Sed posuere consectetur est at lobortis. Nulla vitae elit libero, a pharetra augue. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Donec sed odio dui.</p>';
$c2 = '<p><img src="' . $attachment_url . '" alt="1559758083_cef4ef63d2_o" width="771" height="475" class="alignnone size-medium" /></p>
<h2>Headings</h2>
<p>Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Sed posuere consectetur est at lobortis. Nulla vitae elit libero, a pharetra augue. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Donec sed odio dui.</p>';
$c2final = '<p><img src="' . $attachment_url . '" alt="1559758083_cef4ef63d2_o" width="771" height="475" class="alignnone size-medium" /></p>
<h2>Headings</h2>
<p>Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Sed posuere consectetur est at lobortis. Nulla vitae elit libero, a pharetra augue. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Donec sed odio dui.</p>';
$post_id = $this->factory->post->create();
add_post_meta($post_id, '_thumbnail_id', $attachment_id);
$this->go_to("/?p={$post_id}");
$final1 = largo_remove_hero($c1);
$final2 = largo_remove_hero($c2);
$this->assertEquals($c1final, $final1);
$this->assertEquals($c2final, $final2);
}