• hello,
    i used this code to change the upload directory according to each custom type:

    function wpse_16722_type_upload_dir( $args ) {
    
        // Get the current post_id
        $id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : '' );
    
        if( $id ) {
           // Set the new path depends on current post_type
           $newdir = '/' . get_post_type( $id );
    
           $args['path']    = str_replace( $args['subdir'], '', $args['path'] ); //remove default subdir
           $args['url']     = str_replace( $args['subdir'], '', $args['url'] );
           $args['subdir']  = $newdir;
           $args['path']   .= $newdir;
           $args['url']    .= $newdir; 
    
           return $args;
       }
    }
    add_filter( 'upload_dir', 'wpse_16722_type_upload_dir' );

    this actually works perfectly so i uploaded files using your plugin into their custom directories.
    files will be located in :
    /wp-content/uploads/custom_post/file2.pdf

    but, at the front end, when a visitor try to download the file he got a blank page.
    is there any solution please?
    kind regards

    https://wordpress.org/plugins/download-attachments/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author dFactory

    (@dfactory)

    The download process is done with da_download_attachment function located in plugin folder/includes/functions.php

    You’d have to chek what is the filepath you have in this function, right at the top 3 lines (print_r or sth):

    $filepath = $uploads['basedir'] . '/' . $attachment;

    There’s a filter hook to modify the filepath if needed, but in any case you have to know what is the filepath there.

    This can definitely be solved.

    Thread Starter Nisr

    (@nisr)

    thanks for replying 🙂
    ok let me be more clear,
    the code that i use will put attachments in different upload directories according to each post type. this means, for the publications post type, the attachments will be uploaded to :
    /wp-content/uploads/publications/file2.pdf
    while for the communications post type they will be uploaded to :
    /wp-content/uploads/communications/file3.pdf
    So, i need the plugin to make visitors able to download these attachments from any upload directory where they were uploaded to.
    i hope you can try the code in any theme function.php and tell me what i have to do. because i looked inside the plugin function.php and i found the line you mentioned but i really didn’t know what to do.

    function da_download_attachment($attachment_id = 0)
    {
    	if(get_post_type($attachment_id) === 'attachment')
    	{
    		$uploads = wp_upload_dir();
    		$attachment = get_post_meta($attachment_id, '_wp_attached_file', true);
    		$filepath = $uploads['basedir'].'/'.$attachment;
    
    		if(!file_exists($filepath) || !is_readable($filepath))
    			return false;
    
    		// if filename contains folders
    		if(($position = strrpos($attachment, '/', 0)) !== false)
    			$filename = substr($attachment, $position + 1);
    		else
    			$filename = $attachment;
    
    		if(ini_get('zlib.output_compression'))
    			ini_set('zlib.output_compression', 0);
    
    		header('Content-Type: application/download');
    		header('Content-Disposition: attachment; filename='.rawurldecode($filename));
    		header('Content-Transfer-Encoding: binary');
    		header('Accept-Ranges: bytes');
    		header('Cache-control: private');
    		header('Pragma: private');
    		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    		header('Content-Length: '.filesize($filepath));
    
    		if($filepath = fopen($filepath, 'rb'))
    		{
    			while(!feof($filepath) && (!connection_aborted()))
    			{
    				echo fread($filepath, 1048576);
    				flush();
    			}
    
    			fclose($filepath);
    		}
    		else
    			return false;
    
    		update_post_meta($attachment_id, '_da_downloads', (int)get_post_meta($attachment_id, '_da_downloads', true) + 1);
    
    		exit;
    	}
    	else
    		return false;
    }

    waiting for your reply
    best regards

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘blank page after changing upload directory’ is closed to new replies.