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.
Related
So, i want to make if user click the delete button the picture profile will be user.jpg (this is default picture).
enter image description here
here is my controller :
public function update()
{
$fileimg = $this->request->getFile('img');
$oldimg= $this->request->getVar('oldimage');
$defaultimg= $this->request->getVar('defaultimage');
if ($fileimg == 'user.jpg') {
$uploadImg = $defaultimg;
}
if ($fileimg->getError() == 4) {
$uploadImg= $oldimg;
} else {
$uploadImg = $fileimg->getRandomName();
$fileimg->move('img/', $uploadImg);
if ($oldimg != 'user.jpg') {
unlink('img/' . $oldimg);
}
}
When i click delete button image the image will be user.jpg and then i click button upload but the image wont change to user.jpg, but always showing the old picture.
sorry for bad english.
How about think like this:
Default picture is not a data which user is willingly save to database. It's rather a placeholder. Thinking further, it can be handled in the views. If user profile picture is not set or exists, show default picture. To prevent typing manually you can make it a global data or a config value or a constant. But the key is it is a placeholder, not a data.
Happy coding...
Write the image name in a database somewhere, usually in the user profile table. It will make your life much easier, and you'll be able to access it all the time. Then in your model, you'll be able to see if there is no image and return the default image.
I am not sure what your getRandomName() is doing, but some hash function (sha1, md5...) are good enough for creating a file name to avoid possible name collisions.
I use Laravel 4.
I have a download button in my view.
I have a lot of users that have the access to see/click to download from that button.
Here is my question,
is there any way to keep track of how many time my file has been downloaded and by whom ?
Let’s say,
user A download 27 times
user B download 5 times
user C never download at all
user D download 200 time
so on and so forth …
My Controller Function
public function file_download($id)
{
$catalog_download = CatalogDownload::findOrFail($id);
$distributor = Auth::user()->distributor()->first();
$export_type = $distributor->export_type()->first();
$product_export = $catalog_download->product_exports()->first();
$destinationPath = base_path().'/app/files/product_export/'. $catalog_download->id.'/'. $export_type->id.'/';
$file_name = $product_export->file_path;
$pathToFile = $destinationPath .$file_name;
return Response::download($pathToFile);
}
In your database
You should create another attribute. Let’s called it download_count
integer
Set default to 0
In your controller function, at the end, just do.
$user->download_count = $user->download_count +1;
$user->save();
Every-time,the user trying to download sth, they will need to go through that function - right ?
Just increment(+1) it, every-time, they go through it. I hope I am clear enough. Good Luck :)
Just add an ajax call with the username and the downloadfile's name as the Ajax call 's data to the button and safe every click on the button to the DB. Then you can simply query for the file and user combination and you will get the number of downloads.
Other way would be to add the save function to your controller and save above mentioned combination through the controller
The only option I can think of is to store it in the database.
Once the user clicks the download button, add +1 in the database.
im working on a script that shows Minecraft faces based on URL, but i got a problem, everytime an invalid username is inputed, it saves it into the skins folder, a thing it should only do when when a username IS valid.
full script:
Sorry, the problem has been already solved, i'm not going to give my script away.
It's quite long i know, but it's the full script.
So i remind, the problem is that the script saves invalid usernames too, a thing it should only do if a username is vaild.
Try to add exit of wrap all other code in else{ } statement
if(!$src){
$src = imagecreatefrompng("http://s3.amazonaws.com/MinecraftSkins/char.png");
//here's where my problem, i am trying to only display the image on screen if not found, but for some reason it saves it into the skins folder
imagepng($final);
**exit();**
}
You can use a pre-existing api made by minotar; To get my skin "Phyore" You use https://minotar.net/avatar/Phyore when Minotar doesn't recognise the username (Doesn't exist) it'll give you the steve head.
$user = 'Phyore';
//Size defaults to 180 x 180
$size = '100';
echo '<img src="https://minotar.net/avatar/'.$user.'/'.$size.'"';
See more at https://minotar.net/ They even do full skins.
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
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 :-).