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

Matthew Gerring mgerring at emrl.com
Thu Nov 12 18:28:54 UTC 2009


I figured that out- media_sideload_image() handles the upload and  
creates a fake $_FILES-like array from a remote URL. You can pass a  
regular $_FILES array to media_handle_sideload() along with the post  
ID and it handles all of the attachment, meta, mimetype, thumbnail  
generation, and etc.

I had to require /wp-admin/includes/admin.php to have access to all of  
the functions I needed- is this the best way to do it?

-Matthew

On Nov 12, 2009, at 9:19 AM, Otto wrote:

> Sideload is where you "upload" a file from somewhere else on the web.
> So it'll go grab that file from a website URL that you give it.
>
>
> -Otto
> Sent from Memphis, TN, United States
>
>
> On Wed, Nov 11, 2009 at 2:39 PM, Matthew Gerring <mgerring at emrl.com>  
> wrote:
>> Finally found that function just now, but I'm leaning towards using
>> media_handle_sideload.
>>
>> On Nov 11, 2009, at 12:37 PM, Leo germani wrote:
>>
>>> I think you should use media_handle_upload()
>>>
>>> Leo,,
>>>
>>> On Wed, Nov 11, 2009 at 6:04 PM, Matthew Gerring <mgerring at emrl.com>
>>> wrote:
>>>
>>>> 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
>>>> _______________________________________________
>>>> wp-hackers mailing list
>>>> wp-hackers at lists.automattic.com
>>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>>>
>>>
>>>
>>>
>>> --
>>> leogermani.pirex.com.br
>>> leogermani.estudiolivre.org
>>> _______________________________________________
>>> wp-hackers mailing list
>>> wp-hackers at lists.automattic.com
>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list