在Wordpress中使用php发布的帖子的上传时间越来越多

时间:2018-04-07 13:53:21

标签: php wordpress

我们使用以下代码将自定义帖子上传到我们的wordpress网站。基本上帖子是一个包含10-15个图像文件的文件夹,这些文件是从C:/ ****** / S1 /文件夹中递归检索的。使用文件夹的名称和内容创建帖子,将图像文件创建为附件类型并与该特定帖子相关联。我们使用groups插件来处理访问控制。

问题是当我们第一次开始使用此代码时,我们可以在14-15分钟内上传100个自定义类型。但是时间不断增加,现在相同数量的帖子需要大约30-35分钟,大小和数量保持不变。由于我们继续上传帖子,我们不确定此时间会持续多长时间,我们打算在一个月内创建约30000个帖子。因此,时间是一个关键因素。到目前为止,我们上传了大约3000个。

我们不确定为什么这段时间会增加,我们需要保持不变。我们目前在4 GB RAM,2.6Ghz 2核处理器系统上托管此应用程序。目前posts表有大约53000个条目,post_meta有大约70000个条目。欢迎任何建议/意见!!

提前致谢!!

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."<a href = \"".$image."\" rel=\"lightbox[". $post["id"]."]\"><img style=\"width:150px;height:150px\"; class=\"alignleft size-thumbnail wp-image-".$attach_id."\" src=\"".$image."\"  /></a>";


}

$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/>";
}

0 个答案:

没有答案