I am trying to write code to download image in WordPress, the image download correctly but image not opening after download , I have tried same code without WordPress its wok correctly , I want it to run in WordPress also
My code
<form method="post">
<input type="submit" name="submit"/>
</form>
<?php
function sendHeaders($file, $type, $name=NULL)
{
if (empty($name))
{
$name = basename($file);
}
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="'.$name.'";');
header('Content-Type: ' . $type);
header('Content-Length: ' . filesize($file));
}
if(isset($_POST['submit']))
{
$file = 'logo.png';
if (is_file($file))
{
sendHeaders($file, 'image/png', 'My picture.png');
ob_clean();
flush();
@readfile($file);
exit;
}
}
?>
image after download:
There are two ways to rename.
Using Shortcode:
[wpfid new_name="new-name-of-file"]
You can use variables also like this:
[wpfid new_name="%post_id%"]
,[wpfid new_name="%filename%_%rand%"]
, etc.Bulk Rename Images:
Goto settings >> Wp-Force Images Download page and set your desired combination to rename images, e.g.
%filename%-%rand%
.Link: https://wordpress.org/plugins/wp-force-images-download/