Filters whether to short-circuit moving the uploaded file after passing all checks.
Description
If a non-null value is returned from the filter, moving the file and any related error reporting will be completely skipped.
Parameters
$move_new_file
mixed- If null (default) move the file after the upload.
$file
array- Reference to a single element from
$_FILES
.
name
stringThe original name of the file on the client machine.type
stringThe mime type of the file, if the browser provided this information.tmp_name
stringThe temporary filename of the file in which the uploaded file was stored on the server.size
intThe size, in bytes, of the uploaded file.error
intThe error code associated with this file upload.
$new_file
string- Filename of the newly-uploaded file.
$type
string- Mime type of the newly-uploaded file.
Source
$move_new_file = apply_filters( 'pre_move_uploaded_file', null, $file, $new_file, $type );
Changelog
Version | Description |
---|---|
4.9.0 | Introduced. |
There is an interesting question on this filter in the support forum with a rather complicated reply, however, I have to use this for a slightly different problem.
I normally use the function
media_handle_sideload()
to move a file into the wp uploads fodler and save it as a media attachment post, which works quite nicely.However, recently I had to copy a file (rather than move it), in my plugin extension as the original plugin still needed to have access to the original file location (a temporary folder on the fs), and unfortunately there is no way to get the
media_handle_sideload()
to copy rather than move a file.This is where this filter comes in handy. The
media_handle_sideload()
makes use of thewp_handle_sideload()
, which in turns calls the_wp_handle_upload()
function, within which thepre_move_uploaded_file
filter is applied.The filter can be used to switch off the actual moving of the file while retaining all the goodness of validation that the function handles, allowing one to handle the actual file handling, copying it instead of moving it in my case. This is how I used it,
Note, the above only copies the file to the
wp-content/uploads/<year>/<month>/
folder structure, but does not actually create an attachment post (required to see your file in the Media section of the dashboard), nor links it to a parent post (eg as a featured/thumbnail image). If you need to see how to do this I suggest you take a look at the process used in themedia_handle_sideload()
function.