(first posted: Aug 23, 2006)
(1933 Reads)
keywords: articles dynamic data uploads
Permalink
Uploading files in xaraya
Articles + standard uploads
This is the simplest case, and least flexible. Lets say you have a pubtype where the Body field contains an image file. Define the Body as property "File Upload".
Referencing it in a template
<img src="#$body#" alt="#$title#" />
Standard uploads Validations
- Base directory: relative path where files are uploaded and stored
- File extensions: permitted file extensions
- File size: max file size (also subject to you php.ini upload_max_filesize setting)
Caveats
Beware that some hooked transforms could screw up the File Uploads field if that field is transformed. For example, line break transforms performed by the HTML Module and/or BBCode Module. If you are using a built-in Articles field for File Upload, either don't hook a transforming module to this pubtype, or turn off the transform (in that module) (if its a Summary field you can also turn off transforms in the pubtype config settings).
In this case, you could just turning off linebreaks everywhere and then manually transform textarea fields in the template. Or, keep linebreaks on, and call strip_tags() on the uploads field. That's probably better, if and when the problem occurs.
Articles + Uploads module
Hook Uploads module to Articles.
Your "File Upload" field now use the Uploads module (acts like an "Upload" prop) instead of standard "file upload".
For example, Pictures pubtype, Body property File Uploads. The call to current() grabs the first elemet of the array (we assume its an array of one).
<xar:set name="fileInfo">current( xarModAPIFunc( 'uploads', 'user', 'showoutput', array('value'=>$body, 'multiple'=>false)) )</xar:set>
<img src="#$fileInfo['fileLocation']#" />
If you allow multiple attachments,
<xar:set name="img_output">xarModAPIFunc('uploads','user','showoutput',array('value' => $body, 'multiple' => true))</xar:set>
<xar:foreach in="$img_output" key="$fileId" value="$fileInfo">
<img src="#$fileInfo['fileLocation']#" />
</xar:foreach>
The fields in each $fileInfo are:
- fileName - original filename (w/o path info)
- 'fileLocation' - full url of file (e.g. src="$fileInfo['fileLocation']")
- fileDownload - relative file location
-
fileId - item id (in uploads module), (e.g. used in calls Images module
- fileSize - files size in bytes
- (others? check this)
The caveat about hooked transforms applies here too.
Configuring Uploads module
Via Admin > Global > Uploads > Modify Config
Uploads Directory:
File Uploads Property Validation
Multiple: yes/no
Methods: trusted, external, uploads, stored
Upload Directory:
Trusted Directory:
Other Rule:
Uploads Privileges
Note, if you want to constrain uploads to specific file or mime types, you do this in Privileges (not the upload property validation or uploads module configuration). Create a priv for the uploads module, and in "Specify instances that apply", tell it the Mime Type, Subtype, User, and/or File, with Access Level (minimum Submit).
Another thing, if you require uploads be approved (default state "submitted") the files may not be accessible until approved. This one got me because things were working fine, then seemed to stop. Turns out I was testing while logged in as Admin, then when testing as anon the uploaded pix weren't showing up (Uploads showout function returns null).
Content of the $myfield_output Array
[tile_image] => ;23
[tile_image_output] => Array
(
[23] => Array
(
[fileId] => 23
[userId] => 3
[userName] => Administrator
[fileName] => 10050m.gif
[fileLocation] => var/uploads/10050m.gif
[fileSize] => 541
[fileStatus] => 2
[fileType] => image/gif
[fileTypeInfo] => Array
(
[typeId] => 5
[subtypeId] => 153
)
[storeType] => 3
[mimeImage] => modules/mime/xarimages/image-gif.png
[fileDownload] => http://localhost/alumbiz/?module=uploads&func=download&fileId=23
[fileURL] => http://localhost/alumbiz/?module=uploads&func=download&fileId=23
[DownloadLabel] => Download file: 10050m.gif
[fileModified] => 1156709005
[fileDirectory] => imports
[fileHash] => 10050m.gif
[fileHashName] => imports/10050m.gif
[fileHashRealName] => imports/10050m.gif
[fileStatusName] => Approved
)
)
Articles + DynamicData + Uploads module
We can avoid the caveats by using DynamicData instead of a built-in articles field. For one, you dont run into the transform problems. And when dd is hooked to articles, a shortcut is provided -- the showoutput is already set in your $fieldname_output variable.
Thus, if your field is named "attachment", you say
<xar:foreach in="$attachment_output" key="$fileid" value="$fileinfo">
<a href="http://www.vaporbase.com/index.php#$fileinfo['fileDownload']#">#$fileinfo['fileName']#</a>
</xar:foreach>
H ere's something unexpected: I have Uploads hooked to Articles, and I have a DD field with Uploads property (lets called it "img"). Then, in an Xarpages template I do an articles getall on the Articles items, the img_output field in blank. (Other non-upload fields do have their _output field). It turns out I had to also hook Uploads to Xarpages for the img field, and then img_output contains the file info data.
Content of the myfield properties
Xarpages + DD
not different, just another example
<xar:set name="img_output">current( xarModAPIFunc( 'uploads', 'user', 'showoutput', array('value'=>$current_page['dd']['main_image'], 'multiple'=>false)) )</xar:set>
<img src="#$img_output['fileLocation']#" />
OK, now on to something different:
Tinymce Filebrowser
( didn't i write this up somewhere else?)
Files Module
<input type="file"> and Php
Lets look under the hood a bit, at how to use a regular HTML <input type="file"> and the Uploads module.
In normal forms processing with Php, a file input field puts its values in the global $_FILES array rather than a POST var.
Thus, in your template, the form:
<form method="post" name="post" id="post" action="doform.php" enctype='multipart/form-data'>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<input type="hidden" name="imageupload_attach_type" id="imageupload_attach_type" value="7" />
<input type="file" name="imageupload" id="imageupload" />
etc.
And in the doform.php :
echo "<pre>"; print_r($_FILES); echo "</pre>";
gives
Array
(
[imageupload] => Array
(
[name] => homeoffice.gif
[type] => image/gif
[tmp_name] => /var/tmp/phpHtehur
[error] => 0
[size] => 24255
)
)
To integrate the upload with the Uploads module directly (rather than via hooks), you can use api functions at this point . For example, if your forms processing is in php:
$fileinfo = xarModApiFunc('uploads','user', 'prepare_uploads', array('fileInfo' =>$_FILES['imageupload']) );
$imageinfo = xarModApiFunc('uploads','user', 'file_store', array('fileInfo' => current($fileinfo)) );
The results of these functions are:
$fileinfo:
Array
(
[boy.jpg] => Array
(
[fileType] => image/jpeg
[fileSrc] => /var/tmp/phpux97l9
[fileSize] => 6274
[fileName] => boy.jpg
[fileDest] => var/uploads/boy.jpg_6
[fileLocation] => var/uploads/boy.jpg_6
[isUpload] => 1
)
)
$imageinfo :
Array
(
[fileType] => image/jpeg
[fileSrc] => /var/tmp/phpux97l9
[fileSize] => 6274
[fileName] => boy.jpg
[fileDest] => var/uploads/boy.jpg_6
[fileLocation] => var/uploads/boy.jpg_6
[isUpload] => 1
[store_type] => 3
[fileId] => 87
)
The fileId field is whats to be saved in the File Upload field (with Uploads module hooked) of your item.
The inverse of this-- if you have a fileId and want the fileInfo[] stuff, you use this api call
$imageinfo = xarModApiFunc('uploads','user','db_get_file', array('fileId'=>$imageid));
if (!empty($imageinfo))
$imageinfo = current($imageinfo);
One more thing
Just a quick reminder, when you have a form with an uploads field be sure the enctype is
enctype='multipart/form-data'
and NOT
enctype='application/x-www-form-urlencoded'
else the file will not upload. Not sure why the former is used in Xar templates, but afaik we can use the latter enctype any time.




Uploading files in xaraya
Posted by: Steve Switzer on April 22, 2008 12:29 PM#