Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

upload image

By: a guest on Apr 29th, 2011  |  syntax: PHP  |  size: 7.66 KB  |  views: 688  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. define("THUMB_DIR", WP_CONTENT_DIR . '/plugins/meta-upload/thumbs/');
  4. define("THUMB_URL", WP_CONTENT_URL . '/plugins/meta-upload/thumbs/');
  5.  
  6. // needs to be implemented
  7.  
  8. function fileupload( $label ) { ?>
  9.     <tr>
  10.         <td class="left_label"> <?php
  11.             echo $label; ?>
  12.         </td>
  13.         <td>
  14.             <form name="uploadfile" id="uploadfile_form" method="POST" enctype="multipart/form-data" action="<?php echo $this->filepath.'#uploadfile'; ?>" accept-charset="utf-8" >
  15.                 <input type="file" name="uploadfiles[]" id="uploadfiles" size="35" class="uploadfiles" />
  16.                 <input class="button-primary" type="submit" name="uploadfile" id="uploadfile_btn" value="Upload"  />
  17.             </form>
  18.         </td>
  19.     </tr>  <?php
  20. }
  21.  
  22. // this too
  23.  
  24. function fileupload_process() {
  25.     $uploadfiles = $_FILES['uploadfiles'];
  26.  
  27.     if (is_array($uploadfiles)) {
  28.  
  29.         foreach ($uploadfiles['name'] as $key => $value) {
  30.  
  31.             // look only for uploded files
  32.             if ($uploadfiles['error'][$key] == 0) {
  33.  
  34.                 $filetmp = $uploadfiles['tmp_name'][$key];
  35.  
  36.                 //clean filename and extract extension
  37.                 $filename = $uploadfiles['name'][$key];
  38.  
  39.                 // get file info
  40.                 // @fixme: wp checks the file extension....
  41.                 $filetype = wp_check_filetype( basename( $filename ), null );
  42.                 $filetitle = preg_replace('/\.[^.]+$/', '', basename( $filename ) );
  43.                 $filename = $filetitle . '.' . $filetype['ext'];
  44.                 $upload_dir = wp_upload_dir();
  45.  
  46.                 /**
  47.                  * Check if the filename already exist in the directory and rename the
  48.                  * file if necessary
  49.                  */
  50.                 $i = 0;
  51.                 while ( file_exists( $upload_dir['path'] .'/' . $filename ) ) {
  52.                     $filename = $filetitle . '_' . $i . '.' . $filetype['ext'];
  53.                     $i++;
  54.                 }
  55.                 $filedest = $upload_dir['path'] . '/' . $filename;
  56.  
  57.                 /**
  58.                  * Check write permissions
  59.                  */
  60.                 if ( !is_writeable( $upload_dir['path'] ) ) {
  61.                     $this->msg_e('Unable to write to directory %s. Is this directory writable by the server?');
  62.                     return;
  63.                 }
  64.  
  65.                 /**
  66.                  * Save temporary file to uploads dir
  67.                  */
  68.                 if ( !@move_uploaded_file($filetmp, $filedest) ){
  69.                     $this->msg_e("Error, the file $filetmp could not moved to : $filedest ");
  70.                     continue;
  71.                 }
  72.  
  73.                 $attachment = array(
  74.                     'post_mime_type' => $filetype['type'],
  75.                     'post_title' => $filetitle,
  76.                     'post_content' => '',
  77.                     'post_status' => 'inherit'
  78.                 );
  79.  
  80.                 $attach_id = wp_insert_attachment( $attachment, $filedest );
  81.                 require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
  82.                 $attach_data = wp_generate_attachment_metadata( $attach_id, $filedest );
  83.                 wp_update_attachment_metadata( $attach_id,  $attach_data );
  84.             }
  85.         }
  86.     }
  87. }
  88.  
  89. add_action('admin_menu', "post_upload_box_init");
  90. add_action('save_post', 'post_save_thumb');
  91.  
  92. function post_upload_box_init() {
  93.     add_meta_box("post-thumbnail-posting", "Dark Toob Thumbnail", "post_upload_thumbnail", "post", "advanced");
  94.    
  95. }
  96.  
  97. function post_upload_thumbnail() {
  98.     global $post;
  99. ?>
  100.     <script type="text/javascript">
  101.         document.getElementById("post").setAttribute("enctype","multipart/form-data");
  102.         document.getElementById('post').setAttribute('encoding','multipart/form-data');
  103.     </script>
  104.  
  105.     <?php
  106.         $thumb = get_post_meta($post->ID, 'custom_thumbnail',true);
  107.  
  108.         if ( $thumb )
  109.         {
  110.     ?>
  111.     <div style="float: left; margin-right: 10px;">
  112.         <img style="border: 1px solid #ccc; padding: 3px;" src="<?php echo THUMB_URL . $thumb; ?>" alt="Thumbnail preview" />
  113.     </div>
  114.     <?php
  115.         }
  116.         else
  117.         {
  118.     ?>
  119.     <div style="float: left; margin-right: 10px; width: 200px; height: 150px; line-height: 150px; border: solid 1px #ccc; text-align: center;">Thumbnail preview</div>
  120.     <?php } ?>
  121.    
  122.     <div style="float: left;">
  123.         <p>
  124.             <label for="thumb-url-upload"><?php _e("Upload via URL, or Select Image (Below)"); ?>:</label><br />
  125.             <input style="width: 300px; margin-top:5px;" id="thumb-url-upload" name="thumb-url-upload" type="text" />
  126.         </p>    
  127.         <p>
  128.             <p><label for="thumbnail"><?php _e("Upload a thumbnail"); ?>:</label><br />
  129.             <input id="thumbnail" type="file" name="thumbnail" />
  130.         </p>
  131.         <p><input id="thumb-delete" type="checkbox" name="thumb-delete"> <label for="thumb-delete"><?php _e("Delete thumbnail"); ?></label></p>
  132.          
  133.         <p style="margin:10px 0 0 0;"><input id="publish" class="button-primary" type="submit" value="<?php _e("Update Post"); ?>" accesskey="p" tabindex="5" name="save"/></p>
  134.     </div>
  135.    
  136.     <div class="clear"></div>
  137. <?php
  138. }
  139.  
  140. function post_save_thumb( $postID )
  141. {
  142.     global $wpdb;
  143.  
  144.     // Get the correct post ID if revision.
  145.     if ( $wpdb->get_var("SELECT post_type FROM $wpdb->posts WHERE ID=$postID")=='revision')
  146.         $postID = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID=$postID");
  147.  
  148.     if ( $_POST['thumb-delete'] )
  149.     {
  150.         @unlink(THUMB_DIR . get_post_meta($postID, 'custom_thumbnail', true));
  151.         delete_post_meta($postID, 'custom_thumbnail');
  152.     }
  153.     elseif ( $_POST['thumb-url-upload'] || !empty($_FILES['thumbnail']['tmp_name']) )
  154.     {
  155.         if ( !empty($_FILES['thumbnail']['name']) )
  156.             preg_match("/(\.(?:jpg|jpeg|png|gif))$/i", $_FILES['thumbnail']['name'], $matches);
  157.         else
  158.             preg_match("/(\.(?:jpg|jpeg|png|gif))$/i", $_POST['thumb-url-upload'], $matches);
  159.        
  160.         $thumbFileName = $postID . strtolower($matches[0]);
  161.    
  162.         // Location of thumbnail on server.
  163.         $loc = THUMB_DIR . $thumbFileName;
  164.        
  165.         $thumbUploaded = false;
  166.    
  167.         if ( $_POST['thumb-url-upload'] )
  168.         {
  169.             // Try just using fopen to download the image.
  170.             if( ini_get('allow_url_fopen') )
  171.             {
  172.                 copy($_POST['thumb-url-upload'], $loc);
  173.                 $thumbUploaded = true;
  174.  
  175.             }
  176.             else
  177.            
  178.             // If fopen doesn't work, try cURL.
  179.             if( function_exists('curl_init') )
  180.             {
  181.                 $ch = curl_init($_POST['thumb-url-upload']);
  182.                 $fp = fopen($loc, "wb");
  183.    
  184.                 $options = array(CURLOPT_FILE => $fp,
  185.                     CURLOPT_HEADER => 0,
  186.                     CURLOPT_FOLLOWLOCATION => 1,
  187.                     CURLOPT_TIMEOUT => 60);
  188.                 curl_setopt_array($ch, $options);
  189.                
  190.                 curl_exec($ch);
  191.                 curl_close($ch);
  192.    
  193.                 fclose($fp);
  194.                 $thumbUploaded = true;
  195.             }
  196.         }
  197.         else
  198.    
  199.         // Attempt to move the uploaded thumbnail to the thumbnail directory.
  200.         if ( !empty($_FILES['thumbnail']['tmp_name']) && move_uploaded_file($_FILES['thumbnail']['tmp_name'], $loc) )
  201.             $thumbUploaded = true;
  202.        
  203.         if ( $thumbUploaded )
  204.         {
  205.             if ( !update_post_meta($postID, 'custom_thumbnail', $thumbFileName) )
  206.                 add_post_meta($postID, 'custom_thumbnail', $thumbFileName);
  207.         }
  208.  
  209.     }
  210. }
  211.  
  212. ?>