My image isn't being saved in the folder? - php

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);

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.

how to save data after download image?

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

Downloading an image from php page

So i have this one small thing im doing, and the thing is: It generates QRcodes using the google qrcode api. Id like to download the generated qr code with the press of a button into a /img folder i have inside the folder my php file is located at, and id need it to save with a numerical number which is the button's id. (it has like 120 buttons and each one has a 0-120 id, id like to save the qrcode that belongs to the button 10 with a name like 10.png) any ideas on how to do so?
__
Little edit: as Man Manam pointed out my question is: I have the image link, i want to save it in my server. Sorry for the confusion.
As you're using Google for creating the QR code, I assume you have a code like this somewhere:
$ga = new \Google\Authenticator\GoogleAuthenticator();
$qr_link = $ga->getUrl($username, $_SERVER['HTTP_HOST'], $secret);
// then you'd display that code with <img src="$qr_link">
What you need to do is to simply download that QR code instead of showing in browser. You can use curl for this or, even simpler, just fopen/file_get_contents if allow_url_fopen is set to ON in php.ini
$image_bin = file_get_contents($qr_link);
file_put_contents($target_file_on_filesyste, $image_bin);

Read multiple image files using php readfile

I am trying to read multiple image files from a folder (.htaccess protected) and display in a HTML page using php readfile().
The problem is I can see only the first image is read and the next is not shown in the browser. The code is as below
<?php
$image1 = 'files/com_download\256\50\www\res\icon\android\icon-36-ldpi.png';
$image2 = 'files/com_download\256\50\www\res\icon\android\icon-48-mdpi.png';
$imginfo = getimagesize($image1);
header("Content-type: ".$imginfo['mime']);
readfile($image1);
$imginfo = getimagesize($image2);
header("Content-type: ".$imginfo['mime']);
readfile($image2);
?>
I could see the first image 'icon-36-ldpi.png' successfully read and displayed in the browser and the second image is not read and not displayed in the browser.
Am I missing something? Any advice please.
Sorry if I am doing stupid but the requirement is to read multiple image files and render in the browser like a grid view. I cannot use img tag because of security reasons.
You can't dump both images out at once. Why not make two images in your html so the browser makes two calls to your script. Then use a GET param to pass the filename you want to display.
---Edit---
Important Security Note
There is an attack vector which you open up when doing soething like this. Someone could easily view your source html and change the parameter to get your image script to output any file they want. They could even use "../../" to go up directories and search for well known files that exist. e.g. "../../../wp_config.php". Now the attacker has your wordpress database credentials. The correct way to prevent against this is to always validate the input parameter properly. For example, only output if the file name ends with ".jpg"

How to treat a PHP image generation script as an image

This is an odd question but I'm stuck on how I would achieve this and I am unable to find any methods of doing so.
I have a simple php script that takes variables (containing file names) from the URL, cleans then and then uses them to generate a single image from the inputted values. This works fine and outputs a new png to the webpage using:
imagepng($img);
I also have a facebook sharing script in PHP that takes a filepath as an input and then shares the image on the users feed where this statement is used to define the image variable:
$photo = './mypic.png'; // Path to the photo on the local filesystem
I don't know how I can link these two together though. I would like to use my generation script as the image to share.
Can anyone point me in the right direction of how to do this? I am not the master of PHP so go easy please.
-Tim
UPDATE
If it helps, here are the links to the two pages on my website containing the outputs. They are very ruff mind you:
The php script generating the image:
http://the8bitman.herobo.com/Download/download.php?face=a.png&color=b.png&hat=c.png
The html page with the img tag:
http://the8bitman.herobo.com/Share.html
Treat it as a simple image:
<img src="http://yourserve/yourscript.php?onlyImage=1" />
yourscript.php
if($_GET['onlyimage']) {
header('Content-type:image/png'); //or your image content type
//print only image
} else {
//print image and text too
}

Categories