PHP Save an Image to a Server? - php

I'm having a stump with some PHP...
I have a Flash Application that sends an image (using as3corelib) to a PHP script that previews it in the browser, which works! But, I would actually like it to permanently save it the a server folder (uploads, etc.) instead of temporarily saving it. I can't find the right variable in the PHP that actually sends the image to a server so it could save it.
<?php
switch ($_POST["format"]) {
case 'jpg':
header('Content-Type: image/jpeg');
break;
case 'png':
header('Content-Type: image/png');
break;
}
if ($_POST['action'] == 'prompt') {
header("Content-Disposition: attachment; filename=" . $_POST['fileName']);
}
echo base64_decode($_POST["image"]);
?>
Here's an example of it: http://shmoggo.com/snapshot
JPEG, Open to Browser (but I would like it to SAVE to browser)
Any PHP guru help would be terrific, thanks a lot!
Aaron

If you have the filename, you can simply do
$newpath = "/folders/image.jpg";
$data = file_get_contents($_POST['fileName']);
file_put_contents($newpath, $data);

Rather then displaying it, save $_POST['image'] to the server, see File System

Related

Android: Images not downloading from server

I have uploaded few images on my server which will be downloaded to my android application by passing a url. I have written a php file which displays the image. I pass the URL to my android application like this :
'http://myURL/getImage.php?image=logo.png'.
When I copy paste the URL directly in browser the image is displayed correctly.
However the image file is not getting downloaded on my android application. I know that android code is correct because when I am giving any other random image URL the image is downloading correctly. Do I have to give something else in my php file to make the image 'downloadable'.
Image.php
<?php
echo '<img src="Images/'.$_REQUEST['image'].'"/><br />';
?>
It seems you want the PHP to output the image directly. Instead, your code is generating an HTML with the image on it. Although your browser displays similar results, the underlying content is different.
What you actually need could be this:
<?php
$filepath = 'Images/'.$_REQUEST['image'];
$filename = basename($filepath);
$ctype = 'image/jpeg'; // assuming it is a .jpg file
if (is_file($filepath)) {
header('Content-Type: '.$ctype);
header('Content-Length: ' . filesize($filepath));
header('Content-Disposition: attachment; filename="'.$fileName.'"');
echo file_get_contents($file);
exit();
} else {
header("HTTP/1.0 404 Not Found");
// you may add some other message here
exit();
}
This is vulnerable to hazardous $_REQUEST['image'] input. Just filter it somehow. Also you must generate the correct $ctype for the image file.

PHP display image without HTML

How may I display an image without using any kind of HTML.
Let's say I have some protected images, which may only be shown to some users.
The protected image is in directory images/protected/NoOneCanViewMeDirectly.png
Then we have a PHP file in the directory images/ShowImage.php, which check if the user is permitted to view the image.
How may I display the image using PHP only.
If you're not sure what I mean this is how I want to display the image. http://gyazo.com/077e14bc824f5c8764dbb061a7ead058
The solution is not echo '<img src="images/protected/NoOneCanViewMeDirectly.png">';
You could just header out the image with the following syntax
header("Content-type: image/png");
echo file_get_contents(__DIR__."/images/protected/NoOneCanViewMeDirectly.png");
if (<here your logic to show file or not>)
$path = $_SERVER["DOCUMENT_ROOT"].<path_to_your_file>; // to get it correctly
$ext = explode(".", $path);
header("Content-Type: image/".array_pop($ext)); // correct MIME-type
header('Content-Length: ' . filesize($path)); // content length for most software
readfile($path); // send it
}
Note: 1. image/jpg is not correct MIME-type, use image/jpeg instead (additional check needed), 2. readlfile() is better than echo file_get_contents() for binary data, 3. always try to provide content length for browsers and other software
You can use imagepng to output png image to browser:
$im = imagecreatefrompng("images/protected/NoOneCanViewMeDirectly.png");
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);

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

How can I force a file download using php?

I am new to programming, just learning it at school, but i want my users download some excel files from my homepage. The following code isn't working, it only displays it in the browser and not forcing a download dialogue.
How can i solve this problem?
The link should be: http://myurl.com/download.php?fileid=1 or http://myurl.com/download.php?fileid=2 and so on.
<?php
switch ($_GET["fileid"]) {
case 0:
$file = "files/mon.xls";
break;
case 1:
$file = "files/uru2.xls";
break;
case 2:
$file = "files/oppo23.xls";
break;
}
readfile($file);
Thanks for your help!
You have to use header. Like you can read on the php manual, its for your file:
<?php
header('Content-type: application/xls');
header('Content-Disposition: attachment; filename="downloaded.xls"');
readfile($file);
There are a few ways to do this but the most simple way I have found is to just use header. You could do something like this.
header('Location: files/oppo23.xls');

PHP Image Serving, extraneous bytes before marker

I'm desperately trying to solve this one. I have a bunch of files stored outside of the webroot and I need to serve them to a user after a few auth checks. These files have been uploaded using a Flex application or have just been manually uploaded through FTP. I have a serving script that looks something like:
<?php
$filePath = '/for/demonstration/only.jpg';
...
$type = exif_imagetype($filePath);
$size = filesize($filePath);
if ($type && $size > 0) {
switch($type)
{
case IMAGETYPE_PNG:
header("Content-Type: image/png");
break;
case IMAGETYPE_JPEG:
header("Content-Type: image/jpeg");
break;
default:
header("Content-Type: text/plain");
break;
}
header("Content-Length: {$size}");
readfile($filePath);
exit;
} else {
echo 'error';
}
Pretty simple. The image however, somewhere in the upload process, Because of the encoding process, the file has gained an extra 100-130B, and now seems to be corrupted. I get the extraneous bytes error. The upload script is pretty simple as well, Flex uses FileRefrence for the user to select the file, then encodes the data and sends it to the server script:
<?php
function fileupload($data)
{
$daily_folder = 'today/';
$fileName_clipped = substr( $fileName, 0, $max_file_len );
$fileName_clipped = preg_replace('/\./','_',$fileName_clipped);
$filePath = '/path/to/storage' . $daily_folder;
if(!is_dir($filePath))
mkdir($filePath);
if( strlen($data->filedata) > 0 ) {
if( !file_put_contents($filePath . $fileName_clipped, base64_decode($data->filedata)) )
return false;
} else {
return false;
}
}
Running the process
file A: 31,740B in, 31,848B out, 108B extra
file B: 35,273B in, 31,403B out, 130B extra
I imagine this could be on the Flash side, but honestly it's dirt simple. I just don't see where the extra data is coming in, and why its corrupting the file. Anyone know why this is happening? or better yet, how I can clean these files up now?
Here's the dealio. When I backed up all the images from one server and moved them to another I FTPed all the files onto my Windows laptop. That process, whether because of Windows or perhaps FileZilla, corrupted all of the files. adding a bunch of junk onto them. I'd live input as to what you think caused the problem, but regardless I located the problem and fixed it.
The solution was to zip the parent directory on the server and download the zip. I didn't have to modify any code. Just a simple procedure revision. How lame.

Categories