wordpress - Upload files directly from system to web server without using html form in php -


i reading excel file , inserting excel data wordpress post php code. have folder of images in system , want upload images wordpress , assign images post featured image 1 one. image name same post title in order related post image folder.

$dir = "/images/";  // open directory, , read contents if (is_dir($dir)){   if ($dh = opendir($dir)){     while (($file = readdir($dh)) !== false){       echo "filename:" . $file . "<br>";     }     closedir($dh);   } } 

this code used read directory, how can images name folder , upload server

the code used upload files wordpress media below

if ($_files) {                         foreach ($_files $file => $array) {                             if ($_files[$file]['error'] !== upload_err_ok) {                                 return "upload error : " . $_files[$file]['error'];                             }                             $attach_id = media_handle_upload( $file, $userid );                             $attach_src = wp_get_attachment_image_src( $attach_id );                          }                            }                     set_post_thumbnail( $listingid, $attach_id ); 

now want uploads files folder not $_files array

i have create code images name folder , upload wordpress media uploader.

$dir = plugin_dir_path( __file__ )."images/"; //image folder in wordpress plugin directory, can change own  $list = glob($dir."*.*",glob_nosort); // read images directory images arrays      //create $file array indexes show below $file["name"] =$list[0];// first image path , name $list[0] $file["type"] = 'image/jpeg'; $file["size"] = filesize( $file["name"] );// image size $file["tmp_name"] =$list[0]; $file["error"] =1;     //upload image wordpress upload folder wp_upload_bits() function      //but wp_upload_bits() function didn't return image id return image path , url.     //i want image id in order attach image id wordpress post featured image $upload_image = wp_upload_bits($list[0], null, file_get_contents($list[0]));      //get upload image id, use wp_handle_upload() function giving max file size error on side $upload_overrides = array( 'test_form' => false ); $movefile = wp_handle_upload( $file, $upload_overrides ); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -