saving image from a webpage + saving its name too using PHP - php

how can i save an image from a web page , for example:
http://othersdomain.com/image/colorful.jpg
How can i save the image and save it to my directory "image" inside FTP.
and also get the "colorful" name from the link.
As a conclusion , it will get colorful.jpg and save in my directory with the same name and type then echo out the link to it(on my web server).
after being saved to my server , it will echo out somehting like this:
http://mydomain.com/image/colorful.jpg

You can get a file from another server using file_get_contents.
$file = file_get_contents('http://othersdomain.com/image/colorful.jpg');

Related

passing a file identifier string via $_GET method to modify an image src in the target page: solved

sending page
has a link like so,
<a href="http://someurl.org/share?id=<?php echo $ftime;?>" >link to share</a>
which produces the correct url form of the GET method by inserting the filemtime() integer that i want to show on the share page, it looks like this on the end of the url
...share?id=1588888386
on the share page
i'm using $_GET['id'] to define a variable
$id = $_GET['id'];
and then inserting that variable into the src of the image file, like so
<img src="test<?php echo $id;?>.jpg">
which correctly resolves to
test1588888386.jpg
but the image is broken and "Could not load image" according to Firefox even tho that file exists right along side the test.jpg file on the host server... and the base image loads just fine if the 'id' string is NULL.
originally tried using $_GET['id'] directly in the src without going the $id route gives the same results.
i'm at a loss, the only weird clue is FF warning me about "mixed content" with a greyed out padlock.
am i violating some taboo by doing this?
all i want to do is share the version of test.jpg that has that 10digit dynamic number in the file name (the number is extracted from the timestamp when the file is first created).
for now, i'm just manually renaming the file on server, but will eventually use php to copy the test.jpg file and rename it dynamically to add the timestamp, so the test.jpg file can be overwritten the next time it's created by user activity.

Moodle File API uploading an image

I am currently working on Moodle and i am trying to upload an image and display it.
I followed all the steps explained here : https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#Simple_use
and it is working.
For example if i upload a file with some text in it and i access the URL given by the make_pluginfile_url function it will display the text that is inside the file.
The problem is when i upload an image it doesn't display the image but some text like this :
(+�ء��\U�k���*�j~�Uܽ�U���W\Uت�k��zb��S�
I suppose it's because the File API treats the image as a text file and not as an .jpg.
Could someone tell me how i could make it display the image ?
Display it with html_writer class. Example:
html_writer::empty_tag('img', array('src'=> $url));
Where $url is valid url to your file. (Prepared with moodle_url::make_pluginfile_url() or similar)
empty_tag docs

Get pdf file name from a URL in PHP

I want to get the pdfname from a URL generating the pdffile....
here is the link
$pdflink = "http://example.com/a.php?action=getSubmissionPDF&sid=".$sid."&formID=".$formid;
$pdflink is having the sid i.e form submission_id... with this sid name of the pdf file is generated
when i click on this url the pdf is generated with the name of $sid.pdf like 34556677.pdf
how I got the pdf name from this url??
I want to save this pdf file in infusionsoft filebox..
$filename=$_GET['sid'].".pdf";// filename
$contactId=5865;//contactid
$dataEncoded = base64_encode($pdflink);
$uploadFile = $app->uploadFile($filename, $dataEncoded, $contactId);
from this code the file is opladed in filebox bt not able to open the file because it shows the file is damaged and error msg it generates
when i download the file from filebox then it displays file is damaged not able to open the file in adobe...
use this to get sid value from url $_GET['sid']

Display user input image without saving it to a folder

How can I display an image and pass it as an input parameter in an executable in php without saving the image in a folder. The user gives the image path as input and I am using ajax to display the image when it is selected when I save it to a folder it works but how can I display it without saving it in a folder? My code now is
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/".$_FILES["file"]["name"]);
//echo "Stored in "."upload/".$_FILES["file"]["name"];
echo "<img src='upload/".$_FILES["file"]["name"]."' class='preview'>";
I tried
<img src=$_FILES["file"]["tmp_name"]. class='preview'>
but it didnt work. As I will have thousands of input from thousands of user I dont want to save it. Is there any optimised and efficient method to do this?
I think, its not possible to show image without saving it. You could try to save the image in temp folder on the server side and clean this folder periodically to avoid much space consumption.
The src attribute of the <img> tag should be an URL accessible by the client.
You try to give a local path (ex: path/to/your/file.jpg) of a temporary file as URL, it will not working.
info: The uploaded image is save on the local disk on a temp directory, and could be deleted by PHP later.
If you want to show the image without moving it at a place reacheable by a URL, you can try to load its content as base64 content
$imagedata = file_get_contents($_FILES["file"]["tmp_name"]);
$base64 = base64_encode($imagedata);
and use in your HTML
<img src="data:image/png;base64, <?php echo $base64; ?>" />
I don't think you can show the image without saving it.
You need to save the file either to the filesystem or to memory if you want to later output
Your problem here is that $_FILES only exists in the script that the image was sent to. so when you initiate another http request for img source, php no longer has any clue what file your trying to read.
You need a way to tell which image to be read on http request.
One thing you can do is that you can save the file in a place accessible by the client and then just have php delete it after you output it. So once the image is outputted it will be deleted and no longer be existing in the file system.
Another approach would be to get the image from the memory by directly writing the contents to httpresponse.
You can do this way
$image = file_get_contents($_FILES["file"]["tmp_name"]);
$enocoded_data = base64_encode($image);
and when you show your image tag :
<img src="data:image/png;base64, <?php echo $enocoded_data ; ?>" />
Hope any of these helps you

How to recieve & store an image file from another server, being passed programatically with paramaters?

I am using the API of an image editing website (pixlr.com) for use by members of my site. I open Pixlr.com in an iframe where they can create an image and upon SAVE, pixlr sends the image file by use of parameters.
I want to save these image files (unique for each member) in a folder on my server (or on Amazon's S3 image server), using PHP. How do I receive their parameters ("image") of the image file and store them on my/Amazon's image server?
If the image is sent to your PHP script via POST, then you should be able to do something like this:
$handle = fopen($imageName, "wb");
fwrite($handle, $_POST["image"]);
fclose($handle);
Where $imageName is the absolute path and filename of the image where you want to save it (make sure you Apache user has write permissions to that directory). Depending on the picture's encoding you may need to figure out which extension to save it with (ie .jpg, .bmp, .png, etc).
EDIT:
Looks like they are sending the image via $_FILES. Try this:
move_uploaded_file($_FILES["image"]["tmp_name"], "/home/path/domain.com/upload/". time() .".png");

Categories