martedì 29 gennaio 2013

Upload multifile con wordpress via script

Per aggiungere piu' di un file (in rosso le differenze rispetto allo script precedente).

for($x=1;$x<7;$x++) {
    
     $nomefile="file_".$x;
     if($_FILES[$nomefile]['name']) {
  
 if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $_FILES[$nomefile];
$wp_filetype = wp_check_filetype(basename($uploadedfile), null );
//print_r($_FILES[$nomefile]);
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
  //  echo "File is valid, and was successfully uploaded.\n";
    var_dump( $movefile);
} else {
  //  echo "Possible file upload attack!\n";
}$title = $_FILES[$nomefile]['name'];

  $attachment = array(
                'post_mime_type'    => 'image/jpeg',
                'post_title'        => addslashes($title),
                'post_content'      => '',
                'post_status'       => 'inherit',
                'post_parent'       => $newpostID
            );

$attach_id  = wp_insert_attachment($attachment, $uploadedfile, $newpostID);


$wp_upload_dir = wp_upload_dir();
$filename=$wp_upload_dir['path'] . '/' . $title;

  require_once(ABSPATH . 'wp-admin/includes/image.php');


  $attach_data = wp_generate_attachment_metadata( $attach_id, $filename);

 
  $nuovaurl=$attach_data["file"];

 update_post_meta($attach_id, '_wp_attached_file', $nuovaurl);

 if($nomefile=="file_1") {

update_post_meta($newpostID, '_thumbnail_id', $attach_id);

 
 }


     } else {
     header('Location: http://www.ateamgarage.com/wp/form?message=ok');
          }

     }

Nessun commento: