multiple Image download after selecting - php

This is pushan once again .I have a applied a code to download multiple pictures after selecting them through check boxes. the value property of the check box contains the full path of the image .The image file is not downloading here is the code snippet:
if(isset($_POST['picdnld'])) {
$picarry=$_POST['supplier_picture'];
foreach($picarry as $pic) {
$handle = fopen($pic, "r");
$filename1 = basename($pic);
$xt=pathinfo($pic, PATHINFO_EXTENSION);
$filename=$filename1;
echo $filename.'</br>';
$outhandle=fopen('image'."/".$filename,"w");
if($outhandle){
echo 'directory found'.'</br>';
} else {
echo "directory not found".'</br>';
}
while (!feof($handle)) {
$buffer=fread($handle,4096);
fputs($outhandle,$buffer);
}
}
fclose($handle);
fclose($outhandle);
}
supplier picture is the name of the checkbox whose post contains the image links.I found that $outhandle is returning false every time. please help me to down load multiple selected images.

This is not possible the way you show. A response can generally contain only one image resource.
The most common way of doing this is to put all images into a ZIP file, and offer that for download. You could use the ZipArchive class for that. The linked page contains a small example.

Just read the image file and before sending it to browser
set the content header before sending to browser
header ("Content-Type: image/jpeg").

Related

PHP file that accepts file path from loop and returns an image

I am trying to display multiple images from my database through a loop. Basically it's something like this
while loop is running{
$_SESSION['path'] = $imageURL;
echo '<img src="pic.php">';
}
my idea is the pic php gets the path then displays it. Then another path comes in and it returns the image again.
HOWEVER IT DID NOT. It just returns the last image retrieved and repeat it until the loop is done.
so instead of displaying a.png, b.png and c.png. What displayed were 3 c.png.
here is my pic.php
<?php
session_start();
$name = $_SESSION['path'];
$fp = fopen($name, 'rb');
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
fpassthru($fp);
exit;
?>
I'm particularly new to this stuff so it will be a great help if you guys check this out! TYIA!
Here is what happens:
You access your web page through your favorite browser
The server process your request, and asks PHP to create an HTML file containing 3 <img src="pic.php" />
The server returns the HTML file to your browser
The browser analyze your HTML file and detect it has links to external resources, indeed it has 3 times pic.php, so it begins to ask the server to return the content of this file
The server process again the request and asks PHP to return to him the content of pic.php
The server returns this content, and HTML put it 3 times
You can see at the end, even if you looped 3 times and changed 3 times $_SESSION['path'], the server comes after the war and only see c.php, so it returns it to the browser.
You should adopt another strategy to fetch your images.
Workaround
One way to fix the issue is to fetch image "at demand" like this:
pic.php
<?php
session_start();
$name = filter_var($_GET['q'], FILTER_SANITIZE_URL);
$fp = fopen($name, 'rb');
header("Content-Type: image/png");
header("Content-Length: " . filesize($name));
fpassthru($fp);
exit;
?>
index.php (the file you access)
<?php
while loop is running{
echo "<img src='pic.php?q=$imageURL'>";
}
?>

Using input[type="file"] to prompt the user to save the file

I am trying to achieve two actions from an input file tag. I have the following input:
<input id='file-input' name='attach' type='file' style='margin-left:15px;'/>
This is found in messages.php. What I am trying to achieve are two things:
If the file uploaded is over 1mb in size (or is a file which is not an image), then produce a button which on click opens the save as menu, from where a user can select where they wish to download the data.
And secondly, as mentioned, if the file size is lower than 1mb, then simply display the data on the page (only works for image files).
I have other pages where I have used input type="file" to upload profile images, and have just displayed the image on the page. But I am unsure on how I can execute (1) - how I can open a menu from where the user can save the data?
Just serve the file with a Content-Disposition: attachment header, see PHP Outputting File Attachments with Headers
Among other nice answers I will do it this way.
I have made it possible by clicking on your suggested Download button, if file is greater than 1Mb you will get download pop up like this
Otherwise if file less than 1Mb it will just show it on browser will look like this:
Btw, the code is self explaining.
PHP part called index.php
<?php
$filename = "test3.jpg";
$maxSize = 1000000; // 1Mb
if (isset($_POST['save']))
fileHandler($filename, $maxSize);
function fileHandler($filename, $maxSize)
{
$fileinfo = getimagesize($filename);
$filesize = filesize($filename);
$fp = fopen($filename, "rb");
if ($fileinfo && $fp)
{
header("Content-Type: {$fileinfo['mime']}");
if ($filesize > $maxSize)
{
header('Content-Disposition: attachment; filename="NewName.jpg"');
}
fpassthru($fp);
exit;
} else
{
echo "Error! please contact administrator";
}
}
?>
HTML part inside index.php but it is important this code should comes after php tags and not before.
<form action="index.php" method="post">
<button type="submit" style="border: 0; background: transparent" name="save">
<img src="download.jpg" alt="submit" />
</button>
</form>
Note: it is important your php document start directly with <?php ..., please read
You won't need to fake a click or something. You probably need something like this.
User selects file and clicks the "Upload file" button.
File gets uploaded using e.g. PHP.
PHP displays the contents of the file, using the correct headers.
PHP determines if the file is smaller or larger than 1MB.
If the file is larger, set a header that forces the user to download the file (causing a select location popup).
If the file is smaller, do not set the header, causing the file to display in the browser.
Where Italic is a user action and Bold is a server action.
You can do this by getting the image mime type and setting the content disposition and content type headers:
$file = 'path/to/file';
if (file_exists($file)) {
$contents = file_get_contents($file);
$fileSize = filesize($file);
$image_info = getImageSize($file);
$mimeType = $image_info['mime'];
header("content-disposition: attachment; filename=" . basename($file));
header("content-type:$mimeType");
header("Content-length: $fileSize");
echo $contents;
exit;
}

