how to save data after download image? - php

I am facing one problem i want to save data after downloading an image. My image has been successfully downloaded after that i want to maintain the history of downloads.Here is my Function:-
enter code here
public function UserImageDownlaod($userid=null,$imageid=null){
$imagedata = DB::table('alt_images')->where('id',$imageid)->first();
$imagedata = json_decode(json_encode($imagedata),true);
$destination = 'images/ContributorImages'.'/';
$pathToFile = $destination.$imagedata['img'];
return response()->download($pathToFile);
//Now i want to save the history of downloaded images
$history= new DownloadHistory;
$history->user_id = Auth::user()->id;
$history->alt_image_id = $imageid;
$history->save();
}
Now you can see after this return response()->download($pathToFile) line i want to save the history data. I want when User click on OK button then data will saved on history table. Can anyone help me.

The popup you showed us is from the browser. At the point, you see this popup, the browser might already started to download the file in the background. As this is part of your browser and you dont have any callback about pressing ok/cancel, there is no direct way to determine, what the user did.
A simple workaround would be, to show the user an alert box in javascript and ask, if he really wants to download the file.
Download

Related

Downloading a file from laravel storage using saved url in mysql

This is my code to save a file in laravel storage folder and the file url in mysql database.
$summary_attachment = $request->file('claimSummary');
$new_summary_attachment = time().$summary_attachment->getClientOriginalName();
$summary_attachment_path = $request->file('claimSummary')->storeAs('uploads', $new_summary_attachment);
$attachment_path = Storage::url($summary_attachment_path);
$individual_application->summary_attachment = $attachment_path;
$individual_application->save();
Anf this is my view file where I am retrieving the url of the file and giving the url as a value to href in anchor tag.
Claim Summary
But nothing happens when I click the link. When I hover over the link, the url is displayed in the bottom. But it doesn't show any errors. Just nothing happens when I click the link. When I right click, use copy url and paste it in new tab, the file is downloading. Just nothing happens when I click the link. Could somebody please mention what am I missing here. Please help.

My image isn't being saved in the folder?

