Save browser image into Directory - php

How to save the image from browser into Directory by PHP code..
First I pass an image into a php file via:
<http://localhost/filename.php?image=image.png>
I have code like below in filename.php:
$image = $_GET['image'];
header('Content-type: image/png');
imagepng($image);
now the image will be shown in the browser. but how I save it into directory?
I tried this
file_put_contents(DIR_IMAGE.'watermark.png', file_get_contents(the link http://.....=image.png));
the files is saving into the directory but the file is corrupted/damage. how can I do workable? Thanks

You can use fopen to open directory and write (save) the file. (fopen php manual)
$f = fopen('/home/www/path/image.jpg', 'w');
fwrite($f, $image);
fclose($f);

Related

How to save an image in upload folder from bytes string of image?

I want to save an image in particular folder. here i am using image bytes string to upload. i am using "imagecreatefromstring($bytesarray)" for this. and "imagejpeg()" which is given me a jpeg image which i passed in imagecreatefromstring() function.
here i am using file_put_contents() for uploading an image. but the image still not save in folder.
Here below is my code.
$imgdata = base64_decode($fl_data[$i]['Archivo']);
$im = imagecreatefromstring($imgdata);
header('Content-Type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
$filemainpath = base_url().'uploads/apifiles/'.$filename.".".$extension;
file_put_contents($filemainpath, $im);
give the folder permission to 777 and use absolute path instead of base_url()
$filemainpath = __dir__.'../uploads/apifiles/'.$filename.".".$extension;
You can also use DOCUMENT_ROOT:
$filemainpath = $_SERVER['DOCUMENT_ROOT'] . "/YourAppName/uploads/apifiles/".$filename.".".$extension;

Display image in web use file .php

i am a newbie android programmer, i not good at website design. However, i have a project send image from android to website, image is sent from android to website ok fine when i use this script
<?php
$base=$_REQUEST['image'];
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('uploaded_image.jpg', 'wb');
fwrite($file, $binary);
fclose($file);
echo 'Image upload complete!!, Please check your php file directory……';
?>
and my picture located here.
but now i would like to show image in web page(in my website nns12151069.esy.es), what should i write more in script .php
Please lookup the file upload with php http://www.w3schools.com/php/php_file_upload.asp
another hand you can call directly your image from uploaded path

Renaming file while downloading from other website

I am trying to rename file and download it for user!
Example... In my website I offer to download Google Logo from my website link!
http://www.mywebsite.com/files/logo-mysite.jpg
then the file is saved as logo-mysite.jpg in the users computer but the file in real is downloaded from https://www.google.co.in/images/srpr/logo4w.png
We can do it by saving the image from https://www.google.co.in/images/srpr/logo4w.png temporary to our website and then auto-delete it on download or auto-delete within 1 hour!
You can suggest a filename for a download by adding a content-disposition header:
header('Content-disposition', 'attachment;filename=logo-mysite.jpg');
die(file_get_contents('https://www.google.co.in/images/srpr/logo4w.png'));
Obviously this would give problems since you are downloading a png file with a jpg extension, but I'm just following your question... To convert the image you'd need to add some lines of GD2 code.
Use the header() function to set the name that the user will download:
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename=logo-mysite.png');
// Open the new file, and dump it out to the user
$handle = fopen( "https://www.google.co.in/images/srpr/logo4w.png" );
fpassthru( $handle );
fclose( $handle );
If you really want to convert it from jpg to png, you'll need to run it through ImageMagick or the likes.

download image from wamp server and save in my folder loacated in same using php

hello friends i want to save image from my wamp server to my specific wamp project folder i have used below code ,this code show me the dialog to save image but i want to call image from wamp server and save it in my folder
<?php
header('Content-type: image/png');
$imageUrl = "http://ip/demo/images/itemimage/Platters.png";
$filename = realpath(dirname("http://ip/demo/images/itemimage/Platters.png"))."/image1.png";
$handle = file_get_contents($imageUrl);
file_put_contents($filename, $handle);
?>
this code do not download image automatically to my folde can any one help me ?
i got ths error with above code
The problem is you do two things on your script. You set the image to deliver the image over the php script.
<?php
header('Content-type: image/png');
echo file_get_contents("http://ip/demo/images/itemimage/Platters.png");
?>
Then you should fetch the image and send the image with echo to the user.
When you want to save the image then you need only fetch the image with file_get_contents and put them to your harddrive what you do at the moment.
I think in your example the echo missing this is why you get the error. You set the content type of the site but you don't deliver any png data.
<?php
header('Content-type: image/png');
$handle = file_get_contents("http://ip/demo/images/itemimage/Platters.png");
file_put_contents(dirname(__FILE__).md5(time()), $handle);
echo $handle;
?>
Edit: Try something like this. Then he should save the image in the same path.
One way:
exec("wget http://ip/demo/images/itemimage/Platters.png /tmp/demo/images/itemimage/Platters.png");

Stream protected media (located outside of httpdocs) with jPlayer

I have uploaded some sample mp3 files to a directory outside of httpdocs, I have ensured that this is accessible to PHP by configuring open_basedir correctly and tested that this directory is working.
What I would like to do is stream these files via a PHP file as non-authenticated users should never have access to these files. I am currently using jPlayer and expect the setMedia function should look similar to this:
$("#jquery_jplayer").jPlayer("setMedia", { mp3: "stream.php?track=" + id + ".mp3" });
I have tried setting content headers etc in stream.php and it currently looks like this:
$filePath = "../song_files/mp3/";
$fileName = "$_GET[track].mp3";
header("Content-Type: audio/mpeg");
header('Content-Disposition: attachment; filename="'.$fileName.'"');
getFile($filePath + $fileName);
If I load this page directly, the mp3 file downloads and plays fine, but when I use the above javascript, jPlayer doesn't play the track.
I have had a look at this post ( Streaming an MP3 on stdout to Jplayer using PHP ) and it appears the user was trying to achieve exactly what I want, but upon testing the solution I keep running into a problem, all I get is "CURL Failed".
Are there any different methods I can use to achieve this. Pointing me in the right direction would be greatly appreciated.
After searching around some more I have found a solution that is working fine. I used the code from a similar topic ( PHP to protect PDF and DOC )
I will place the code I used here to help answer the question correctly:
//check users is loged in and valid for download if not redirect them out
// YOU NEED TO ADD CODE HERE FOR THAT CHECK
// array of support file types for download script and there mimetype
$mimeTypes = array(
'doc' => 'application/msword',
'pdf' => 'application/pdf',
);
// set the file here (best of using a $_GET[])
$file = "../documents/file.doc";
// gets the extension of the file to be loaded for searching array above
$ext = explode('.', $file);
$ext = end($ext);
// gets the file name to send to the browser to force download of file
$fileName = explode("/", $file);
$fileName = end($fileName);
// opens the file for reading and sends headers to browser
$fp = fopen($file,"r") ;
header("Content-Type: ".$mimeTypes[$ext]);
header('Content-Disposition: attachment; filename="'.$fileName.'"');
// reads file and send the raw code to browser
while (! feof($fp)) {
$buff = fread($fp,4096);
echo $buff;
}
// closes file after whe have finished reading it
fclose($fp);
</code></pre>

Categories