Fixing file upload
-
Hi
A while ago I had a plugin built for me. It uses openstreetmap and leflet.js. In 2016 when the plugin was built I could upload gpx walking routes. See this page:
River Fowey showing walking routeHowever, when I now upload a gpx file it doesn’t display:
Tarka Trail not displayingI think I have worked out the fault. Previously when a file was uploaded it added a url underneath the file upload button:
Showing url pathIs there anyway of working out what has changed that might have caused the path to stop working or displaying. Is there a place where I might be able to view how to use uploads in WordPress please?
Thanks
Rich
The page I need help with: [log in to see the link]
-
Check your error logs for any indication on why the upload failed. If there is a logged error, the log might direct you to exactly where the problem is. Or not. Sometimes where PHP sees the error occur is not the root cause of the error. Even then, it might be possible to trace back from the logged error to the root cause.
It looks like the file upload is handled via Ajax, so also check you browser console for possible errors after attempting an upload.
The default file upload handler for WP is wp_media_upload_handler(). Your plugin might use this, or use one of its sub-functions, or manage its own file upload independent of WP functions.
With no hints from error logs, about all you can do is carefully debug the upload handling code. See where the upload request goes by using the network developer tool of your browser. If Ajax is used as part of a WP upload, the POSTed data will include an “action” parameter which is used to create a action tag in the form of
"wp_ajax_{$_POST['action']}"
. Examine the global $wp_filter array for the tag resulting from that code. The associated callback function will be the upload handler.If WP Ajax is not used, the destination of the POST likely contains the upload handler code.
Thanks for the reply. I am a bit lost on this one – [redacted]. It is a frustrating error.
if ( !function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
The code above is the only code I can find that handles the upload. Should it read wp_media_upload_handler please?
Thanks
Rich
-
This reply was modified 6 years, 9 months ago by
bcworkz. Reason: redact payment offer
This is the full code used by the path upload element:
public function path_metabox( $path ) { /** Register all path metaboxes */ add_meta_box( 'im_path_details', __( 'Path Details', self::$TEXTDOMAIN ), array( $this, 'path_metabox_details' ), 'path', 'normal' ); } public function path_metabox_details( $path ) { $path = $path->filter( 'raw' ); ?> <table class="form-table"> <tr> <th> <label><?php esc_html_e( 'Upload GPX', self::$TEXTDOMAIN ); ?></label> </th> <td> <input type="file" name="path" accept=".gpx"/> <?php if ( $path->gpx ) { ?><p><?php printf( __( 'Current: <a href="%s">%s</a>', self::$TEXTDOMAIN ), esc_attr( $path->gpx ), esc_html( $path->gpx ) ); ?></p><?php } ?> </td> </tr> <tr> <th> <label><?php esc_html_e( 'Enter Path Description', self::$TEXTDOMAIN ); ?></label> </th> <td> <?php wp_editor( $path->description, 'description', array( ) ); ?> </td> </tr> </table> <?php } public function maybe_form_uploads( $post ) { if ( in_array( $post->post_type, array( 'path' ) ) ) { printf( ' enctype="multipart/form-data"' ); } } public function save_path( $path_id, $path, $update ) { if ( $path->post_type != 'path' ) return; if ( !empty( $_FILES['path'] ) ) { $file = $_FILES['path']; if ( !function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' ); add_filter( 'mime_types', function( $mimes ) { return array_merge( $mimes, array( 'gpx' => 'application/gpx+xml' ) ); } ); $file = wp_handle_upload( $file, array( 'action' => 'editpost' ) ); if ( empty( $file['error'] ) ) update_post_meta( $path_id, 'gpx', $file['url'] ); } if ( isset( $_POST['description'] ) ) update_post_meta( $path_id, 'description', $_POST['description'] ); } public function add_shortcodes() { add_shortcode( 'interactivemap', array( $this, 'interactivemap_shortcode' ) ); }
I hope someone can spot the problem.
Thanks
Rich
wp_handle_upload() will certainly, uh… handle uploads. var_dump $file just before this function is called and ensure it conforms to a single file reference in the standard PHP $_FILES structure.
var_dump $file again after the call and see if any error message is in place.
I’m assuming other WP media uploads work correctly, which rules out any file permissions issues.
Is there an add_filter() call for “upload_mimes” somewhere? I don’t think WP normally allows .gpx uploads. Adding this type to the mimes list through this filter will enable GPX uploads. This filter also passes the current user so only certain users or roles can be allowed to upload GPX files, in case this might be useful to you.
If GPX files are not normally allowed and are not added through the filter, the error returned in $file after calling wp_handle_upload() will say “Sorry, this file type is not permitted for security reasons.”.
Thanks for the reply. Yes uploads still work as normally and there is a call to allow gpx files.
I have worked out that the error happened somewhere between:
16th November 2016 – we were able to upload a gpx file
3rd February 2017 – no longer able to uploadDuring this time WordPress released WordPress 4.7 Vaughan. Did anything significant happen with uploads? Reading the blog release, they did do something with pdf uploads and thumbnails. I can’t work it out.
I have been really ill and not able to do any work until now. Unfortunately, the medicine I am on has scrambled my brain and I am struggling with any sort of coding. I have posted it on jobs.wordpress.net to see if anyone can work out the issue.
If anyone can help I would be so happy.
Thanks
Rich
I neglected to mention that I removed your comment about payment from a few posts back. Offering payment in these forums is against our guidelines. Announcing that you’ve posted in jobs.wp.net is fine. Please reject any offers of paid assistance that may arise through these forums. In particular do not provide privileged access to your site to any such individuals. You have no assurance such people have honest intentions. The vast majority of members here are well intentioned, but we can never be sure. It’s not worth the risk.
Sorry to hear you’ve been ill, I know all too well how medication can make coding impossible.
The PDF changes, IIRC, were related to preview images, nothing about the upload process itself.
Thanks for your reply and I’m sorry I didn’t mean to go against the forum guidelines. And thanks for your advice, your wisdom shines through.
Rich
I’m assuming other WP media uploads work correctly, which rules out any file permissions issues.
You called it correctly. It is a security issue. It won’t allow files of this type to uploaded.
Rich
Which “it” won’t allow GPX? WordPress or your server? WordPress restrictions can usually be altered. For server restrictions, unless you are on VPS, you’d need to coordinate with your host. Where there is a will, there is a way ??
Hi
I finally got there! WordPress rejected uploading of gpx files via the media area. So I looked at the code and tried a plugin that would allow different uploads. That worked. So worked with another developer this afternoon and he fixed it for me in 10 minutes.
Rich
Good to hear! Amazing what a difference it makes to have someone that knows what they are doing working on your site ??
Yes. It makes a huge difference. It is frustrating that it took me a week to work what it was but I couldn’t have done it without your promptings. You gave me invaluable time throughout this journey and provided wisdom that kept me moving to solving the problem. The developer I contacted might have had to do a lot of research just to get to the 10 minute fix.
Huge thumbs up and thank you to you.
-
This reply was modified 6 years, 9 months ago by
- The topic ‘Fixing file upload’ is closed to new replies.