I have an add form, where users can add an image, once they click submit I want the image to be saved into a certain folder located called Event_images (which I use to display the image on another page where I display the image along with other details they inputted. But once I submit the form, the image goes to the MYSQL database, but isn't in the folder where I direct it to. My code and a screenshot are provided. I want the image to be displayed in the Event_images folder I have created. Thank you in advance.
<?php
$event_img = $_FILES['event_img']['name'];
$tempimage = $_FILES['event_img']['tempname'];
move_uploaded_file($tempimage,"Event_images/$event_img");
?>
The screenshot I have provided is what happens when I try to display the information (because nothing is in the folder which I direct to)
['tempname'] isn't an index of $_FILES. See here.
Use $tempimage = $_FILES['event_img']['tmp_name']; instead. I tested locally and it worked fine. If you're using PHP 7 you may need to use { } around $event_img when you move the file so it'll process that variable.
Does this work?
$event_img = rand(1000,100000)."-".$_FILES['event_img']['name'];
$tempimage = $_FILES['event_img']['temp_name'];
$folder="Event_images/";
move_uploaded_file($file_loc,$folder.$event_img);

file_get_contents give random older images from Facebook image profile

I have simple php script, for get image from Facebook and download user profile image to my server.
$url = 'https://graph.facebook.com/'.$idUser.'/picture?width=200&height=200&redirect=false';
$data = json_decode(file_get_contents($url), true);
$url = $data["data"]["url"];
$path = $idUser.'.jpg';
file_put_contents ($path, file_get_contents($url));
In my CMS site I try to open user profile page, where all time I execute that script, because I want to see actual user profile image. But when user change user profile image my script is do wrong. file_get_contents download old image, when I refresh page or new image randomly. What that mean? Where is error?
That still my answer file_get_contents no caching?
but not work for me(
> UPDATE:
Facebook wrong get me json link randomly
I ran your code locally and got the same problem. Something is getting cached along the line, so I added a cache buster to end of the URL, and it forces the new image.
$url = 'https://graph.facebook.com/'.$idUser.'/picture?width=200&height=200&redirect=false&' . time();

how to block uploading same image after refresh the page?

im using verot.net's upload class and i almost finished my image upload project.
i just couldnt manage to do this uploading same image over and over thing.
here is a little code.
if ($upload -> uploaded){
$rand = uniqid(true);
$upload -> file_new_name_body = $rand;
$upload -> Process("upload");
if ($upload -> processed){
i have to rename it with random but if i do that ,everytime my when my upload.php refreshed,it renames it randomly and uploads to the server.
how can i block this ?
Pages that are loaded via POST will cause the browser to ask the user to resubmit the information to view the page resulting in the actions performed by that page happening again. If the pages is requested via GET and has variables in the querystring the same thing happens but silently (without the user being prompted to d it again).
The best to work around this is to use the POST/REDIRECT/GET pattern.

Actionscript - AS 2.0 - PHP copy file from server to server THEN run function

Basic Idea: I have a flash file that takes screenshots with a click of a button, sending the data to a PHP file, and then the user gets to save a PNG image. The images that are merged together (via PHP) require that they reside on the same server as the PHP, otherwise they do not merge and the final PNG shows up blank.
My solution so far: I have two PHP files, and I just need to find a way to merge them. The screenshot one, and one that copies a file from one server to another. This is my cheat work around to bring the image to reside on the same server, THEN run the screenshot php.
The Server-to-Server PHP Code:
<?PHP
$inputfile = FOPEN("https://www.google.com/intl/en_com/images/srpr/logo3w.png", "r");
$outputfile = FOPEN("transferedfile.gif", "w");
ECHO "File opened...";
$data = '';
WHILE (!FEOF($inputfile)) {
$data .= FREAD($inputfile, 8192);
}
ECHO "Data read...";
FWRITE($outputfile, $data);
ECHO "transfered data";
FCLOSE ($inputfile);
FCLOSE ($outputfile);
ECHO "Done.";
?>
So as you can see, it pulls Google's logo and saves it as "transferedfile.gif" to the directory the PHP resides on. I can get this PHP code to work by saving this as whateverIWant.php on my webserver, and visiting it directly, but I need to in place of Google's logo (in this example) put a value that will be dynamically changing via flash.
So basically… in the flash file, I'll have a dyniamic variable where the URL will change, in short. So we'll just say that I define that variable in flash as var imageToGet so somehow I need to pass that variable into this PHP. That's one step... here's the AS 2.0 code:
My Actionscript (2.0) Code:
button.onRelease = function ():Void {
sendImageToServer();
ScreenShot.save(_root, "screenshot.png", 0, 0, 100, 140);
};
the sendImageToServer() function isn't made yet. This is where I'm stuck. I would need the sendImageToServer() function to send var imageToGet as what image to get, THEN run the ScreenShot.save() function after the transfer is done (aka FCLOSE ($outputfile); is complete)
In Summary: A movie clip on the stage will have a dynamic image loaded into it, that once a button is pressed, it would need to copy that dynamic image to the local server, and then run the screenShot function. I believe once I have this figured out, I should be able to do everything else, such as saving as a unique name, saving multiple files, etc. But I just need pushed in the right direction :)
Thanks so much everyone # StackOverflow. You've been nothing but awesome to me thus far!
EDIT -- I've found a good starting point!!
I found a good starting point, and am answering my own question in case someone else stumbles upon this. I used these two codes as a starting point, and I think I'm on the right track…
In Flash: I simply made a dynamic textbox with the instance name of traceText
In Actionscript (2.0):
var send:LoadVars = new LoadVars;
var receive:LoadVars = new LoadVars;
send.toPHP = "asd123";
receive.onLoad = function(){
encrypted = this.toFlash;
traceText.text = encrypted;
}
send.sendAndLoad("test.php",receive,"POST");
In "test.php" file:
$fromFlash = $_POST['toPHP'];
$encrypted = $fromFlash;
$toFlash = "&toFlash=";
$toFlash .= $encrypted;
echo $toFlash;
What this ended up doing was sending the variable to PHP and then back again. Which is perfect for what I needed. For now, I should be good! Hope this helps anyone that needs it.

Categories