John Davidson

php - upload image to wordpress library using local location

0 comments
Message:


I need to upload images to wordpress using location of the image on the drive like this :


"C:\Users\me\Pictures\1.jpg"


without using file dialog to choose the image file because I need to upload image using url and its location on the drive


how to mod the code below to do what I need


<?php
require('../wp-load.php');
require_once('../wp-admin/includes/image.php');
//==========================================
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<p>
<label for="img_url">img_url</label>
<input type="text" name="img_url" id="img_url" />
</p>
<p>
<input type="submit" name="submit" id="submit" value="Submit" />
</p>
</form>
</body>
</html>';
//==========================================
if (isset($_POST["submit"])) {

$image_url = $_POST["img_url"];

$upload_dir = wp_upload_dir();

$image_data = file_get_contents( $image_url );

$filename = basename( $image_url );

if ( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
}
else {
$file = $upload_dir['basedir'] . '/' . $filename;
}

file_put_contents( $file, $image_data );

$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file );
ECHO $img_url = wp_get_attachment_url($attach_id);
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
}

?>


My Answer:
To upload an image to the WordPress library using the local location of the image on the drive, you can modify the code as follows:

php
require('../wp-load.php');
require_once('../wp-admin/includes/image.php');

if (isset($_POST["submit"])) {
$image_url = "C:\\Users\\me\\Pictures\\1.jpg"; // Specify the local location of the image on the drive

$upload_dir = wp_upload_dir();

$image_data = file_get_contents($image_url);

$filename = basename($image_url);

if (wp_mkdir_p($upload_dir['path'])) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}

file_put_contents($file, $image_data);

$wp_filetype = wp_check_filetype($filename, null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $file);
$img_url = wp_get_attachment_url($attach_id);
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);

echo "Image uploaded successfully. URL: " . $img_url;
}
?>





Upload Image
















In this code snippet, I have hardcoded the image URL to "C:\Users\me\Pictures\1.jpg" for demonstration purposes. You can modify this value to dynamically accept the image location from a form input or any other source. When the form is submitted, the image will be uploaded to the WordPress library using the specified local location.

Rate this post

3 of 5 based on 9040 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us