改进我的解压缩和移动功能 - PHP



我是一个PHP新手,正在寻找一些关于我创建的PHP函数的建议,以便在Wordpress安装中使用。

从下面的代码中可以看到,当一个管理员在一个待处理的帖子上按下'Publish'时,它会运行。

它需要一个用户通过Gravity Forms上传的Zip文件,然后解压缩。mp3扩展名。将所有文件重新压缩并移动到Amazon S3目录中的新文件夹中。

这些代码是根据我有限的知识和一些帮助拼凑在一起的。

所以,这是我最后的结果:

    add_action('pending_to_publish', 'unzip_to_s3');  
   function unzip_to_s3() { 
   global $post;
   global $wpdb;
   // Only run function if post is portfolio post type
    if ('portfolio' == $post->post_type) {
   // Set temp path
   $temp_path = '../wp-content/uploads/gravity_forms/1-9e5dc27086c8b2fd2e48678e1f54f98c/2013/02/tmp/';
   // Get filename from Zip file
   $file = get_post_meta($post->ID, 'file_url', true);
   $zip_file = basename($file);
   // Create full Zip file path
   $zip_file_path = $temp_path.$zip_file;
   // Generate unique name for temp sub_folder for unzipped files
   $temp_unzip_folder = uniqid('temp_TMS_', true);
   // Create full temp sub_folder path
   $temp_unzip_path = $temp_path.$temp_unzip_folder;
   // Make the new temp sub_folder for unzipped files
   if (!mkdir($temp_unzip_path, 0755, true)) {
    die('Error: Could not create path: '.$temp_unzip_path);
   }
   // Unzip files to temp unzip folder, ignoring anything that is not a .mp3 extension
   $zip = new ZipArchive();
   $filename = $zip_file_path;
   if ($zip->open($filename)!==TRUE) {
      exit("cannot open <$filename>n");
   }
   for ($i=0; $i<$zip->numFiles;$i++) {
      $info = $zip->statIndex($i);
      $file = pathinfo($info['name']);
      if(strtolower($file['extension']) == "mp3") {
           file_put_contents($temp_unzip_path.'/'.basename($info['name']), $zip->getFromIndex($i));
      } else {
      $zip->deleteIndex($i);
      }
   }
   $zip->close();
   // Re-zip the unzipped mp3's and store new zip file in temp folder created earlier
   $temp_unzip_path = $temp_unzip_path.'/';
   $zip = new ZipArchive();
   $dirArray = array();
   $new_zip_file = $temp_unzip_path.$zip_file;
   $new = $zip->open($new_zip_file, ZIPARCHIVE::CREATE);
   if ($new === true) {
       $handle = opendir($temp_unzip_path);
       while (false !== ($entry = readdir($handle))) {
           if(!in_array($entry,array('.','..')))
           {
               $dirArray[] = $entry;
               $zip->addFile($temp_unzip_path.$entry,$entry);
           }
       }
       closedir($handle);
   } else {
       echo 'Failed to create Zip';
   }
   $zip->close();
   // Set Media bucket dir
   $bucket_path = '../wp-content/uploads/gravity_forms/1-9e5dc27086c8b2fd2e48678e1f54f98c/2013/02/mixtape2/';
   // Generate unique name for sub_bucket
   $sub_bucket = uniqid('TMS_', true);
   // Create full sub_bucket path
   $sub_bucket_path = $bucket_path.$sub_bucket;
   // Make the new sub_bucket
   if (!mkdir($sub_bucket_path, 0755, true)) {
    die('Error: Could not create path: '.$sub_bucket_path);
   }
   // Move mp3's to new sub_bucket 
   // Get array of all source files
   $files = scandir($temp_unzip_path);
   // Identify directories
   $source = $temp_unzip_path;
   $destination = $sub_bucket_path.'/';
   // Cycle through all source files
   foreach ($files as $file) {
    if (in_array($file, array(".",".."))) continue;
        // if move files is successful delete the original temp folder
        if (rename($source.$file, $destination.$file)) {
            rmdir($temp_unzip_path);
        }
    }
   // Delete original Zip file
   unlink($temp_path.$zip_file);
   // Update Custom field for new Zip file location
   update_post_meta($post->ID, 'file_url', 'http://themixtapesite.com/wp-content/uploads/gravity_forms/1-9e5dc27086c8b2fd2e48678e1f54f98c/2013/02/mixtape2/'.$sub_bucket.'/'.$zip_file);
   }
   }

虽然这个函数确实有效,但我们处理的是大文件,所以它确实需要一段时间来处理…发生的事情是,当管理员按下发布,它触发了这个函数,但页面只是坐在那里,直到它完成这个函数,然后将继续。此功能最多可运行5分钟。

我正在寻找优化这个功能(在代码方面),但也看看是否有一种方法,我可以在后台运行这个,以便管理员可以进行其他事情,而不必坐在那里等待。

感谢您的帮助。

您可能想要尝试WP cron并在此时调度任务,以便它在后台运行。这里有一些资源。基本概念是这样的:

if ( ! wp_next_scheduled( 'pending_to_publish' ) ) {
  wp_schedule_single_event($timestamp,'pending_to_publish');
}
add_action('pending_to_publish', 'unzip_to_s3'); 
http://wpengineer.com/1908/use-wordpress-cron/

http://codex.wordpress.org/Category%3aWP-Cron_Functions

https://wordpress.stackexchange.com/questions/42694/run-a-cron-job-or-similar-in-the-background-of-wp-after-post-update-create

最新更新