I have some images used for the avatars of the users on my website, for example http://drksde.tk/images/avatar-Luxie.jpeg
I want to add a link that allow the users to download the avatar image, when someone click in the link a Save As dialog should appear.
Now the problem is that the picture is not downloaded properly, but the dialog appears, here is the link to download the avatar, and the code:
<?php
$username = $_GET['username'];
$size = $_GET['size'];
$ext = $_GET['ext'];
$border = $_GET['border'];
$basename = basename($_SERVER['REQUEST_URI']);
if(!isset($size)) { $size = 'small'; }
if(!isset($ext)) { $ext = 'jpeg'; }
if(!isset($border)) { $border = 'true'; }
$file = 'avatar-'.$size.'-'.$username.'.'.$ext;
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$basename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
#header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
?>
You can check out Example #1 in the php.net manual here:
http://php.net/manual/en/function.readfile.php
It shows how to force an image download.
You could place it into a script image.php and use it like this
download avatar image
You could use it with the $_GET['user'] parameter to serve the right user image file.
Just remember to validate the GET input.
Related
Here is my code. I wrote this to serve mp3, images and videos. Mp3 files are received and working well. Images and videos are received but corrupted. I'm new to php.
<?php
if( !empty( $_GET['type']|| $_GET['name']|| $_GET['ext'] ) ) {
// check if user is logged
if(true) {
$type = preg_replace( '#[^-\w]#', '', $_GET['type'] );
$name = $_GET['name'] ;
$file = "{$_SERVER['DOCUMENT_ROOT']}/cont/{$type}/{$name}";
echo $file;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
}
}
die( "ERROR: invalid file or you don't have permissions to download it." );
Can you help me?
There is an echo $file; before the actual response that doesn't belong there.
Besides the text itself, PHP also produces a Warning on the first call to header(). Both these arrive in the final content before the file header and makes the data received by the browser unrecognizable by most file readers.
In the website page contains many images with downloading options. If I click the download button it automatically downloaded on user system and it shows on browser downloadable page. I have PHP code like
$image = file_get_contents('http://website.com/images/logo.png');
file_put_contents('C:/Users/ASUS/Downloads/image.jpg', $image);
Above coding is working fine. But I need to provide the path name for image to save. In user side we don`t know the path.
I need the PHP code to use the browser download location and download images need to show the browser downloads.
not possible to store the image in particular user location due to security issues .you don't force user .you have to suggest him to store particular location .and also you don't know the what file system there in user system.and also downloading path can be setting up by user anywhere so your not able to get that.
$filename = '/images/'.basename($_POST['text']);
file_put_contents($filename, $content);
you have to save/download the image somewhere on your web serwer and next send the file to user using header function, for example:
$file = 'path_to_image';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
else {
echo "file not exists";
}
manual
`<?php
$filename ='http://website.com/images/logo.png';
$size = #getimagesize($filename);
$fp = #fopen($filename, "rb");
if ($size && $fp)
{
header("Content-type: {$size['mime']}");
header("Content-Length: " . filesize($filename));
header("Content-Disposition: attachment; filename=$filename");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
fpassthru($fp);
exit;
}
header("HTTP/1.0 404 Not Found");
?>`
i have a problem in my code php
i make wallpaper script
After upload image the name of image is saved in the database and image saved in the file (images/)
i want after show image in page php and click download
i want to download image in desktop
but after click in download the image saved in desktop but But be empty
see this image : http://im51.gulfup.com/riod8Y.png
my code
<?php
$getpic = $db->query('SELECT *','wallpaper',$where = array(
'id',
'=',
"".$id.""
));
$row = mysql_fetch_object($getpic);
$file = 'images/'.$row->image;
if($_POST['download']){
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
}
?>
I have created a PHP page that allows users to download a file when they click the this link:
Download File
I have also created the download page that the link directs to:
<?php
if(isset($_GET['file'])) {
$fileID = $_GET['pubid'];
$filename= ($_GET['file']);
$path = "admin/pubfiles/";
$fullPath = $path . $filename;
mysql_select_db($database_connDioceseofife, $connDioceseofife);
$sql = "SELECT file FROM publications WHERE pubID = $fileID";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if($filename == NULL) {
die('No file exists or the name is invalid!');
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=\"$filename\"");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
readfile($fullPath);
}
?>
Now my problem is, when the download popup window come up, it reads that the file is of 0 bytes. And when downloaded, it can't open. i get the message that it is not a supported file type or its been damaged or corrupted.
Please any help would be much appreciated.
You're not doing anything with the query result in $row.
Use $row['file'] to get the actual file itself.
Thank you all for assisting me. I have been able to solve the problem after further reading. I have updated the initial script i wrote. what is above is now the working script.
What i did was to include $fullpath = $path . $filename then changed the header("Content-Disposition: attachment; filename=\"$filename\""); and then the readfile function from readfile($path) to readfile($fullpath).
Thanks again #nlsbshtr and everybody else for your help.
I'm trying to save canvas as a image file ,users get to determine which image format to download (png or jpg), then force download the file without storing the file on the server.
This is what I got so far:
JS Script:
$.ajax(
{
type : "POST",
url : "../php/download_image.php",
data:
{
format: 'png',
dataURL: flattenCanvas.toDataURL('image/png')
}
});
PHP:
$data = $_POST['dataURL'];
$format = $_POST['format'];
$file = $file = md5(uniqid()) . '.'.$format;
$uri = substr($data,strpos($data,",")+1);
file_put_contents($file, base64_decode($uri));
if($format == 'png')
{
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: image/png');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
else {
echo "$file not found";
}
}
The code cannot force download and I have no idea why it is not working.
Any help greatly appreciated.
If you don't want to store the file on the server, you don't need to interact with the server in any way. Just let the user download the content of the canvas as described here.