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_filemixed- If null (default) move the file after the upload.
$filearray- Reference to a single element from
$_FILES.
namestringThe original name of the file on the client machine.typestringThe mime type of the file, if the browser provided this information.tmp_namestringThe temporary filename of the file in which the uploaded file was stored on the server.sizeintThe size, in bytes, of the uploaded file.errorintThe error code associated with this file upload.
$new_filestring- Filename of the newly-uploaded file.
$typestring- 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_filefilter 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.