Upload time of posts posted using php in Wordpress increasing - php

We are uploading custom posts to our wordpress site using the code below. Basically the posts are a folder containing 10-15 image files which are being recursively retrieved from the C:/******/S1/ folder. A post is created using the name of the folder and the contents, being image files are created as attachment types and associated with that particular post. We are using the groups plugin to handle access control.
The problem is when we first started using this code we could upload 100 custom types in 14-15 mins. But the time is consistently increasing and now it takes around 30-35 mins for the same number of posts, the size and number remaining the same. We are not sure how long this time will reach as we continue to upload posts, we intend to create around 30000 posts over a period of a month. Hence the time is a critical factor. We have uploaded around 3000 till now.
We are not sure why this time period is increasing and we need to keep it constant. We have currently hosted this application on a 4 GB RAM, 2.6Ghz 2 core processor system. Currently posts table has around 53000 entries and post_meta has around 70000 entries. Any suggestions/advice are most welcome!!
Thanks in advance!!
function upload_service_register_call_func2() {
echo "<form id=\"featured_upload\" method=\"post\" action =\"/wordpress/index.php/upload-sr-status-v2/\" />";
echo "<input type=\"text\" name=\"my_image_upload\" value=\"C:/******/S1/\" id=\"my_image_upload\" />";
echo"<input id=\"submit_my_image_upload\" name=\"submit_my_image_upload\" type=\"submit\" value=\"Upload\" />";
echo"</form>";
}
function upload_service_register_func2() {
$filename = $_POST[my_image_upload];
$results = array();
$directory1 = $filename;
$handler1 = opendir($directory1);
$uploads_dir=wp_upload_dir();
// open directory and walk through the filenames
while ($file1 = readdir($handler1)) {
if ($file1 != "." && $file1 != "..") {
$directory = $filename."/".$file1;
$handler = opendir($directory);
$myproducts = get_posts( array(
'post_type' => 'se******rs',
'meta_query' => array(
array(
'key' => 's*******o',
'value' => $file1
)
)
) );
$uploaddir = wp_upload_dir();
if (file_exists($uploaddir['path'] . '/'. 'ST' . '/' . $file1)) {
delete_directory($uploaddir['path'] . '/'. 'ST' . '/' . $file1);
}
if(!empty($myproducts)){
foreach($myproducts as $myproduct)
{
wp_delete_post( $myproduct->ID, true);
}
}
$post["id"] = wp_insert_post( array(
"post_title" => $file1,
"post_content" => "",
"post_type" => "se******rs",
"post_status" => "publish",
'meta_query' => array(
array(
'key' => 's*******o',
'value' => $file1
)
)));
$content="";
$files = array();
while ($file = readdir($handler)) {
if ($file != "." && $file != "..") {
array_push($files,$file);
}
}
natsort($files);
$count=0;
foreach($files as $file)
{
$date = date_create();
$timestamp = date_timestamp_get($date);
$foldercreate = wp_mkdir_p($uploaddir['path'] . '/'. 'ST' . '/' . $file1);
$uploadfile = $uploaddir['path'] . '/'. 'ST' . '/' . $file1. '/' .$timestamp $file;
$contents= file_get_contents($directory."/".$file);
$savefile = fopen($uploadfile, 'w');
fwrite($savefile, $contents);
fclose($savefile);
if($count==0)
{
copy('C:\********************\uploads\STORE\index.html',$uploaddir['path'] . '/'. 'ST' . '/' . $file1 . '/index.html' );
copy('C:\********************\uploads\STORE\.htaccess',$uploaddir['path'] . '/'. 'ST' . '/' . $file1 . '/.htaccess');
}
$count++;
$parent_post_id = $post["id"];
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $file ), null );
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] .'/'. 'ST'. '/' . $file1. '/' . basename( $uploadfile ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $uploadfile, $parent_post_id );
// 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.
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
$image = $wp_upload_dir['url'] .'/'. 'ST'. '/' . $file1. '/' . basename( $uploadfile );
$content = $content."<img style=\"width:150px;height:150px\"; class=\"alignleft size-thumbnail wp-image-".$attach_id."\" src=\"".$image."\" />";
}
$content=$content."";
$my_post = array(
'ID' => $post["id"],
'post_content' => $content,
);
wp_update_post($my_post);
$group_id=get_group_id_by_name( $file1 );
if(!$group_id) $group_id=4;
add_post_meta($post["id"],"groups-read",$group_id);
add_post_meta($post["id"],"groups-read","2");
add_post_meta($post["id"],"s******o",$file1);
$em***=$file1;
$sql2="SELECT de**,bi******* FROM b**********8 WHERE em*** = %s";
$results2=prdt_run_sql($sql2,$em***);
if($results2)
{
foreach($results2 as $data)
{
$de**='SR_'.$data->de**;
$bi******='SR_'.$data->bi*******;
break;
}
$group_id=get_group_id_by_name($de**);
add_post_meta($post["id"],"groups-read",$group_id);
$group_id=get_group_id_by_name($bi*****);
add_post_meta($post["id"],"groups-read",$group_id);
echo "Uploaded folder ".$file1." with permissions<br/>";
}
else
{
echo "Uploaded folder ".$file1." without permissions<br/>";
}
}
}
echo"Completed Uploading folders <br/>";
}

