I am developing a Laravel application.Its have a form which have image upload field and its also not a required field. Now i want to do if anyone submitting form without uploading image then a default random image will be inserted into database from public/images/default directory. How can i do that with Laravel? I have 10 images of that directory (ex: 1b.png, 2b.png, 3b.png....)
<form method="POST" action="{{ route($base_route.'.eventEdit', ['event_id' => $event->event_code]) }}" accept-charset="UTF-8" id="general_form" novalidate="novalidate">
<input name="name" type="text" value="something" class="name">
<input name="name" type="file" value="DefaultImageName" class="image">
<button class="btn btn-default"> Submit </button>
</form>
Add this code to your controller action
$images = scandir(public_path('images/default'));
$image = $request->file('DefaultImageName', $images[mt_rand(0, count($images -1))]);
I would't advise storing images in the database. The approach I take is usually, I store in the database a name, alias and path to the file.
You can do this depending on the pattern you're using on the application, but for your situation, what I'd do is create a mutator that validates whenever you're going to insert a record, if certain field is empty, it does something (if you're using eloquent I assume).
https://laravel.com/docs/5.2/eloquent-mutators
public function setImageAttribute($value)
{
if (empty($value)) {
$this->attributes['image'] = //fetch your image and apply your logic
} else {
$this->attributes['image'] = value;
}
}
Otherwise, I'd create a formrequest that validates if a certain field is empty, I place some content in it (and this way, when it reaches the insertion code it already contains an image)
https://laravel.com/docs/5.2/validation
Related
My teacher,he was uploading and retrieving images from folder with ID recognition. It works with database.
So, when we upload an image, it will be stored in a folder with it's name changing automatically with ID.
Example: The upload form must be on "yourwebsite.com/yourpages/3144242" so the image will be stored as "3144242" and blablabla the same for other pages with ID
This is how he upload and retrieve it
<?php
$ff = './assets/images/kegiatan/'.$_SESSION['idkeg'].'.jpg';
if( file_exists( $ff ) ) {
<img src="<?= base_url()?>assets/images/kegiatan/<?=$_SESSION['idkeg']?>.jpg" />
<?php
}
?>
Retrieve images part:
if( file_exists( $ff ) ) {
<img src="<?= base_url()?>assets/images/kegiatan/<?=$_SESSION['idkeg']?>.jpg" />
<?php
}
?>
However, I change it a bit so I don't have to use a database and $_SESSION things. With this:
<?php
$ff = './assets/images/kegiatan/'.$this->input->post('img_name').'.jpg';
?>
<form id="form" class="kart" action="<?= base_url()?>gallery/upload2" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="text" class="form-control" name="img_name" required="required">
</div>
<div class="form-group">
<input type="file" class="form-control" name="foto">
</div>
<div class="form-group">
<button type="submit" class="btn btn-common" value="Update"><i class="fa fa-upload fa-2x"> Upload</i></button>
</div>
</form>
It works uploading images into folder "kegiatan" (means activity) with simply write the names of images in a form. The img_name that you have input, now become the name of that image.
Now I have a little problem to retrieve that images. How?
mistake in your form I realized is asking user to enter image name and get image from folder by using that input value.
First of all, I haven't tried upload an image with codeigniter but I will answer reference to php itself.
When we get an image as input from user by form, then we use "move_uploaded_file" function in php and we send temp_file, path with file name arguments to this function to save the file user selected in the form we prepared.
move_uploaded_file(string $filename , string $target);
// $filename: I use $_FILES['getFileUpload']['tmp_name'] for this argument
// $target: I define path with file name, eg: uploads/files/new_file_25.jpg for jpg image
So, you souldn't ask user to enter file name, because you will deifne it. Or, you must use "img_name" that you asked user to enter as file name which is most probably won't satisfy your ID prediction.
I hope this will help you.
I have a scenario where I have to make use of a form the old fashioned why like this;
<form action="{{ path('admin_app_address_import') }}" method="post" name="form_import">
<div class="form-group">
<label for="inputFile">File input</label>
<input type="file" id="inputFile" accept="text/csv" name="inputFile">
<p class="help-block">Select a CSV file to import.</p>
</div>
</form>
And then get the parameters from the form like this inside my action;
public function importAction(Request $request) {
$file = $request->files->get('inputFile');
var_dump($file);exit;
//...
}
But I keep on getting null. How can I get the file I try to read from? I don't need to store it in a database whatsoever, I just need to read the content to upload that to the database. I'm making a CSV import action, but I can't seem to get the file object.
When doing var_dump($request->request->all()); I do get the filename, but that won't work right?
When allowing file uploads, the encoding type of the form must be set to multipart/form-data:
<form ... enctype="multipart/form-data">
I am trying to submit file inside a form to Laravel backend. It posts all the other text fields but not file (when I dd($request->all()))
<form class="form-horizontal" role="form" method="POST" action="" enctype="multipart/form-data">
<input id="item_price" type="item_price" class="form-control" name="item_price" value="">
<!-- other inputs -->
<input type="file" id="product_image" name="product_image" onchange="previewImage(this);"/>
<img id="previewing" src="{{asset('noImage.gif')}}" style="width: 100%; height: 100%" />
</form>
item_price seems on dd($request->all()) and also other fields are seen, but no file input
When I display the image with jQuery (previewImage) before submitting the form, it shows the photo. But after I submit the form, on Laravel side it shows all the other fields and values but not any for file (when I use dd($request->all()).
On php.ini, max-post-size is 100M.
jQuery('#previewing').click(function() {
uploadImageClicked();
});
function uploadImageClicked() {
jQuery('#product_image').click();
}
dd($request->hasFile('product_image') returns false
In Laravel (which should be tagged in your question) it's not part of the Request class with the other form values, it's in the file method.
See https://laravel.com/docs/5.3/requests#retrieving-uploaded-files
$file = $request->file("image"); is what you're looking for.
I'm using codeigniter for my project and I need to read contents of files. So,I need validation to check whether the file is selected or not.
Here is my code in controller
$this->form_validation->set_rules(
'estimation_file',
'Project Estimation File',
'required'
);
But while choosing a file it shows error saying - The Project
Estimation File field is required
In codeigniter you cannot check the validation of two dimension array or file field with form_validation, instead you can check it after posting the data.
$this->form_validation->set_rules('validation_check','any required field','required');
if($this->form_validation->run()==FALSE)
{
// your code before posting...
}
else
{
// check the file posting
if($_FILES['estimation_file']['name']!='')
{
// if file selected or not empty
}
else
{
// if file not selected | empty | redirect
}
}
do not forget to write enctype="multipart/form-data" within the form field, otherwise your file field will not pass the value of two dimension array.
<form method="post" enctype="multipart/form-data" name="upload_form" action="">
<input type="hidden" name="validation_check" value="TRUE" />
<input type="file" name="estimation_file" value="" />
<input type="submit" value="Post" />
</form>
I am using plupload to upload image files. My goal is to save the name of the uploaded thumb_nail files to a session, in case user leaves page or an submit error happens.
Plupload creates file_names in this manner:
<div id="files">
<input type="hidden" id="p1adsfucka1h0p1s0624cauu623" name="files[]" value="p1adsfucka1h0p1s0624cauu623.jpg">
<input type="hidden" id="p1adsfucka1h0p1s0624cauu624" name="files[]" value="p1adsfucka1h0p1s0624cauu623.jpg">
</div>
from plupload functions I want to call this function:
function autosave_form_cl(session_name){
console.log($("input[name=files").val() + $('#title').val());
$.post("/act_autosave_formdata.php", {
session_name:session_name,
cellphone: $('#cellphone').val(),
files: $("input[name=files").val()
} );
}
This returns an undefined for the value of the file fields. How could I access and save those names? Submitting and saving the values to a session works by accessing $_POST.
Collect all [name=files] first then pass it to your $.post.
I assume what's inside <div id="files"> is all inputs with [name=files]
function autosave_form_cl(session_name){
//console.log($("input[name=files]").val() + $('#title').val());
files = [];
$('div#id').find('input').each(function(i,inp){
files.push($(inp).val());
});
console.log(files);
$.post("/act_autosave_formdata.php", {
session_name:session_name,
cellphone: $('#cellphone').val(),
files: files
} );
}
Let me know if it works.