I have to create a form for uploading a file. What should be given in order to include an upload option. I tried giving 'enctype' as 'multipart/form-data'...but it doesn't work..any help??
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.
All you need to do is set 'enctype'=>'multipart/form-data' and make sure the name of fields are same in both form and the action page.
If you want to do it the elgg way:
$form_body = elgg_view('input/file', array(
'name' => 'upload'
));
$form_body .= elgg_view('input/submit', array(
'value' => 'Upload Now'
));
echo elgg_view('input/form', array(
'body' => $form_body,
'enctype' => 'multipart/form-data',
'action' => 'action/myfileupload'
));
--------------------------
In your init function in start.php you need to register an action:
elgg_register_action('myfileupload', elgg_get_plugins_path() . 'myplugin/actions/myfileupload.php');
--------------------------
Create an action to process the uploaded file (it's accisble via $_FILES['upload']) in myplugin/actions/myfileupload.php.
If you need to save the file in elgg data folder, you will need to define a new ElggFile. Look into files plugin for examples
Thats the full code you need to use. The previous post was made expecting you have coded the basic framework.
I am trying to implement this given code in my elgg test site as i need a upload file provision inside a form, every thing else is ok but code below display's 'EMPTY'.
if(!empty($_FILES['upload'])){system_message(' NOT EMPTY');}
else{system_message('EMPTY');}
Any ideas what could be the reason behind this?
Thank you in advance.
If you do error_log(print_r($_FILES['upload'), true); you will see that it's not empty. If you have included the file input in your form, $_FILES will not be empty. Use $_FILES['upload]['error'] instead.
Thank you for your quick reply but i am still not able to figure it out as what is the problem
upload.php (form file)
-----------------------------------------------------
<?php
$form_body = '<input type="file" name="upload" id="upload"/>';
$form_body .= elgg_view('input/submit', array(
'value' => 'Upload Now'
));
echo elgg_view('input/form', array(
'body' => $form_body,
'enctype' => 'multipart/form-data',
));
?>
-----------------------------------------------------
upload.php (action file) [code snippet form http://www.w3schools.com/php/php_file_upload.asp]
-----------------------------------------------------
<?php
if ($_FILES["upload"]["error"] > 0)
{
echo "Error: " . $_FILES["upload"]["error"];
}
else
{
echo "Upload: " . $_FILES["upload"]["name"];
echo "Type: " . $_FILES["upload"]["type"];
echo "Size: " . ($_FILES["upload"]["size"] / 1024)."Kb";
echo "Stored in: " . $_FILES["upload"]["tmp_name"];
}
?>
Can you please take a look at the code and let me know if everything i am doing is ok or do i need some changes.
Thank you once agian
Every thing in else block shows blank as output
updated action file
if($_FILES['upload']['error']>0)
{
register_error('Error');
}
else
{
$info = pathinfo($_FILES['upload']['name']);
$ext = $info['extension'];
$newName = "projectTest.".$ext;
$target = elgg_get_plugins_path().'project/roles/user/pFile/33/'.$newName;
move_uploaded_file( $_FILES['upload']['tmp_name'], $target);
}
no file is at given target.......i will update if can find any solutions
This is what i found in my error log file related to my action file
PHP WARNING: 2013-10-07 18:58:38 (UTC): "error_log() [<a href='function.error-log'>function.error-log</a>]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()" in file C:\\wamp\\www\\Elgg_for_testing_purposes\\elgg-1.8.16\\mod\\project\\actions\\project\\upload.php (line 21)
- Previous
- 1
- 2
- Next
You must log in to post replies.