[pmwiki-users] back to an old story : conditional for attachments (fwd)

Hans design5 at softflow.co.uk
Thu Mar 27 13:10:40 CDT 2008


Thursday, March 27, 2008, 3:57:39 PM, Alain Castera wrote:

> One year ago, there seemed to be a thread on "conditional for 
> attachments". But I was unable to find a conclusive message, except of
> letting the "attach" markup do the job.

> But I need(!) a pure attachment existence test, really !

Try this: add to config file:

$Conditions['attachexists'] = 'UploadFileExist($pagename, $condparm)';
function UploadFileExist($pagename, $attachname) {
        global $UploadDir, $UploadPrefixFmt;
        $fname = explode("/", $attachname);
        $filename = end($fname);
        if(count($fname)==2) 
                $pagename = MakePageName($pagename, $fname[0]);
        if(count($fname)==3) 
                $pagename = MakePageName($pagename, $fname[0].".".$fname[1]);
        $uploaddir = FmtPageName("$UploadDir$UploadPrefixFmt", $pagename);
        $dirp = @opendir($uploaddir);
        if (!$dirp) return '';
        $filelist = array();
        while (($file=readdir($dirp)) !== false) {
                if ($file{0} == '.') continue;
                        $filelist[$file] = $file;
        }
        closedir($dirp);
        return in_array($filename, $filelist);
}

then use as conditional in wiki pages like
(:if attachexists filename.jpg:) filename.jpg is here!

if you got upload files in Group/ subfolders:
(:if attachexist Group/myfile.doc:) it's there!

if you got upload files in Group/PageName subfolders:
(:if attachexist Group/PageName/myfile.doc:) it's there!



  ~Hans




More information about the pmwiki-users mailing list