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.
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 have a index.html file consist of certain data with refresh button.
On pressing refresh button it will call refresh.php.
Refresh.php connects database and gets new updated data from database (Say- today's event data) And shows updated data in that refresh.php page
this is what I do.. But I want dynamic home page and want to remove refresh button. In short- whenever user loads homepage..that division should display updated data from database. So should I use .index.php and use php code in index.php itself will work?
I dont want to use asp/ajax/cookie/session. Please give me idea apart from these. Thanks :)
You could check the file's age, e.g.
<?php
$ca_file = '/path/to/foo.blah';
if (is_file($ca_file)) {
// check if file is not older then 1 hour
if (time() - filemtime($ca_file) <1 * 3600) {
$ca_news = 'y';
}
}
That would check if the file is not older then 1 hour. You'll probably want something smaller. Now all you need to do is to check the value of $ca_news and do your magic.
I'm writing a component for Joomla 2.5 that generates a graph using the PHP GD Library depending on the users input.
What name should I give the image file to avoid conflict when multiple users are using the component.
In my opinion you have two way.
The first is save the image with prefix like this
scotttiger-img12321323.jpg
where scotttiger is the username and 12321323 is the user input.
Another way is to use different folder for every user
scotttiger/1231321.jpg
But if You don't have the user logged in and doesn't know the username, do a simple check with isfile (http://it1.php.net/is_file) function and set incremental name. eg:
$wrote = false;
$prog = 0;
$filename="img-{$user_input}-{$prog}.jpg";
while(isfile($filename)) {
$prog++;
$filename="img-{$user_input}-{$prog}.jpg";
}
write_file($filename);
So you have the first time img-input-1.jpg the second once img-input-2.jpg and so on.
With this solution You doesn't have duplicates or conflict.
You can improve performance with this solution:
$wrote = false;
$prog = md5(time());
$filename="img-{$user_input}-{$prog}.jpg";
while(isfile($filename)) {
$prog = md5(time());
$filename="img-{$user_input}-{$prog}.jpg";
}
write_file($filename);
you can generate random name with help of e.g. date and php hash function:
http://www.php.net/manual/en/function.hash.php
So the name will be unique.
Or, depends on used feature in your component, you can somehow add the user name to the image name. It depends of the function of the component and on my factors (if there will be only one image per user, or per some other feature, etc.)
Jan
I am trying to create a PHP trigger for when a user views certain pages on my website it will update the user table in the points section.
I understand the process would work something like this
on page view > update user > where user id is (**get username from session**) > add 5 to points row
Anyone have any idea how to set up something simple like this for giving users simple points for viewing pages?
My site is using PHP and mySQL for the database.
Use cookies or session variables to keep track of the user details like the username or ID. So making a pageview trigger would be as easy as adding a mysql query at the top of every page which would update the database table for views. Kinda the same way that forums operate.
E.g
<?php
session_start();
$db_connection = mysqli_connect('host','username','password','db');
$user_id = $_SESSION['userid']; //That is asssuming that you had gotten the user id on login
mysqli_query($db_connection, 'UPDATE page_views SET views_column=views_column+1 WHERE userid=$user_id');
?>
Yes, you could do something like (if you own the page the user has to visit):
<?php
$pointsForThisSite = 5;
include "points_adder.php";
?>
While Points_adder looks whether $pointsForThisSite is defined and > 0, then adds the Points to the database as you descripbed.
Is that what you are looking for?
Create a php function and call it everytime the user enter the page.
You don't need a mysql trigger because, the action is at the webpage.
function add_points($user, $page){
//If users visits too many maybe you don't want to gave him some points.
//add points
}
and invoke the function in that pages you want to score
The most unobtrusive way to do this is with an AJAX call after the page has loaded. The call should be to an include file that performs the database update operation and returns a 204 response so that the visitor's browser doesn't wait for response content.
For an Apache server;
header('HTTP/1.0 204 No Content');
header('Content-Length: 0', true);
header('Content-Type: text/html', true);
flush();
// do the table update here
So how can I pass a variable into a script that is not connected with symfony?
For example, I have variable $a, which I render in template a.html.php
How can I use this $a in some example.php script?
More close: I wanna make image uploading with TinyMCE image manager; but each user should have their own directory (which corresponds to user id). So I should pass variable user_id to config.php file of image manager.
So this directory depends on the user's session! How can I make different dirs to upload images for each user? Or can you advise me how to deal with this problem?
Maybe to use another text editor? Which one can I connect with symfony or to use different directories for different users?
You can store information in the session if you want to share it with other scripts. Make a var_dump($_SESSION) in example.php to see what you already have.
$formPath = 'nameBundle:name:name.html.twig';
$session = $controler->getRequest()->getSession();
$session->set('formPath', $formPath);
$session->set('neededVariables', $neededVariables);
return $controler->redirect($controler->generateUrl('show_result'));
I used this to handle data and read data function
public function showResultAction() {
$session = $this->getRequest()->getSession();
$formPath = $session->get('formPath');
$neededVariables = $session->get('neededVariables');
if ($formPath or $neededVariables)
return $this->render($formPath,$neededVariables); else
throw $this->createNotFoundException('The product does not exist');
}