Related

WordPress plugin development : How to upload a file to the default "uploads" directory?

I'm trying to upload files to the default "wp-content/uploads" directory. Have tried almost all the available tags, still not working.
Few of the codes I tried:
$filename = $_FILES["uploadfile"]["name"];
$tempname = $_FILES["uploadfile"]["tmp_name"];
$folder= 'wp_upload_dir()'. $filename; //This code is uploading the file to the "wp-admin" directory
if(move_uploaded_file($tempname, $folder)){$response}
$uploaddir = 'uploads/'; $uploadfile = $uploaddir . $filename;`
if(move_uploaded_file($tempname, $uploadfile))
`
I've coded to save the image in uploads folder for one of my website. please check if this code helps
//taking image in $name_icon
$name_icon = $_POST['myfile'];
// WordPress environment
require( dirname(__FILE__) . '/../../../wp-load.php' );
$wordpress_upload_dir = wp_upload_dir();
$i = 1; // number of tries when the file with the same name is already exists
$profilepicture = $_FILES['myfile'];
$new_file_path = $wordpress_upload_dir['path'] . '/' . $profilepicture['name'];
$new_file_mime = mime_content_type( $profilepicture['tmp_name'] );
if( empty( $profilepicture ) )
die();
if( $profilepicture['error'] )
die( $profilepicture['error'] );
if( $profilepicture['size'] > wp_max_upload_size() )
die( 'It is too large than expected.' );
if( !in_array( $new_file_mime, get_allowed_mime_types() ) )
die( 'WordPress doesn\'t allow this type of uploads.' );
while( file_exists( $new_file_path ) ) {
$i++;
$new_file_path = $wordpress_upload_dir['path'] . '/' . $i . '_' . $profilepicture['name'];
}
// looks like everything is OK
if( move_uploaded_file( $profilepicture['tmp_name'], $new_file_path ) ) {
$upload_id = wp_insert_attachment( array(
'guid' => $new_file_path,
'post_mime_type' => $new_file_mime,
'post_title' => preg_replace( '/\.[^.]+$/', '', $profilepicture['name'] ),
'post_content' => '',
'post_status' => 'inherit'
), $new_file_path );
// wp_generate_attachment_metadata() won't work if you do not include this file
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate and save the attachment metas into the database
wp_update_attachment_metadata( $upload_id, wp_generate_attachment_metadata( $upload_id, $new_file_path ) );
Tested & works

How to add Woocommerce Product Gallery using Gravity Forms

I am trying to add WooCommerce Product Gallery using Gravity forms multiple file upload but product gallery should not be added
add_action( 'gform_after_submission_3', 'set_post_content', 10, 2 );
function set_post_content( $entry, $form ) {
if (!function_exists('wp_generate_attachment_metadata')) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
}
$uploaded_files = json_decode(rgpost("gform_uploaded_files"));
foreach($uploaded_files->input_76 as $key => $data){
if (isset($_FILES[$data])) {
$file_url = $data->input_76; //great but what is its url?
$upload_dir = wp_upload_dir(); //where do you want to put it?
$file_data = file_get_contents($file_url); //show me what you're made of
$filename = basename($file_url); //so cute but what's its name?
if (wp_mkdir_p($upload_dir['path'])) //can we put it there?
$file = $upload_dir['path'] . '/' . $filename; //yes great
else //or no, okay fine let's try somewhere else
$file = $upload_dir['basedir'] . '/' . $filename; //get the whole location
file_put_contents($file, $file_data); // tada home at last
//$wp_filetype = wp_check_filetype($filename, array('pdf' => 'application/pdf','pdf' => 'application/x-pdf') ); //is it the right type of of file?
$attachment = array(//set up the attachment
//'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $file, $entry['post_id']); //insert attachment
$attach_data = wp_generate_attachment_metadata($attach_id, $file); //asign the meta
wp_update_attachment_metadata($attach_id, $attach_data); //update the post
update_post_meta($entry['post_id'], '_product_image_gallery', $attach_id);
}
}
}

WordPress import big number of images from server and associate with post

I was wondering if it is possible to speed up the existing import process I created for a wordpress site? I need to import the data from external xml file, create products based on that data, and dowmload the images and assign them to created products. The problem is that thumbnails also need to be generated and associated to the products via wp functions. I currently manage to insert 1 to 2 images per sec, the script is called every 5 min using cron job and limit for import is 120 images. That equals to approx 80 to 150 sec for a block of 120 images. Tre problem is that i need to import around 10000 products with 200000 images.
in tmp folder per time is approx 1000 images
Current functionality that i use is:
function upload_image(){
ini_set("memory_limit","2048M");
add_filter('intermediate_image_sizes', function ($image_sizes){
return array('thumbnail');
},1000 );
if($this->ids===false) {
self::get_all_ids();
}
$upload_dir = wp_upload_dir();
$root = $upload_dir['basedir'].DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR;
$handle = opendir($root);
$files=array();
$propertys=array();
$propertys_images=array();
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$tmp_data=explode('_',$entry);
if(isset($tmp_data[1])) {
$post_id = isset($this->ids[$tmp_data[1]]) ? $this->ids[$tmp_data[1]] : 0;
if (!empty($post_id)) {
$propertys[$post_id] = $post_id;
$files[] = array('url' => $root . $entry, 'post_id' => $post_id, 'name' => $entry);
}else{
unlink( $root . $entry);
}
}else{
unlink( $root . $entry);
}
}
}
if (empty($propertys)){return'';}
global $wpdb;
$results = $wpdb->get_results("
SELECT ID,post_parent, post_title
FROM $wpdb->posts AS posts
WHERE post_type = 'attachment'
AND post_parent IN(".implode(',',$propertys).")
Order by ID asc
",ARRAY_A ) ;
$set_images=array();
foreach ($results as $r){
$set_images[]=$r['post_title'];
$propertys_images[$r['post_parent']][$r['ID']]=$r['ID'];
}
foreach ($files as $k => $d){
if(in_array($d['name'],$set_images)){
unset($files[$k]);
unlink($d['url']);
}
}
require_once( ABSPATH . 'wp-admin/includes/image.php' );
foreach ($files as $id_of_i => $f){
if($id_of_i > 120){continue;}
$image_url = $f['name'];
$post_id=$f['post_id'];
$base_path=$upload_dir['basedir'].DIRECTORY_SEPARATOR.'tmp' .DIRECTORY_SEPARATOR.$image_url;
$filename = basename( $image_url );
if ( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . '/' . $filename;
}
else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
if(file_exists($base_path)) {
rename($base_path, $file);
//copy($base_path, $file);
}
$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_parent' => $post_id,
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file );
$propertys_images[$post_id][$attach_id]=$attach_id;
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
foreach ($propertys_images as $prop_id => $images){
$images_ids=array_values($images);
set_post_thumbnail($prop_id, $images_ids[0]);
update_post_meta($prop_id, '_thumbnail_id', $images_ids[0]);
update_post_meta($prop_id, 'fave_prop_slider_image', $images_ids[0]);
$num=get_post_meta($prop_id,'numb_of_images',true);
delete_post_meta($prop_id,'fave_property_images');
foreach ($images_ids as $k =>$id){
if($k!='0'){
add_post_meta($prop_id, 'fave_property_images', $id);
}
}
if($num<=count($images_ids)){
update_post_meta($prop_id, 'all_images_imp', 'true');
}
}
exit();
}
First i set to only generate thumbnail of this images,
then i collect all product_codes( "get_all_ids()" )
then i get all images from tmp folder and check if product for that img exist,
then get all attachment for products whose images i have
then i migrate image to wp upload path
then call "wp_insert_attachment" to add base image,
then using "wp_generate_attachment_metadata" i generate a thumbnail
and then that information i append to attachment using "wp_update_attachment_metadata"
received attachment ids i add to product and product gallery's
i think that in this script the slowed part is generating a thumbnail
is there any possibility to speed up this process, or if you have any suggestion
with current speed it will took approx 5 - to 10 days to add all the images
Thank you in advance
Problem was EWWW Image Optimizer
so you need to watch if you have some kind of optimizer active between your plugins.
when i disabled plugin (disconnect a hook) speed of inserting images increase a loot

