[wp-hackers] Uploading image attachments from the front-end

Matthew Gerring mgerring at emrl.com
Wed Nov 11 20:04:38 UTC 2009


Hello all,

I'm working on a collaborative editing system for WP and I'm trying to  
figure out how to access WP's media features from the frontend.  
wp_insert_attachment works just fine, but if I try to generate a  
thumbnail or the image metadata, WP shuts down and stops loading the  
page. I think this is because these functions are only supposed to run  
from the admin area but I'm not sure. This is what my code looks like:

function sw_photo_upload() {
if ($_FILES != null) {
$upath = wp_upload_dir();
$target_path = $upath['path'].'/';
$target_path = $target_path . basename( $_FILES['uploadedfile'] 
['name']);
$target_uri = $upath['url'].'/'.basename( $_FILES['uploadedfile'] 
['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'],  
$target_path)) {
     echo "The file ".  basename( $_FILES['uploadedfile']['name'])."  
has been uploaded";
     echo $target_path;
     $attach = array();
     $attach['guid'] = $target_uri;
     $attach['post_title'] = basename( $_FILES['uploadedfile']['name']);
     $attach_id = wp_insert_attachment($attach, $target_uri,  
$_POST['parent']);
     wp_create_thumbnail($attach_id, '90');
     //wp_generate_attachment_metadata( $attach_id, $target_path );
} else {
     echo "There was an error uploading ".$_FILES['uploadedfile'] 
['name']." to ".$target_path.". PHP returned the following error: ". 
$_FILES['uploadedfile']['error'];
}
}
}

There's lots of debugging stuff in there and I'm sure it's very  
sloppy, but I don't understand why wp_create_thumbnail causes  
everything to crash. It executes just fine if I comment that line out,  
but I want to use WP's built in thumbnail generation. Is there a good  
way to do this?

(And yes, I know this needs security improvements, that's my next step  
once it's working).

-Matthew


More information about the wp-hackers mailing list