PHP mySQL turning a string into a png is only working for the first image

So I am trying to get a bunch of photos to appear that are saved in a mysql database. With the code I am using, the page displays just the very first photo in the database with nothing else. There are 5 different photos and I don't know where they are going. If someone could help that would be great. My code is here:
while($imageRow = mysql_fetch_array($imageResults)){
$data = $imageRow['image_data'];
$data = base64_decode($data);
$im = imagecreatefromstring($data);
if ($im !== false) {
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
}
}
The headers will only display one image per page.
You could try:
while($imageRow = mysql_fetch_array($imageResults)){
$data = $imageRow['image_data'];
print '<img src="data:image/png;base64,'.$data.'" />';
}
That's untested, but something like that should work.
A better way would be to store the image file on the disk and store the location in the database.
You could either insert them into mysql as in blob type or simply upload the file to a folder and insert the file name into the database (I assume you just dont create random folders for each instance therefore you would know the destination path already)

File uploading only working with some images (class.upload.php)

I've tried the native way of uploading images with PHP, but my friend suggested me to the class.upload.php library, which still had the same results as before. I can only upload certain images without them having that little icon that means that the browser can't find the image, but what's weird is that when I download the "invalid" images to my computer, they're fine. About 50% of images actually do work, but the rest just show the appropriate size that they should have but no image. Here is my code (my html form has a file input type called filename:
$handle = new upload($_FILES['filename']);
if ($handle->uploaded)
{
if ($file_src_size < 20000)
{
$handle->file_new_name_body = "test";
$handle->image_convert = 'jpg';
$handle->process('/');
echo "<img src = \"test.jpg\" />";
}
else echo "Files must be 20 kb or under";
}
else echo "Upload failed, please try again";
The fact that you can download the images and re-display locally them suggests that the problem is not with the upload script. An alternative problem could be that the images are not properly saved for web viewing. For example, just because a file ends in .jpg and can be viewed locally does not mean that it is recognizable by a browser. A CMYK jpeg cannot be viewed by many browsers.
You need to debug your code as something is going wrong, and only someone with access to your computer can tell you why.
1) The library you're using has error detection code
if ($handle->processed) {
echo 'image resized';
$handle->clean();
} else {
echo 'error : ' . $handle->error;
}
2) You should be able to look at the processed image data, and figure out why the browser is not able to parse it as an image. If you can give a URL for an invalid image I would be able to tell you what's wrong with it.
3) Writing files to the root directory by calling "process('/');" is bad. You should make a directory to hold files that come from other people, to avoid having potential problems when some uploads an image called 'index.php'.

Ajax image grabber

I need an image grabber. By that I mean a Digg like image grabber that can search other pages (including youtube, normal websites, economist,...whatever), get the images that are of decent sizes and if I select it, I can upload it to my server.
Does anyone know of a plugin for this?
Thanks.
I don't know about any off-the-shelf library. But I once needed a quick way to retrieve the "main image" off a page. My best guess was to just get the largest in file size. I was using the PHP SimpleHTMLDom library for easy access to the site's <img> tags.
Now, here's the main part of the code, that returns the URL of the biggest image file for a given page.
Hope you can build on that.
// Load the remote document
$html = file_get_html($url);
$largest_file_size=0;
$largest_file_url='';
// Go through all images of that page
foreach($html->find('img') as $element){
// Helper function to make absolute URLs from relative
$img_url=$this->InternetCombineUrl($url,$element->src);
// Try to get image file size info from header:
$header=array_change_key_case(get_headers($img_url, 1));
// Only continue if "200 OK" directly or after first redirect:
if($header[0]=='HTTP/1.1 200 OK' || #$header[1]=='HTTP/1.1 200 OK'){
if(!empty($header['content-length'])){
// If we were redirected, the second entry is the one.
// See http://us3.php.net/manual/en/function.filesize.php#84130
if(!empty($header['content-length'][1])){
$header['content-length']=$header['content-length'][1];
}
if($header['content-length']>$largest_file_size){
$largest_file_size=$header['content-length'];
$largest_file_url=$img_url;
}
}else{
// If no content-length-header is sent, we need to download the image to check the size
$tmp_filename=sha1($img_url);
$content = file_get_contents($img_url);
$handle = fopen(TMP.$tmp_filename, "w");
fwrite($handle, $content);
fclose($handle);
$filesize=filesize(TMP.$tmp_filename);
if($filesize>$largest_file_size){
$largest_file_size=$filesize;
$largest_file_url=$img_url;
unlink(TMP.$tmp_filename);
}
}
}
}
return $largest_file_url;

Categories