#17604 closed defect (bug) (fixed)
wp.uploadFile overwrite doesn't work
Reported by: | M66B | Owned by: | wonderboymusic |
---|---|---|---|
Milestone: | 4.4 | Priority: | normal |
Severity: | normal | Version: | |
Component: | XML-RPC | Keywords: | has-patch 3.6-early |
Focuses: | Cc: |
Description
Removing the following lines from the function mw_newMediaObject file class-wp-xmlrpc-server.php solves the problem:
$filename = preg_replace('/^wpid\d+-/', '', $name); $name = "wpid{$old_file->ID}-{$filename}";
Why are these lines there anyway? overwrite is overwrite.
This, presumably, bug exists in WP 3.1 too.
Attachments (1)
Change History (16)
#3
follow-up:
↓ 4
@
12 years ago
Can we remove this functionality. I don't get why it is in their the first place because it isn't something WordPress does by itself
#4
in reply to:
↑ 3
@
12 years ago
Replying to markoheijnen:
Can we remove this functionality. I don't get why it is in their the first place because it isn't something WordPress does by itself
Works for me.
#6
@
12 years ago
The patch removes overwrite functionality. Looked in to it more and it was never overwrite. It's more delete and upload a new one.
#9
follow-up:
↓ 10
@
10 years ago
Hi there, what's the matter with this bug? I would like to have unique media names in my wordpress and thus overwrite existing files but if I set overwrite=True via xml-rpc api, strange things happen. Wordpress creates files with "wpid-" prefix, which seems to be caused by the two lines mentioned above. They also don't get overwritten but created several times with the same strange name.
My Wordpress: Version 4.1.1
My request:
POST /xmlrpc.php HTTP/1.1 Host: test.shop.de Accept-Encoding: gzip User-Agent: xmlrpclib.py/1.0.1 (by www.pythonware.com) Content-Type: text/xml Content-Length: 23651 <?xml version='1.0'?> <methodCall> <methodName>wp.uploadFile</methodName> <params> <param> <value><int>0</int></value> </param> <param> <value><string>my-user</string></value> </param> <param> <value><string>my-secret</string></value> </param> <param> <value><struct> <member> <name>bits</name> <value><base64> ... </base64></value> </member> <member> <name>type</name> <value><string>image/jpeg</string></value> </member> <member> <name>name</name> <value><string>test.jpg</string></value> </member> <member> <name>overwrite</name> <value><boolean>1</boolean></value> </member> </struct></value> </param> </params> </methodCall>
Created Files:
{'type': 'image/jpeg', 'id': '571', 'file': 'wpid-test.jpg'} {'type': 'image/jpeg', 'id': '572', 'file': 'wpid-test.jpg'} {'type': 'image/jpeg', 'id': '573', 'file': 'wpid-test.jpg'} {'type': 'image/jpeg', 'id': '574', 'file': 'wpid-test.jpg'} {'type': 'image/jpeg', 'id': '575', 'file': 'wpid-test.jpg'}
#10
in reply to:
↑ 9
@
9 years ago
- Version 3.2 deleted
Replying to marco_hoyer:
Hi, I also encounter this curious behavior about overwrite parameter of XMLRPC uploadFile.
WordPress version is 4.2.2.
I expect that when overwrite parameter is true, an attachment file which has same filename and inherits same post id should be removed, and new attachment file via XMLRPC should be registered.
However, actually each time I post the attached file each time, I found that files continue to increase.
{'type': 'image/jpeg', 'id': '571', 'file': 'wpid-test.jpg'}
{'type': 'image/jpeg', 'id': '572', 'file': 'wpid-test1.jpg'}
{'type': 'image/jpeg', 'id': '573', 'file': 'wpid-test2.jpg'}
{'type': 'image/jpeg', 'id': '574', 'file': 'wpid-test3.jpg'}
{'type': 'image/jpeg', 'id': '575', 'file': 'wpid-test4.jpg'}
....
And, in case of "overwrite" parameter is false, every time I post I can get
{'type': 'image/jpeg', 'id': '571', 'file': 'test.jpg'}
{'type': 'image/jpeg', 'id': '572', 'file': 'test1.jpg'}
{'type': 'image/jpeg', 'id': '573', 'file': 'test2.jpg'}
{'type': 'image/jpeg', 'id': '574', 'file': 'test3.jpg'}
{'type': 'image/jpeg', 'id': '575', 'file': 'test4.jpg'}
What exactly is going on? :-?
#11
@
9 years ago
I create wp.uploadFile wrapper XML-RPC method.
<?php add_filter('xmlrpc_methods', 'add_junoe_xmlrpc_methods'); function add_junoe_xmlrpc_methods($methods) { return array_merge($methods, array( 'wp.JuploadFile' => 'junoe_wp_uploadFile', )); } function junoe_wp_uploadFile($args) { global $wp_xmlrpc_server; if (isset($args[3])) { $data = $args[3]; if ((isset($data['overwrite']) && $data['overwrite']) && (isset($data['post_id']) && $data['post_id'])){ $attachments = get_posts(array( 'post_type' => array( 'attachment' ), 'post_parent' => $data['post_id'], 'posts_per_page' => -1, 'post_status' => 'inherit', )); foreach ($attachments as $attachment){ if ($attachment->post_title == $data['name'] || $attachment->post_title == "wpid-".$data['name']){ wp_delete_attachment($attachment->ID, true); } } } } return $wp_xmlrpc_server->mw_newMediaObject($args); }
This may remove if upload file name has prefix "wpid-". But, after XMLRPC fileupload with overwrite parameter off had done, these prefixed files has been created. :-(
#12
@
9 years ago
- Milestone changed from Future Release to 4.4
- Owner set to wonderboymusic
- Status changed from new to assigned
Related: [5008]