Jump to content

Reversing files


matjazp
 Share

Recommended Posts

I would like to reverse the order of files listed in File field type. File upload form would be displayed first, then the list of files, new files would be listed at the top, older at the bottom. Possible? 

Link to comment
Share on other sites

Yes, it would be easier. Maybe Ryan will see this post ...

Another question: I have a page with two Filetype fields: files (multiple files are allowed) and onefile (maximum file upload = 1). User is allowed to access just onefile (I'm using fredi), he uploads the file and then this new uploaded file should get appended to other files:

 

if($page->onefile) {
    $page->setOutputFormatting(false);
    $page->files->append($page->onefile);
    $page->save();
    $page->setOutputFormatting(true);
}

 Now I would like to "empty" onefile field to be ready for the next upload but without deleting the actual file itself.

Link to comment
Share on other sites

something like this should work:

$tempPath = '/some/writeable/path/on/your/server/';

if($page->onefile) {
    $dest = $tempPath . $page->onefile->name;
    if(copy($page->onefile->filename, $dest)) {    // http://www.php.net/manual/en/function.copy.php
        $page->setOutputFormatting(false);
        $page->files->add($dest);
        unlink($dest);
        $page->onefile->removeAll(); // or $page->onefile->delete($page->onefile->filename); ?
        $page->save();
        $page->setOutputFormatting(true);
    }
}
Link to comment
Share on other sites

A problem arises when uploading multiple files though - there is no order as first to finish uploading (smallest) will be first in the list and so on.

Single files - yes it would be nice to have it optional but I have a feeling we've discussed this before (if anyone can find the topic though and link to ryan's reply that would be great).

Link to comment
Share on other sites

Oh, that with the reverse order isn't an easy one. Files / Images are uploaded via ajax, and multiple of them asynchron, as Pete already said.

On a site where the owner uploads many images at once via zip, I use a bootstrap script to let him rebuild the sortorder by name. The script only get invoked manually by clicking on a link. It is in the template, like the edit-link if a user is logged in and the page is editable:

	if($page->editable()) {
		echo "<a class='nav' id='sortimages href='/pwsortimages.php?id={$page->id}'>SortImages</a>"; 
	}

Here are the script code I use. If this can be handy for you, until someone comes with a better solution, you can sort by -created or -modified I think.

<?php

// define userroles that are allowed to work with this script, like: superuser|editor|author
$userRolesSelector = 'superuser|editor';

//-------------------------------------------------------------------------------------------//

// check if we have got an id
$pageId = isset($_GET['id']) && is_numeric($_GET['id']) ? intval($_GET['id']) : false;
if(false===$pageId) {
	header('HTTP/1.1 403 Forbidden');
	exit(3);
}

// bootstrap PW
require_once(dirname(__FILE__) . '/index.php');

// check user-account
if( ! wire('user')->hasRole($userRolesSelector)) {
	header('HTTP/1.1 403 Forbidden');
	exit(2);
}

// check if id points to a valid page
$p = wire('pages')->get($pageId);
if($p->id!=$pageId || !$p->editable()) {
	header('HTTP/1.1 403 Forbidden');
	exit(1);
}

// now we reset the order for the images / files
$p->of(false);
$p->images->sort('name');   // -created ??
$p->save();

// and redirect to the page edit screen
$url = wire('pages')->get(2)->url . "page/edit/?id=$pageId";
header("location: $url");
exit(0);
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...