Как загрузить изображения в WooCommerce [закрыто]Php

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 Как загрузить изображения в WooCommerce [закрыто]

Сообщение Anonymous »

Я пытаюсь загрузить изображения через WooCommerce. У меня есть функция, которой я передаю изображения с именем $files и идентификатор компании. проблема в том, что когда я отправляю запрос, ничего не происходит, и я ничего не вижу на своем бэкэнде WooCommerce. Я не знаю, использую ли я неправильный метод или это что-то другое.
Это функция для отправки изображений, и мои файлы приходят вот так: $images = $_FILES[ 'images'];:
public function uploadImages($files, $bID) {

$uploadedImages = [];
$errors = [];

foreach ($files as $file) {
if (empty($file)) {
$errors[] = 'No image found.';
continue;
}

if (preg_match('/^data:image\/(jpeg|png|gif|jpg|webp);base64,/', $file, $matches)) {
var_dump($file);
die;
$fileType = $matches[1];
$file = substr($file, strpos($file, ',') + 1);
$file = base64_decode($file);

if ($file === false) {
$errors[] = 'Error decoding image.';
continue;
}
} else {
$errors[] = 'Invalid image format.';
continue;
}

$uploadDir = wp_upload_dir();
$fileName = uniqid() . '.' . $fileType;
$filePath = trailingslashit($uploadDir['path']) . $fileName;
$fileURL = trailingslashit($uploadDir['url']) . $fileName;

if (!file_put_contents($filePath, $file)) {
$errors[] = 'Error saving image to the file system.';
continue;
}

$attachment = array(
'post_mime_type' => 'image/' . $fileType,
'post_title' => sanitize_file_name($fileName),
'post_content' => '',
'post_status' => 'inherit',
);

$attachmentID = wp_insert_attachment($attachment, $filePath, $bID);

if (is_wp_error($attachmentID)) {
$errors[] = 'Error inserting image into the database.';
continue;
}

require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');

$attachmentMetadata = wp_generate_attachment_metadata($attachmentID, $filePath);
wp_update_attachment_metadata($attachmentID, $attachmentMetadata);

$uploadedImages[] = $fileURL;
}

if (count($errors) > 0) {
return array(
'message' => 'error',
'status' => 500,
'data' => array(
'message' => 'Errors occurred during image upload.',
'errors' => $errors
)
);
}

// Update post meta with an array of image URLs
update_post_meta($bID, 'uploaded_images', $uploadedImages);

return array(
'message' => 'success',
'status' => 200,
'data' => array(
'message' => 'Images uploaded successfully.',
'urls' => $uploadedImages
)
);
}


Подробнее здесь: https://stackoverflow.com/questions/792 ... oocommerce
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»