How to differ two uploads in one form with same upload handling?

I have already created a form for my plugin, and it has two upload fields; one for an image and one for a zip-file. They are both using the same upload handler, and I want to save the attachment ID's to the database. The problem is that they use the same upload handler, so the value of the variable with the attachment ID will always be the last upload field. How is the best way to do this? Save in array (first index is first field, second index is second field)? Two upload handler is probably a bit overkill. Any ideas how to solve this in a good way?
This is the function that handles the upload:
function releases_action(){
global $wpdb;
// Upload cover
$uploadfiles = $_FILES['uploadfiles'];
if (is_array($uploadfiles)) {
foreach ($uploadfiles['name'] as $key => $value) {
// look only for uploded files
if ($uploadfiles['error'][$key] == 0) {
$filetmp = $uploadfiles['tmp_name'][$key];
//clean filename and extract extension
$filename = $uploadfiles['name'][$key];
// get file info
// #fixme: wp checks the file extension....
$filetype = wp_check_filetype( basename( $filename ), null );
$filetitle = preg_replace('/\.[^.]+$/', '', basename( $filename ) );
$filename = $filetitle . '.' . $filetype['ext'];
$upload_dir = wp_upload_dir();
/**
* Check if the filename already exist in the directory and rename the
* file if necessary
*/
$i = 0;
while ( file_exists( $upload_dir['path'] .'/' . $filename ) ) {
$filename = $filetitle . '_' . $i . '.' . $filetype['ext'];
$i++;
}
$filedest = $upload_dir['path'] . '/' . $filename;
/**
* Check write permissions
*/
if ( !is_writeable( $upload_dir['path'] ) ) {
$this->msg_e('Unable to write to directory %s. Is this directory writable by the server?');
return;
}
/**
* Save temporary file to uploads dir
*/
if ( !#move_uploaded_file($filetmp, $filedest) ){
$this->msg_e("Error, the file $filetmp could not moved to : $filedest ");
continue;
}
$attachment = array(
'post_mime_type' => $filetype['type'],
'post_title' => $filetitle,
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filedest );
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filedest );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
}
}
As I said, as both upload fields uses the same function, the $attach_ID variable will be the value of the latest upload.
function releases_action(){
global $wpdb;
// Upload cover
$uploadfiles = $_FILES['uploadfiles'];
if (is_array($uploadfiles)) {
foreach ($uploadfiles['name'] as $key => $value) {
// look only for uploded files
if ($uploadfiles['error'][$key] == 0) {
$filetmp = $uploadfiles['tmp_name'][$key];
//clean filename and extract extension
$filename = $uploadfiles['name'][$key];
// get file info
// #fixme: wp checks the file extension....
$filetype = wp_check_filetype( basename( $filename ), null );
$filetitle = preg_replace('/\.[^.]+$/', '', basename( $filename ) );
$filename = $filetitle . '.' . $filetype['ext'];
$upload_dir = wp_upload_dir();
/**
* Check if the filename already exist in the directory and rename the
* file if necessary
*/
$i = 0;
while ( file_exists( $upload_dir['path'] .'/' . $filename ) ) {
$filename = $filetitle . '_' . $i . '.' . $filetype['ext'];
$i++;
}
$filedest = $upload_dir['path'] . '/' . $filename;
/**
* Check write permissions
*/
if ( !is_writeable( $upload_dir['path'] ) ) {
$this->msg_e('Unable to write to directory %s. Is this directory writable by the server?');
return;
}
/**
* Save temporary file to uploads dir
*/
if ( !#move_uploaded_file($filetmp, $filedest) ){
$this->msg_e("Error, the file $filetmp could not moved to : $filedest ");
continue;
}
$attachment = array(
'post_mime_type' => $filetype['type'],
'post_title' => $filetitle,
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filedest );
require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filedest );
wp_update_attachment_metadata( $attach_id, $attach_data );
// $ids[]= $attach_id;
// save $attach id here, its correct for this loop, on the next loop it will be different and so on..
}
}
return $ids; // or save here serialize() maybe needed depending on how you are saving.
}

WordPress file "upload" from plugin

I'm trying to create plugin importing posts to WordPress. Imported articles (XML) contain "image-name" attribute and this image is already uploaded to the server.
I would like to, however, make WordPress do its "magic" and import the image to the system (create thumbnails, attach it to the post, place it under the wp-uploads directory scheme)... I found function media_handle_upload($file_id, $post_id, $post_data, $overrides) but it requires array $_FILES to be filled with actual upload (and I'm not uploading file - it is already placed on the server) so it's not very useful
Do you have any hint how to proceed?
Thanks
Check the following script to get the idea. (It does work.)
$title = 'Title for the image';
$post_id = YOUR_POST_ID_HERE; // get it from return value of wp_insert_post
$image = $this->cache_image($YOUR_IMAGE_URL);
if($image) {
$attachment = array(
'guid' => $image['full_path'],
'post_type' => 'attachment',
'post_title' => $title,
'post_content' => '',
'post_parent' => $post_id,
'post_status' => 'publish',
'post_mime_type' => $image['type'],
'post_author' => 1
);
// Attach the image to post
$attach_id = wp_insert_attachment( $attachment, $image['full_path'], $post_id );
// update metadata
if ( !is_wp_error($attach_id) )
{
/** Admin Image API for metadata updating */
require_once(ABSPATH . '/wp-admin/includes/image.php');
wp_update_attachment_metadata
( $attach_id, wp_generate_attachment_metadata
( $attach_id, $image['full_path'] ) );
}
}
function cache_image($url) {
$contents = #file_get_contents($url);
$filename = basename($url);
$dir = wp_upload_dir();
$cache_path = $dir['path'];
$cache_url = $dir['url'];
$image['path'] = $cache_path;
$image['url'] = $cache_url;
$new_filename = wp_unique_filename( $cache_path, $filename );
if(is_writable($cache_path) && $contents)
{
file_put_contents($cache_path . '/' . $new_filename, $contents);
$image['type'] = $this->mime_type($cache_path . '/' . $new_filename); //where is function mime_type() ???
$image['filename'] = $new_filename;
$image['full_path'] = $cache_path . '/' . $new_filename;
$image['full_url'] = $cache_url . '/' . $new_filename;
return $image;
}
return false;
}

Categories