View
<form method="post" enctype='multipart/form-data' action="<?php base_url();?>edituserpic" name="form_editpic" id="form_editpic" class="avatar">
<div class="slim"
data-label="Drop your avatar here"
data-size="240,240"
data-ratio="1:1">
<input type="file" name="avatar" required />
</div>
<button type="submit">Upload now!</button>
</form>
Controller
public function edit_user_pic() {
$data['baseurl'] = $this->config->item('base_url');
// Pass Slim's getImages the name of your file input, and since we only care about one image, postfix it with the first array key
$image = $this->slim->getImages('avatar')[0];
*/
// Grab the ouput data (data modified after Slim has done its thing)
if ( isset($image['output']['data']) )
{
// Original file name
$name = $image['output']['name'];
// Base64 of the image
$data = $image['output']['data'];
}
}
I'm just stuck in server side how to save to get output image and save to database.
I am getting $name undefined. This mean output is empty.
If anyone used it and able to help that would be great.
If you've setup to use a different name than "slim", pass it along.
$images = Slim::getImages('avatar');
$image = $images[0];
It works for me.
Related
Hi I am trying to integrate Dropzone.js in my app and using Laravel Framework. I have a form with below code,
<form method="post" action="{{url('/example/fileupload')}}"
enctype="multipart/form-data" class="dropzone" id="my-awesome-dropzone">
#csrf
<input type="submit">
</form>
The laravel controller attached with this form has below code in which I am just trying to get the name if the image which is dropped in dropzone area,
public function fileupload(Request $request)
{
$file = $request->file('file');
$filename = $file->getClientOriginalName();
echo $filename;
}
After clicking submit button it shows me below error,
Call to a member function getClientOriginalName() on null
Dont know what I am doing wrong here, because when I try to run the same code with simple
<input type="file" name="file">
it shows me the name of the uploaded image file which I want. Any suggestion or fix? Thanks
Try changing the "Input" class for "Request" ... I believe it will work. The error is being reported because there is no method on the object you are calling.
public function fileupload(Request $request)
{
$file= $request->file('file');
$fileName = $image->getClientOriginalName();
echo $fileName;
}
What I want to do is upload an image to my sql database using an html form and then retrieving it from the database to display it. I`ll show you some code so you can understand what I am trying to do.
1st EDIT: Just linking an image to show the database table I am uploading the image to. The row type is blob --> https://imgur.com/a/KBlNWWN in the code below I edited out the other rows of the DB table, but code for them is actually in.
This is the form that is uploading the image:
<form method="POST" action="/~gd339/ci/index.php/Add/postEvent/<?php echo $societyId ?>" enctype="multipart/form-data">
Event image: <input type="file" name="image"/> <br>
<input type="submit" name="POST" value="Post Event!" class = "unique"/>
What it does is that it posts the data to the postEvent controller:
public function postEvent($society_id) {
$this->load->model('Events_model');
$image = file_get_contents($_FILES['image']['tmp_name']);
$this->Events_model->addEvent($image);
}
Note that to get the image from the form, I am using $image = file_get_contents($_FILES['image']['tmp_name']); data retrieved using post is then sent to addEvent in my model:
public function addEvent ($image) {
$image1 = $this->db->escape($image);
$sql = "INSERT INTO events (event_image) VALUES ($image1)";
$query = $this->db->query($sql);
}
To retrieve the image I am using an image tag with a src that calls a function in the controller:
<img src="http://dragon.maple.as.re/~gd339/ci/index.php/add/getEventImage/<?php echo $row['event_id'] ?>">
public function getEventImage($event_id){
$this->load->model('Societies_model');
$image = $this->Societies_model->getEventImage($event_id);
header("Content-type: image/jpeg");
print($image);
}
code for getEventImage:
public function getEventImage($event_id) {
$data = '';
$sql = "SELECT event_image FROM events WHERE event_id = $event_id";
$query = $this->db->query($sql);
if ($query->num_rows()) {
$data = $query->row_array();
$data = $data['MA_PHOTO'];
$query->free_result();
}
return $data;
}
This results in the image just not showing up. I have been reading other examples here on stackoverflow and all of them, except for 1, use the CI upload library to do what I am trying to do. Is the library the only way to retrieve images using CI or is there something wrong in my code preventing the images to appear when called?
Thanks a lot to whoever will have the patience to go through my code. I know it is a lot but it is the first time I am using CI so im still learning.
I have done a similar process without using CI_Upload.
A useful debug is, using some db tool, save the blob to a file and try to display it (using mspaint, image viewer, etc...)
Before I save the image on database, I use the following function to resize (as I don't need it big) and convert(ensure) that it is a jpeg.
private function get_image_resized($data){
$data = smart_resize_image (
null,
$data,
250,
0,
true,
'return');
$temp_file = tempnam(sys_get_temp_dir(), 'ImageResize');
imagejpeg($data, $temp_file, 100);
$data = file_get_contents($temp_file);
unlink($temp_file);
return $data;
}
smart_resize_image can be found here:
To display the image
function imagem($cd_produto){
$im_produto = $this->produto_model->get_imagem ( $cd_produto );
header ( "Content-type:image/jpeg" );
echo $im_produto;
}
Hope it helps.
What I'm trying is to set an image uploader with Laravel 5.
Here is the form.
<form action="/edit/{{$id}}" method="POST" enctype="multipart/form-data">
{!! csrf_field() !!}
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<input type="hidden" name="_method" value="PUT">
//There are other tags (including other input) here
<input type="file" name="pic" accept="image/*">
<input type="submit" value="Apply Changes">
</form>
Here is the controller function which will be run from the form above.
public function update($id)
{
$this->model->find($id)->fill($this->request->all())->save();
if ($this->request->hasFile('pic')) {
$file = $this->request->file('pic');
$name = $file->getClientOriginalName();
$file->move('assets/serviceimages/', $name);
}
return redirect('/blahblah');
}
As you might have noticed, this doesn't store the uploaded file under the assets/serviceimages/ directory, which is what I've been trying.
Basically I've been following this tutorial, but apparently I'm missing something and totally clueless about what it is, even after I had a look at the API documentation.
Inside of the if statement of the controller function, it was confirmed that the $file and $name had what I expected.
Therefore, I suspect that the problem lies in the move() function.
Any small piece of advice will be appreciated, since this is my first time that I've tried to set an uploader.
Permissions if you are on Linux. If that doesn't work try it this way:
$image = $request->file('pic');
$destinationPathImg = '/your path/images/';
if (!$image->move($destinationPathImg, $image->getClientOriginalName())) {
return 'Error saving the file.';
}
Also, this need to be added in ur ()
public function update(Request $request, $id)
I found having to use the Storage adapter to use the full file path worked for me.
$storageAdapter = Storage::disk('v1')->getDriver()->getAdapter()
$full_path_before = $storageAdapter->applyPathPrefix($oldPath);
$full_path_after = $storageAdapter->applyPathPrefix($newPath);
if (!File::exists($full_path_before)){
throw new \Exception("Error moving folders");
}
$move_files = $full_path_after !== $full_path_before;
if ($move_files){
File::move($full_path_before, $full_path_after);
}
Use This Method This Will Work 100%
if($request->hasFile('filename')){
$file = $request->filename;
$file_new_name = $file->move("upload/posts/", $file->getClientOriginalName());
$post->filename = $file_new_names;
}
Remember filename is your < img name="filename" >
I am using ZF2.
I am sending a file via a HTML form not generated by Zend/Form. Now I am trying to get that file as i would normally do with $_FILES. I tried the below PHP code but all I get in the print_r is Array ( [upload] => bnk.csv )
How do I get all the metadata such as temp_dir of that file???.... I am new to Zend so dont know if I am forced to create the form with Zend/Form which seems silly since I only have a file upload field thats a lot easier to manage directly from the view.
$request = $this->getRequest();
if($request->isPost()){
$post= array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
print_r($post);
}
<form id="upload" method="post" action="bnk/upload/csv" enctype="multipart/form-data">
<label for="upload">Upload CSV File</label>
<input type="file" name="upload" />
<input type="submit"/>
</form>
/* upload */
$upload = new Zend_File_Transfer_Adapter_Http();
$upload->setDestination("folder for upload");
$upload->addValidator('Size',false, 1024 * 1024 * 5 ); //size limit
$upload->addValidator('Extension',false,'zip,rar'); //extension limit
$files = $upload->getFileInfo();
foreach($files as $file=>$info){
//if file is valid
if($upload->isValid($file)){
//confirm to upload
$upload->receive($file);
}
}
I use Zend_File_Transfer_Adapter_Http for uploading
I am trying to add image upload function to my view where I am creating model. I tried many sample codes here but I can't get to full path to file. My code looks like this:
public function submit() {
jimport ( 'joomla.filesystem.file' );
// Check for request forgeries.
JRequest::checkToken () or jexit ( JText::_ ( 'JINVALID_TOKEN' ) );
// Initialise variables.
$app = JFactory::getApplication ();
$model = $this->getModel ( 'createaction' );
// Get the data from the form POST
$data = JRequest::getVar ( 'jform', array (), 'post', 'array' );
echo $data['image']; <-- here
$createdItem = $model->createItem ( $data );
if ($createdItem) {
$redirect = JRoute::_ ( 'index.php?option=com_akcehned&view=actions', false );
$this->setRedirect ( $redirect, "Akce byla vytvořena" );
} else {
echo "<h2>Omlouváme se, ale něco se stalo špatně</h2>";
}
return true;
}
Part of file input in xml:
<field
name="image"
type="file"
description="COM_AKCEHNED_FORM_DESC_CREATEACTION_IMAGE"
label="COM_AKCEHNED_FORM_LBL_CREATEACTION_IMAGE"
size="10"
accept="image/*" />
Where I tried to echo file file input I get just name (image_name.jpg and so) but I need fullpath right? I saw examples with ['tmp_name'] but It's not working for me.
I tried code like this:
$jinput = $app->input;
$files = $jinput->files->get('jform');
$file = $files['image'];
echo $file;
echo $file['tmp_name'];
But it's not working for me either. I just get empty values. Can someone give me working block of code where I get data from other inputs and fullpath to file for upload? It's for joomla 2.5, Thanks
You need to make sure the the form tag contains enctype attribute and that it is set to "multipart/form-data" if your are going to upload files.
eg
<form action="" method="post" enctype="multipart/form-data">
Also see: What does enctype='multipart/form-data' mean?
Did you try DPAttachments, it takes only three lines of code to integrate attachment support into your component.
if (JLoader::import('components.com_dpattachments.libraries.dpattachments.core', JPATH_ADMINISTRATOR)) {
echo DPAttachmentsCore::render('com_demo.item', $object->id);
}
Drag and drop and copy paste from your clipboard (no need to save the file to your hard disk) is supported. Perhaps you want to give it a try. If not here is a link to the Github repo how the upload function is implemented:
https://github.com/Digital-Peak/DPAttachments/blob/master/com_dpattachments/admin/models/attachment.php#L50
[I'm the author of this component]