WordPress.org

Ready to get started?Download WordPress

Forums

Plugin relocate-upload - Not quite working in 2.7 (62 posts)

  1. Jnobody
    Member
    Posted 5 years ago #

    I'm afraid this plugin is not working (at least for me) in WP 2.7 because when a file is first uploaded to the default folder the corresponding row in db table wp_postmeta holds just the filename, not path and filname. So
    $attachment_path=get_post_meta($id,"_wp_attached_file",true);
    does not get the right value. That is why
    if (rename($attachment_path,$new_path))
    does not work and returns false.

    Maybe you should check if the filename is stored with or without path in wp_postmeta and construct $attachment_path accordingly.

    http://wordpress.org/extend/plugins/relocate-upload/

  2. alanft
    Member
    Posted 5 years ago #

    you are absolutely right - the _wp_attached_file data used to be the full path, and now it looks like its just the path relative to something like WP_CONTENT_DIR/uploads.

    let me check through the 2.7 code - it has to be back compatible with a full path starting with a /, as upgrading doesn't update old values.

    so i probably just need to detect these new relative paths and tack on the base. Let me have a bash at that.

    (trouble is i don't have a 2.7 on a standard setup to test this on - just a local MAMP setup that sets up some global PHP vars a little awkwardly.)

    [edit update]

    well that was easy enough to find! the get_attached_file function has been updated in 2.7 to reflect this - and as i should have been using that all along in my code. it's a simple change that will stay compatible with existing versions. I'll update the repository soon, but if you want to try it yourself...

    find line 38:
    $attachment_path=get_post_meta($id,"_wp_attached_file",true);

    replace with:
    $attachment_path=get_attached_file( $id, true);

    [edit update 2]
    well that half fixes 2.7ness. There is at least one more issue i can see -- the relocate menu doesn't work on the page you get when you have just uploaded a new asset.

    that needs an extra line about line 86

    if (   strpos($_SERVER['REQUEST_URI'], "/wp-admin/media-upload.php")===false
    	&& strpos($_SERVER['REQUEST_URI'], "/wp-admin/upload.php")===false
    	&& strpos($_SERVER['REQUEST_URI'], "/wp-admin/media.php")===false )
    	return;

    needs an extra line

    if (   strpos($_SERVER['REQUEST_URI'], "/wp-admin/media-upload.php")===false
    	&& strpos($_SERVER['REQUEST_URI'], "/wp-admin/upload.php")===false
    	&& strpos($_SERVER['REQUEST_URI'], "/wp-admin/media-new.php")===false
    	&& strpos($_SERVER['REQUEST_URI'], "/wp-admin/media.php")===false )
    	return;

    basically i need to look at this properly to get it 2.7'd up. Feel free to post more issues!

  3. chillstarr
    Member
    Posted 5 years ago #

    Firstly, Happy new year to you.

    Secondly, thanks very much for this plugin- I am setting up a blog for a mate which is to have header images selected randomly at pageload. He needs to be able to upload new images into the header image pool folder, ideally through WP's admin. This is the only way I've found to do that.

    I'm running 2.7 and made the change to line 38 OK, but I can't find the 2nd edit anywhere in the 0.11 file- there's only one mention of strpos and that's on line 135 of the plugin. Or are you talking about editing a core file?

    Before and after making the edit, when trying to define a new folder in the Locations control panel page I get a page returned with just "Cannot load relocate-upload/relocate-upload.php." in a box in the middle.

    Any help getting this going'd be mach appreciated.

  4. alanft
    Member
    Posted 5 years ago #

    hi - yes, sorry that is confusing. those lines are in the development version which you can get here:

    http://wordpress.org/extend/plugins/relocate-upload/download/

    not sure about the source of the "cannot load relocate-upload/relocate-upload.php" error. sorry.

    i have been dragging my heels on making this 2.7 i realise -- been working on another plugin more. will get on this soon.

  5. chillstarr
    Member
    Posted 5 years ago #

    Excellent-

    That worked a treat, thanks a lot!

    The error page didn't show again once on the dev version & having made the above edits.

    Now I've discovered another problem- WP automatically creates and dumps its default set of sized images in the same folder which pretty much screws up what I'm trying to do as I only want the original file to be in the special headers folder I created.

    Be great if you could add an option to stop WP creating its assorted image sizes on upload. Or is there another way around this that I'm missing?

  6. alanft
    Member
    Posted 5 years ago #

    cool, glad that worked out for you :-)

    as for the thumbnail images that WP makes by default, relocate upload does move them to follow the original item. just so you know.

    stopping WP making those thumbnails seems like an odd thing to want to do - they don't do any harm, and are quite useful when browsing the media library for instance.

    but you could inhibit their creation with a bit of code if you *really* needed to i suppose. i might dig around and see what sort of code could be written for you, but i wouldn't add it to this plugin though, sorry.

  7. alanft
    Member
    Posted 5 years ago #

    the thumbnail code in WP core code (in the wp_generate_attachment_metadata function) is:

    $sizes = array('thumbnail', 'medium', 'large');
    $sizes = apply_filters('intermediate_image_sizes', $sizes);
    foreach ($sizes as $size) { [code to make thumbnails] }

    so your theme's functions.php could call that filter and return an empty array.

    add_filter('intermediate_image_sizes', 'intermediate_image_sizes_suppress');
    function intermediate_image_sizes_suppress($sizes) { return (array) null;}

    or something LIKE that. or does foreach fail with an empty array? i can't recall right now. but you get the general idea there.

    i'd just let it go ahead and make them TBH

  8. chillstarr
    Member
    Posted 5 years ago #

    Thanks, I'll have a play around with that code, but don't think I've got the skills to do what I need.

    The reason the resized files can't be there is that there's a PHP script which chooses an image at random from any of the JPEGs that are in that folder for use as a header graphic.

    I do want the normal thumbnail creation elsewhere, just not in that folder. I guess that is a pretty unusual request and can see why you wouldn't be interested in adding the feature :)

    Looks like I'll have to manually upload the images into the right folder. Ah well, was worth a try.

    Thanks anyway

  9. alanft
    Member
    Posted 5 years ago #

    "there's a PHP script which chooses an image at random from any of the JPEGs that are in that folder"

    if you post that code, i could suggest changes to filter out the thumbnails - as they have a very specific filename and could be easy to identify and filter out.

  10. chillstarr
    Member
    Posted 5 years ago #

    Wow- thanks muchly. Really appreciated. Hope this formats OK...

    <?php
    	$folder = '.';
    
        $extList = array();
    	$extList['gif'] = 'image/gif';
    	$extList['jpg'] = 'image/jpeg';
    	$extList['jpeg'] = 'image/jpeg';
    	$extList['png'] = 'image/png';
    
    $img = null;
    
    if (substr($folder,-1) != '/') {
    	$folder = $folder.'/';
    }
    
    if (isset($_GET['img'])) {
    	$imageInfo = pathinfo($_GET['img']);
    	if (
    	    isset( $extList[ strtolower( $imageInfo['extension'] ) ] ) &&
            file_exists( $folder.$imageInfo['basename'] )
        ) {
    		$img = $folder.$imageInfo['basename'];
    	}
    } else {
    	$fileList = array();
    	$handle = opendir($folder);
    	while ( false !== ( $file = readdir($handle) ) ) {
    		$file_info = pathinfo($file);
    		if (
    		    isset( $extList[ strtolower( $file_info['extension'] ) ] )
    		) {
    			$fileList[] = $file;
    		}
    	}
    	closedir($handle);
    
    	if (count($fileList) > 0) {
    		$imageNumber = time() % count($fileList);
    		$img = $folder.$fileList[$imageNumber];
    	}
    }
    
    if ($img!=null) {
    	$imageInfo = pathinfo($img);
    	$contentType = 'Content-type: '.$extList[ $imageInfo['extension'] ];
    	header ($contentType);
    	readfile($img);
    } else {
    	if ( function_exists('imagecreate') ) {
    		header ("Content-type: image/png");
    		$im = @imagecreate (100, 100)
    		    or die ("Cannot initialize new GD image stream");
    		$background_color = imagecolorallocate ($im, 255, 255, 255);
    		$text_color = imagecolorallocate ($im, 0,0,0);
    		imagestring ($im, 2, 5, 5,  "IMAGE ERROR", $text_color);
    		imagepng ($im);
    		imagedestroy($im);
    	}
    }
    
    ?>
  11. alanft
    Member
    Posted 5 years ago #

    the important bit there is

    if (
    	isset( $extList[ strtolower( $file_info['extension'] ) ] )
    )
    {	$fileList[] = $file; }

    which adds a file to the list of random candidate under the condition that the file is in the list of extensions defined at the top of the code. All you need do is be more picky and add in a AND the filename doesn't match a certain pattern.

    Now I think i'm right in saying that all the WP thumbnails end in a suffix like "-nnnxmmm." which is to say a dash then the dimensions separated by an 'x' and then the usual .gif/.png/.jpg rider. So adding something like:

    && !preg_match ( "/-\d+x\d+\./" , $file_info['basename'] )

    should extend the IF to only add the file if the original condition is true AND (&&) there is NOT (!) a match in the filename for: '-', some decimal digits, 'x', some more decimal digits, '.'

    :-)

  12. chillstarr
    Member
    Posted 5 years ago #

    Thanks for explaining- I almost get it :)

    And it works an absolute treat. Many thanks to you Alan.

  13. Alkorr
    Member
    Posted 5 years ago #

    Hi Alan, I just downloaded the version 0.1 but unfortunately, your plugin doesn't seem to work... Did you update the file with the modifications you indicate in this thread or is the file an old one?

    Let me know, your plugin is the one I was looking for for a long time, I really need it to work :)

  14. alanft
    Member
    Posted 5 years ago #

    still not quite right for 2.7. however my live site is now 2.7 so I need to get on the updates asap.

    see how the development version works for you in the meantime

  15. mljo
    Member
    Posted 5 years ago #

    Hi there

    This is exactly what I need as well - how is it going? It looks to be nearly there for 2.7 as I see a folder option but it only shows the default folder.

    I was really surprised to find this doesn't come as a standard option with WordPress - but who am I, as a clueless user, to complain?!

    I am much in awe of all of you who work on open source software for our benefit. Hope you have a breakthrough soon!

    All the best
    Lynne

  16. alanft
    Member
    Posted 5 years ago #

    i've got a bit distracted of late cos our hosts have decided to throttle our service dramatically. but I *will* be getting on this soon. promise.

    did you try the development version?

  17. mljo
    Member
    Posted 5 years ago #

    I have now and it works! Thanks again - essential plugin I think.

    Regards
    Lynne

  18. barney_1
    Member
    Posted 5 years ago #

    I've tried both the development version and the regular and have not been able to get it to work.

    I am able to add a directory but when I try to move a file it says "moving.." then it says "failed". I did make sure this directory exists and that it has permissions 777.

    Any idea where I'm going wrong?

  19. alanft
    Member
    Posted 5 years ago #

    no idea from what you say. tell me what it says on your Relocate Upload Settings page, specifically the values of the text fields you have set and the 'Default Location', and also the "Paths are relative to the blog root: xxx" text, and finally where you are trying to switch locations between. what sort of server are you on too could help.

  20. paintpops
    Member
    Posted 5 years ago #

    Thanks so much for all your help! I love this plugin. I had the same problem as barney_1 until I changed line 38. But right now I'm encountering another problem. Once I select a folder to relocate my file to, the thumbnail URL changes and therefore the thumbnail image becomes broken.

    This is how the thumbnail URL's supposed to look:
    http://site.com/scans/thumbnail.jpg

    But instead it looks like this:
    http://site.com/wp-content/uploads//home/site/public_html/scans/thumbnail.jpg

    What's up?

  21. alanft
    Member
    Posted 5 years ago #

    Good point about the line 38 fix - barney, check up thread for that - it might be what you need.

    paintpops - yes there is an issue there which i need to resolve. I *think* that its a shortcoming in the WP core code that assumes your media items will always be inside wp-content/uploads. i've been thinking about raising a ticket or whatever it is that you have to do on these projects. (i might dig out my findings on this and post here in a bit.)

    i have recently patched it on my own install with a filter - which is a bit poor - and i'll be putting it in the main code when i work out how to make it work generically. For now you can patch it with something like:

    add_filter( 'wp_get_attachment_url', "wp_get_attachment_url_base_folder");
    
    function wp_get_attachment_url_base_folder($url)
    {	$url=str_replace("/wp-content/uploads//home/site/public_html/","/",$url);
    	return $url;
    }

    put it in your theme's functions.php or add it to the plugin code

  22. alanft
    Member
    Posted 5 years ago #

    follow up, it's a failure of both WP and my plugin of course

    the wp_get_attachment_url function (a central function called eg by wp_get_attachment_thumb_url) in wp-includes/post.php does assume everything is in the upload folder or it falls back on the old-fashion "guid" of the attachment

    so it looks like the guid rewiring in the plugin is not getting it right.

    it's confusing cos the _wp_attached_file postmeta USED to be absolute, but at some point (2.6/2.7) they switched to being relative the uploads folder.

  23. alanft
    Member
    Posted 5 years ago #

    I didn't get that quite right. it USED to fallback to the guid, but the code in wp_get_attachment_url currently does this

    if ( 0 === strpos($file, $uploads['basedir']) ) //Check that the upload base exists in the file location
    	$url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); //replace file location with url location
    elseif ( false !== strpos($file, 'wp-content/uploads') )
    	$url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 );
    else
    	$url = $uploads['baseurl'] . "/$file"; //Its a newly uploaded file, therefor $file is relative to the basedir.

    which translates as

    if the /path/to/wordpress/wp-content/uploads path is in the file path, then it replace that path with http://yrdomain.com/wordpress/wp-content/uploads

    OTHERWISE

    if you can find "content/uploads" anywhere in the filepath replace text up to that with http://yrdomain.com/wordpress/wp-content/uploads

    OTHERWISE

    just put http://yrdomain.com/wordpress/wp-content/uploads/ on the start of the filepath (probably as this is the old relative to uploads filepath WP *used* to do)

    which means it will not fall back on the guid (which the plugin does rewire correctly!). I'd rather it didn't use that final otherwise and instead did fail back to the guid, as it means, you can no longer have media items outside of the uploads path! my plugin is violating that assumption, so I'm not going to raise a ticket to get that assumption changed. instead i'll work out the proper wp_get_attachment_url filter function to use (as shown for the specific case above)

  24. alanft
    Member
    Posted 5 years ago #

    excuse my long-windedness, they sort of work as notes to self and to update the dev version.

    HERE'S YOUR SHORT ANSWER...

    add this code on the end of relocate-upload.php

    add_filter( 'wp_get_attachment_url', "wp_get_attachment_url_base_folder");
    
    function wp_get_attachment_url_base_folder($url)
    {	$url=preg_replace('#(http://.*?/).*?/'.($GLOBALS['_SERVER']['DOCUMENT_ROOT']).'/#','\1',$url);
    	return $url;
    }

    i think this will work as a generic fix for the problem

  25. alanft
    Member
    Posted 5 years ago #

    a-and finally, i have updated the dev version with all the above fixes

    will tag it as an updated release soon, as it looks good here.

  26. paintpops
    Member
    Posted 5 years ago #

    thanks a bunch! =) it's working fine now and the thumbnails show up properly

  27. paintpops
    Member
    Posted 5 years ago #

    i know this may be asking for too much, but i just want to make a suggestion for batch relocation, if possible =) that would really save a lot of time from having to manually move each file.
    looking forward to the development version though!

  28. alanft
    Member
    Posted 5 years ago #

    oo, batch relocation might be fun to work out how to do. the items in the media library list have that tick box and there's the bulk actions drop down. i could see if i can add an action to that - or a second drop down next to it.

    may have time to fiddle with that :-)

  29. paintpops
    Member
    Posted 5 years ago #

    that would be awesome =) i'm currently using the "faster image insert" plugin as well and they have a mass edit function, so maybe something of that sort?

    http://blog.ticktag.org/2009/02/19/2765/

  30. Taschmahal
    Member
    Posted 5 years ago #

    Hi! I wanted to use your plugin, but it won't work.

    Whenever I want to relocate a picture to another folder, it says: "Failed".

    But the settings are right, there is no error. I'm Using WP 2.7
    The folders do exist. So why is this error?

Topic Closed

This topic has been closed to new replies.

About this Topic