In my plugin , i can upload multiple files together in a single file upload.
In that, is it possible to add the metadata in a for loop adding the $key value to the end.
Please check the below code.
if ((isset($_FILES['academics']))) {
foreach( $_FILES['academics'] as $name => $parts ) {
foreach( $parts as $key => $part ) {
$files[$key][$name] = $part;
}
}
$academicdocumentPath = $CONFIG->pluginspath."imam_users/documents/faculties/academics/".$guid;
if (!is_dir($academicdocumentPath))
$newDir = mkdir($academicdocumentPath);
$allowedExts = array("doc","docx","pdf", "jpeg", "jpg", "png");
if ($files) {
foreach($files as $key=>$file) {
$target_path = $academicdocumentPath .'/' . basename($file['name']);
$temp = explode(".", $file['name']);
$extension = end($temp);
if (in_array($extension, $allowedExts)){
if ($file['error']> 0)
{
echo "Return Code: " . $file['error'] . "
";
}
else {
if (!file_exists($target_path))
{
if(!move_uploaded_file($file['tmp_name'], $target_path)){
register_error(elgg_echo("error:while:moving"));
forward($_SERVER['HTTP_REFERER']);
}else{
$new_user->academics[$key] =$file['name'];
$new_user->academicfilepath[$key] = $target_path;
$new_user->save();
}
}
}
}
else {
register_error(elgg_echo("file:notsupport:format"));
forward($_SERVER['HTTP_REFERER']);
}
}
}
}else{
register_error(elgg_echo("academicfile:not:uploaded"));
forward($_SERVER['HTTP_REFERER']);
}
Please check the underlined codes. Is it possible to add like this.
My target is, if there are 3 files in academic file uploading , i want to add this like
$newuser->academic0= $file['name'];
$newuser->academic1= $file['name'];
$newuser->academic2= $file['name'];
Does it work?
Please give me a response soon..
info@elgg.org
Security issues should be reported to security@elgg.org!
©2014 the Elgg Foundation
Elgg is a registered trademark of Thematic Networks.
Cover image by RaĆ¼l Utrera is used under Creative Commons license.
Icons by Flaticon and FontAwesome.
- Team Webgalli@webgalli
Team Webgalli - 0 likes
- Al-ayam@sintu
Al-ayam - 0 likes
- Al-ayam@sintu
Al-ayam - 0 likes
- Team Webgalli@webgalli
Team Webgalli - 0 likes
- Brett@brett.profitt
Brett - 0 likes
- Matt Beckett@Beck24
Matt Beckett - 0 likes
- Al-ayam@sintu
Al-ayam - 0 likes
- Al-ayam@sintu
Al-ayam - 0 likes
- Matt Beckett@Beck24
Matt Beckett - 0 likes
- Al-ayam@sintu
Al-ayam - 0 likes
You must log in to post replies.It seems you are uploading the files to the mod directory. This is an unsafe and wrong method. Whatever the user uploads, please save it in the elgg's data directory (which is not web accessible).
hi Team Webgalli,
First I saved it there..But I canot call it back in the download.php page. Thats why I changed like this.Please find the below code and help me to rectify the issue.
Acion.php
if (isset($_FILES['cprfile']['name']) && !empty($_FILES['cprfile']['name'])) {
$allowedExts = array("doc","docx","pdf", "jpeg", "jpg", "png");
$prefix = "students/cpr/".$new_user->guid;
$ext = pathinfo($_FILES['cprfile']['name'], PATHINFO_EXTENSION);
if (in_array($ext, $allowedExts))
{
if ($_FILES["cprfile"]["error"] > 0)
{
echo "Return Code: " . $_FILES["cprfile"]["error"] . "<br>";
}
else {
$filehandler = new ElggFile();
$filehandler->owner_guid = $new_user->guid;
$filehandler->setFilename($prefix .'.'. $ext);
$filehandler->open("write");
$filehandler->write(get_uploaded_file('cprfile'));
$filehandler->close();
$new_user->cprfile = $new_user->guid.".".$ext;
$new_user->save();
}
}
else
{
register_error(elgg_echo("file:notsupport:format"));
forward($_SERVER['HTTP_REFERER']);
}
}
View.php
$cprfilename =$vars['user']->cprfile;
$downloadCpr = $CONFIG->wwwroot. "pg/simpleusermanagement/downloadCpr/cprfile/".$vars['user']->guid;
$single_addition .= '<div class="simpleusermanagement_single_user_parameter">CPR - <a href="'.$downloadCpr.'"><span style="width:90%;">' . $cprfilename . '</span></a> </div><br clear="all">';
Start.php (page handler)
case "downloadCpr":
set_input('cprfilename',$page[1]);
set_input('userId',$page[2]);
include($CONFIG->pluginspath . 'simpleusermanagement/downloadCpr.php');
break;
downloadCpr.php
<?php
require_once(dirname(dirname(dirname(__FILE__))) . '/engine/start.php');
global $CONFIG;
$file = get_input('cprfilename');
$guid = get_input('userId');
$hidden_entities = access_get_show_hidden_status();
access_show_hidden_entities(true);
if(($file == 'cprfile') && $guid > 0){
$userEntity = get_entity($guid);
if(!empty($userEntity)){
$success = false;
$filehandler = new ElggFile();
$filehandler->owner_guid = $userEntity->guid;
$filehandler->setFilename("students/cpr/" . $userEntity->guid);
if ($filehandler->open("read")) {
if ($contents = $filehandler->read($filehandler->size())) {
$success = true;
}
}
if($success){
$mime = "application/octet-stream";
$filename = $file.'_'.$userEntity->$file;
// fix for IE https issue
header("Pragma: public");
header("Content-Disposition: attachment; filename=\"$filename\"");
ob_clean();
flush();
echo $contents;
exit;
}else{
register_error(elgg_echo('Sorry! no data available!'));
forward($userEntity->getURL());
}
}else{
register_error(elgg_echo('Sorry! This is not a valid download URL!'));
forward();
}
}else{
register_error(elgg_echo('Sorry! This is not a valid download URL!'));
forward();
}
access_show_hidden_entities($hidden_entities);
Please find the problem with this..
Thanks
Here for each user's against I have multiples files saved. How can I fetch them back. Or how can I save that details against that user?? like metadata or like relationship.
Please take a look at the bundled "files" plugin or the action file of group creation.
I updated the title to be more specific about the question being asked.
You can see an example of multiple file uploads in the showcase plugin
Here's where they are being saved: https://github.com/Elgg/showcase/blob/master/actions/showcase/save.php#L131
I saved the file in the elgg datat folder. But while calling them back i need to download the files when we click on the name.
I used the same way in the file plugin's "download.php"
But it shows an error like "The image canot be displayed,because it contains errors". Any body know why?
I want to give one download button for the files I uploaded. How can I add the path of the images there?
Please find the below code
$cprData = '<ul class="elgg-gallery elgg-showcase-screenshots">';
foreach ($cprfile as $key=>$cpr) {
$cprId = $cpr->guid;
$cpr_url = $vars['url'] ."mod/simpleusermanagement/download.php?file_guid=".$cpr->guid;
$cprData .= '<li>';
$cprData .="<a href=\"$cpr_url\" target=\"_blank\">$cpr->cprfilename</a>";
$cprData .= '</li>';
}
$cprData .= '</ul>';
download.php
<?php
/**
* Elgg file download.
*
* @package ElggFile
*/
require_once(dirname(dirname(dirname(__FILE__))) . "/engine/start.php");
// Get the guid
$file_guid = get_input("file_guid");
// Get the file
$file = get_entity($file_guid);
if ($file) {
$mime = $file->getMimeType();
if (!$mime) {
$mime = "application/octet-stream";
}
$filename = $file->cprfilename;
// fix for IE https issue
header("Pragma: public");
header("Content-type: $mime");
if (strpos($mime, "image/")!==false)
header("Content-Disposition: inline; filename=\"$filename\"");
else
header("Content-Disposition: attachment; filename=\"$filename\"");
$contents = $file->grabFile();
$splitString = str_split($contents, 8192);
foreach($splitString as $chunk)
echo $chunk;
exit;
} else {
register_error(elgg_echo("file:downloadfailed"));
forward();
}
But by using this, I canot do the download action..
Please help.
Make sure $file->cprfilename is the correct path to the file
Ideally you should use the ElggFile class and the method getFilenameOnFilestore()
I used the same file uploading like file plugin and also used the same code like in file's download.php page.
But still I got the error " The image cannot be displayed because it contain errors".
How can i solve this problem?
Thanks