wmaraci reklam
lidertakipci

media_handle_upload ile resim yükleme sorunu

2 Mesajlar 532 Okunma
advertseo
wmaraci reklam

fdemir7 fdemir7 WM Aracı Kullanıcı
  • Üyelik 15.03.2016
  • Yaş/Cinsiyet 43 / E
  • Meslek Elektronik Öğretmeni
  • Konum Samsun
  • Ad Soyad F** D**
  • Mesajlar 93
  • Beğeniler 3 / 17
  • Ticaret 4, (%100)
Merhaba Arkadaşlar. Aşağıdaki kod ile html for ile dosya seçip, birden fazla dosyayı ortam kütüphanesine yüklüyorum.
Ancak sorunum şu: Bu işi html for kullanmadan yapmak istiyorum. Yani serverda "resimler/cicek.jpg" dosyasını ortam kütüphanesine yüklemek istiyorum. Yüklenecek dosyanın yolunu php dosyasının içine yazacağım. Bİr html formda dosya seç yükle demeden aynı işi yapmak istiyorum.

Çok araştırdım ama başaramadım. Yardımcı olursanız çok sevinirim.




require_once( '../wp-admin/includes/image.php' );
require_once( '../wp-admin/includes/file.php' );
require_once( '../wp-admin/includes/media.php' );
if ($_SERVER['REQUEST_METHOD'] == 'POST') {


$files = $_FILES["my_file_upload"];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("upload_file" => $file);
$attachment_id = media_handle_upload("upload_file", 0);

if (is_wp_error($attachment_id)) {
// There was an error uploading the image.
echo "Error adding file";
} else {
// The image was uploaded successfully!
echo "File added successfully with ID: " . $attachment_id . "
";
echo wp_get_attachment_image($attachment_id, array(800, 600)) . "
"; //Display the uploaded image with a size you wish. In this case it is 800x600
}
}
}
}
 

 

wmaraci
reklam

fdemir7 fdemir7 WM Aracı Kullanıcı
  • Üyelik 15.03.2016
  • Yaş/Cinsiyet 43 / E
  • Meslek Elektronik Öğretmeni
  • Konum Samsun
  • Ad Soyad F** D**
  • Mesajlar 93
  • Beğeniler 3 / 17
  • Ticaret 4, (%100)
Çözümü buldum: Servderdaki resmi ortam kütüphanesine ekliyor ve upload klasörüne atıyor.
$image_url ='aa.jpg';

$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 );
require_once('../wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
 

 

wmaraci
Konuyu toplam 1 kişi okuyor. (0 kullanıcı ve 1 misafir)
Site Ayarları
  • Tema Seçeneği
  • Site Sesleri
  • Bildirimler
  • Özel Mesaj Al