I writing plugin in my profile page and i want to create upload via ‘Browse’ button and ‘Dir’ field that will upload and return image url.And i don’t want to use media-upload.
I used to read Set Featured Image Front Frontend Form? and I don’t understand code
Help me solve this problem?
There are several parts.
You need to add an
enctype
to the profile form.Then add a field to the form.
And then save the data.
wp_handle_upload
is probably the simplest. From the Codex:The
file
input type works (mostly) like any other form input. If you give it a name likemy_uploads['profile_photo']
instead of justprofile_photo
you will get an array. This will be reflected in the$_FILES
variable when you process the form submission. You can add as manyfile
inputs as you want by building that array, or even just by giving them different names. As far as dynamically addingfile
inputs, that is pretty simple Javascript.Reference
http://codex.wordpress.org/Function_Reference/wp_handle_upload
http://codex.wordpress.org/Filesystem_API
I guess you are not very expirenced with PHP and files uploads. So I start with some basics and end with a simple class.
If you do not have already read the basics about file uploads with PHP, do it now. Nobody will explained it here for you, it is off topic.
Let start with the basic HTML form
That is quiet simple. If the superglobal array
$_FILES
is empty, this means no file was uploaded, display the upload form. If it is not empty, process the upload. I useadmin_url()
and the$pagenow
var to create the action url. If you use the upload form on the frontend, you have to usehome_url()
or something similiar.After sending the file with HTML, you have to process the uploaded files. This will be done in the
File_Upload
class.This is a basic class and not for production! File uploads are a very sensitive topic, you have to validate and sanitize all data by yourself. Otherwise it is possible that someone upload malicious code and hack your server/blog/website!
Now step by step what happend where and when.
With creating an instance of the class, the class try to create a copy of the superglobal array
$_FILES
. If you work with superglobal arrays, don’t touch them! Maybe other parts of the code (plugins or themes) also need the data inside them. The next thing what happend in the constructor is, that we try to guess the index key. As you know, we can pass more than only one file and$_FILES
can contain data for all uploaded files. If you need to process more than one file, loop over$_FILES
and set the index key withset_field_name_for_file()
set_field_name_for_file()
is also needed if you want to pic a specific field from your HTML form, otherwise the class use the first index it can find.The next step is to handle the uploaded file. The method
create_attachment()
call the protected methodhandle_uploaded_file()
, you do not have to do it manually. Thehandle_uploaded_file()
method simply collect and setup some needed pathes and filenames.The last step is to create an attachment.
create_atatchment()
will setup all needed data and create an attachment for you. The method returns the ID of the attachment, so you can access all attachment data withget_post( [id] )
Some words about error handling
I wrote some hints in the class code for production environment. A I mentioned above, file uploads are a very sensitive topic. You have to check, validate and sanitize really everything. WordPress has a build in error handling with
WP_Error()
. Use it! And check if the upload was success withis_wp_error()
.PHP can setup some error messages why the upload was failed if it failed. But PHP do not return clear text messages, it returns error codes. A method to convert this codes into clear text could look like this:
protected function guess_upload_error( $err = 0 ) {
}
You have to check if the upload was successfull, this could look like this (in method
handle_upload_file()
)And then you have to handle the error when calling the class
Always remember: Never ever let a failed upload be untreated!
add Code in function file