I basically have an edit profile page that along with other fields has a option to upload a picture. Now the problem I have is determining wether or not they submitted a file or left it blank(default).
My current code:
if($this->input->post('user_pic') === FALSE)
{
//Don't do anything.
} else {
//Proccess upload.
}
The form is done correctly with multi-part and all, and other values do update, so I am assuming that this exact code that isn't working.
The problem is that even if I do submit a file it remains just like it was, or another words doesn't do anything. Am I doing this wrong?
you can check so: if(isset($_FILES['user_pic']))
CodeIgniter method:
$this->load->library('upload');
if ($this->upload->do_upload('user_pic'))
//good
else
//errors display $this->upload->display_errors()
CodeIgniter File Uploading Class
Related
I have multiple files and input text in my form. My code is :
if(formValidationIsGood) {
if(customeCheckFileErrorIsEmpty) {
//StartProcess
}
else {
//I load my view with my array which contains the different error related to the files.
}
else {
//I load my view with the form error (set up by : $this->form_validation->set_rules('variable', 'Variable', 'required');) But if there is an error for the files I cant display it.
}
With this code I can't show the form error and the files error at the same time.
For example the user, is uploading an csv file instead of a pdf file, and he forgot to write into a text input, the form error will be displayed but not the file error and vice versa.
Obviously I am using the helper file provide by code igniter.
Since you want to display error messages for either A OR B, you can try this:
if (!formValidationIsGood || !customeCheckFileErrorIsEmpty)
{
// load the view and display whatever errors you found
}
else
{
// form validation IS good and the files error is empty
}
The if clause above will evaluate to true if either formValidationIsGood ISN'T true (the ! prefix is key) OR the customeCheckFileErrorIsEmpty ISN't true (which would mean there is an error)
I'm trying to understand uploading files in laravel for a project I'm working on.
Sadly I failed when trying to achieve what I had in mind.
When a form is submitted you get to a site where all your inputs are displayed so you can check them again. So the form data has already been validated to this point.
On this site is also an input button to upload a file.
This means that both will go through the same controller method to be displayed after the validation is successful.
Problem here is that, even though the validation of the form works perfectly, I don't know how to validate the file.
Here you can see the store-method:
public function store(ValidateFormData $request, ValidateUploadedFile $requestFile, $param = '')
{
$validated = $request->validated();
$formData = $request->all();
if ($requestFile->hasFile('file')) {
$file = $requestFile->file('file');
// call to $requestFile/validate the uploaded file
}
return view('/validation', compact('formData'));
}
EDIT
I just need the file to be a pdf/postscript file and successfully uploaded.
This is the rule I have in ValidateUploadedFile:
public function rules()
{
return [
'datei' => 'required|file|mimes:pdf,ps',
];
}
I really hope someone can help me.
Please do not link any of the Laravel documentation, believe me, I've seen way to many sites about this topic but still don't quite get it.
If I upload a file with YII and another rule fails, then the user has to pick the file again. What is the easiest way to avoid this?
For example, I have a rule that the title must be 20 characters at most. The user types in 21 letters. He chooses a file to upload. When the user is returned to the page, the file is no longer there and he must choose it again, and effectively upload it again. This is very frustrating, especially now when the user will be required to upload up to ten files.
I know Drupal works like this. If you upload and other rules fail, the files appear as screenshots when you return to the form. How can I get the same functionality on YII?
UPDATE
If I could get that requirement covered with this extension and not require that the user presses start upload, I would be home free
The original plugin that xupload wraps, there is an additional callback option you could use: .done().
In the xupload wiki, the way to access those additional options would be this way:
<?php
$this->widget('xupload.XUpload', array(
// ... other attributes
'options' => array(
//This is the submit callback that will gather
//the additional data corresponding to the current file
'submit' => "js:function (e, data) {
var inputs = data.context.find(':input');
data.formData = inputs.serializeArray();
return true;
}"
),
));
?>
Source
You could probably just change the submit part to done, and let that save the URL/path of the uploaded file to a temporary hidden field, and move your validation to that hidden field instead, so the user does not have to re-upload a file again.
I moved away from this plugin to the coco uploader as it was easier to implement.
You could enable client side validation and AJAX validation. So your regular attributes will be validated before the form is sent and the file gets uploaded.
You can do it with session.
In your controller
// Here I have taken Users as model. you should replace it as your need.
$model=new Users;
if(isset($_POST['Users']))
{
$model->attributes=$_POST['Users'];
//save file in session if User has actually selected a file and there weren't any errors.
if(isset($_FILES['Users']) && $_FILES['Users']['error']['image'] == 0){
Yii::app()->session['image'] = $_FILES['Users'];
}
if(isset(Yii::app()->session['image']) && !empty(Yii::app()->session['image'])){
$model->image = Yii::app()->session['image'];
$model->image = CUploadedFile::getInstance($model,'image');
}
if($model->save())
{
if(!empty($model->image)){
$model->image->saveAs(Yii::app()->basePath.'/images/'.time()."_".$model->image->name);
unset(Yii::app()->session['image']);
//File has successfully been uploaded.
}
// redirect to other page.
}
}
else{
// remember to unset the session variable if it's a get request.
unset(Yii::app()->session['image']);
}
And in your view file
//Your form fields
//This is to show user that he has already selected a file. You could do it in more sofisticated way.
if(isset(Yii::app()->session['image']) && !empty(Yii::app()->session['image'])) {
echo "<label>".Yii::app()->session['image']['name']['image']."</label><br>";
}
//File uplaod field.
//More form Fields.
Hope that helps.
I have a page with a form where user can upload an image to replace his existing avatar.
However, if the user submits forms without uploading an image, I will not update his avatar and user can keep his old avatar.
This is an UPDATE issue so I need something like this in pseudo code:
if (Input::has_uploaded_file() === true)
//User uploaded something, update avatar column/remove old avatar etc.
else
//User didn't upload anything so don't update avatar column
I just need the first line, though.
Can anybody help? I couldn't find much about this in documentation.
If Input::has_file('name') does not work for you then you can use the equivalent of what it is doing, like this...
if (Input::file('name.tmp_name', '') != '')
Taken from laravel/input.php
I read laravel source code, i think its this:
Input::has_file('input_name');
If that's not work, try using native php:
$_FILES['input_name']['error'] = UPLOAD_ERR_NO_FILE;
Or same above with laravel:
$i = Input::file('input_name');
$i['error'] = UPLOAD_ERR_NO_FILE;
Code above is to check if user leave the upload form empty.
I have a form where a user can edit their public profile and upload a picture in the process. Everything works fine except for when a user has a photo uploaded or just leaves it blank to not change it it still goes ahead and tries to upload it and throws an error if it's empty... I do have a check in place, however it just ignores it.
if(isset($_FILES['userfile']))
{
$file_name = $this->session->userdata('user_id');
$img = $this->custom_lib->img_upload('./upload/user/', $file_name);
$user_pic = 'upload/user/'.$file_name.$img['file_ext'];
} else {
$user_pic = 'upload/default.png';
}
The problem I think is within the if statement however I don't see what it can possibly be. If I do select an image it uploads it just fine and everything works well. If I leave the image field blank (not select a file) instead of doing this:
$user_pic = 'upload/default.png';
it tries to upload a file... which doesn't exist... and then it throws an error. The img_upload() function is inside a custom library I made, however that function shouldn't even be called if no file was submitted.
I appreciate any help!
isset will return true even if the item you are checking contains nothing, is null, or is false.
To avoid this issue, you use empty like so:
if(!empty($_FILES['userfile']))
This will only return true if the $_FILES['userfile'] contains a value that is not null, false or empty.
Hope that helps :-).