英文:
getting the link but images not getting downloaded
问题
I am fairly new at this, I was trying to display and download images stored in database as an image path. the display works fine but when I try to download the images from the download button the URL shows the full path of the image but the download doesn't happen.
$sql_img = "SELECT * FROM cms
JOIN images ON cms.C_ID = images.C_ID
WHERE B_Name = '$select'";
$sql_img_run = mysqli_query($con, $sql_img);
?>
<table class="display_img">
<tr>
<th>Image ID</th>
<th>Cms ID</th>
<th>Images</th>
</tr>
<?php
while ($row = mysqli_fetch_array($sql_img_run)) {
$img_id = $row['Img_ID'];
$image = $row['Image'];
$cms_id = $row['C_ID'];
$path = "http://localhost/Airan/api/";
?>
<tr>
<td><?php echo $img_id ?></td>
<td><?php echo $cms_id ?></td>
<td><?php echo '<img height="100" src=' . $path . $image . '>' ?></td>
<td>
<a href="home.php?Image=<?php echo $path . $image; ?>" class="btn-primary download-btn">Download</a>
</td>
</tr>
<?php } ?>
</table>
I have tried various methods but nothing works.
英文:
I am fairly new at this, I was trying to display and download images stored in database as a image path. the display works fine but when I try to download the images from the download button the URL shows the full path of the image but the download dosent happen.
$sql_img = "SELECT * FROM cms
JOIN images ON cms.C_ID = images.C_ID
WHERE B_Name = '$select'";
$sql_img_run = mysqli_query($con, $sql_img);
?>
<table class="display_img">
<tr>
<th>Image ID</th>
<th>Cms ID</th>
<th>Images</th>
</tr>
<?php
while ($row = mysqli_fetch_array($sql_img_run)) {
$img_id = $row['Img_ID'];
$image = $row['Image'];
$cms_id = $row['C_ID'];
// $path = "C:\xampp\htdocs\Airan\Api\";
$path = "http://localhost/Airan/api/";
?>
<tr>
<td><?php echo $img_id ?></td>
<td><?php echo $cms_id ?></td>
<td><?php echo '<img height="100" src='.$path.$image.'>'?></td>
<td>
<a href="home.php?Image=<?php echo $path.$image; ?>" class="btn-primary download-btn">Download</a>
</td>
</tr>
I have tried various methods but nothing works.
答案1
得分: 0
以下是您要翻译的内容:
"An example, using hardcoded image paths and nonsense IDs to emulate what you might be drawing from the database.
Use the GET method as you are using a hyperlink with Image
parameter in querystring.
Do not output HTML ( or any content ) before calling header
function unless you explicitly flush the buffers before.
Add the download
attribute to each hyperlink.
error_reporting( E_ALL );
ini_set( 'display_errors', 1 );
# simple function to read file and send the binary contents
function sendfile( $filename=NULL, $filepath=NULL ){
if( file_exists( $filepath ) ){
if( !is_file( $filepath ) or connection_status()!=0 ) return FALSE;
header('Content-Type: application/image');
header('Content-Length:'.filesize( $filepath ) );
header("Content-Length: ".(string)( filesize( $filepath ) ) );
header("Content-Disposition: attachement; filename={$filename}");
header("Content-Transfer-Encoding: binary\n");
if( $file = @fopen( $filepath, 'rb' ) ) {
while( !@feof( $file ) and ( connection_status()==0 ) ) {
print( fread( $file, 1024*8 ) );
flush();
}
@fclose( $file );
}
return( ( connection_status()==0 ) and !connection_aborted() );
}
}
/*
intercept the GET request, read the `Image` parameter
and call the above function.
*/
if( isset( $_GET['Image'] ) ){
$name=pathinfo( $_GET['Image'], PATHINFO_BASENAME );
$path=__DIR__ . '/'. $_GET['Image'];
# send the file and terminate.
exit( sendfile( $name, $path ) );
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Download images...</title>
</head>
<body>
<!--
in this example images are located within a sub-directory of the current
working script. The sub-folder is called "carousel" - the images are sequentially
named using numbers (that is not important here however)
-->
<table class='display_img'>
<tr>
<th>Image ID</th>
<th>Cms ID</th>
<th>Images</th>
</tr>
<tr>
<td>1</td>
<td>23</td>
<td><img height="100" src='./images/carousel/01.jpg'></td>
<td><a href='?Image=images/carousel/01.jpg' download><button class="btn-primary download-btn" name="download_btn">Download</a></td>
</tr>
<tr>
<td>2</td>
<td>23</td>
<td><img height="100" src='./images/carousel/02.jpg'></td>
<td><a href='?Image=images/carousel/02.jpg' download><button class="btn-primary download-btn" name="download_btn">Download</a></td>
</tr>
<tr>
<td>3</td>
<td>23</td>
<td><img height="100" src='./images/carousel/03.jpg'></td>
<td><a href='?Image=images/carousel/03.jpg' download><button class="btn-primary download-btn" name="download_btn">Download</a></td>
</tr>
</table>
</body>
</html>
请注意,这是一段包含PHP和HTML代码的示例,描述了如何处理GET请求以下载图像文件。如果您有其他问题或需要进一步的翻译,请告诉我。
英文:
An example, using hardcoded image paths and nonsense IDs to emulate what you might be drawing from the database.
Use the GET method as you are using a hyperlink with Image
parameter in querystring.
Do not output HTML ( or any content ) before calling header
function unless you explicitly flush the buffers before.
Add the download
attribute to each hyperlink.
<?php
error_reporting( E_ALL );
ini_set( 'display_errors', 1 );
# simple function to read file and send the binary contents
function sendfile( $filename=NULL, $filepath=NULL ){
if( file_exists( $filepath ) ){
if( !is_file( $filepath ) or connection_status()!=0 ) return FALSE;
header('Content-Type: application/image');
header('Content-Length:'.filesize( $filepath ) );
header("Content-Length: ".(string)( filesize( $filepath ) ) );
header("Content-Disposition: attachement; filename={$filename}");
header("Content-Transfer-Encoding: binary\n");
if( $file = @fopen( $filepath, 'rb' ) ) {
while( !@feof( $file ) and ( connection_status()==0 ) ) {
print( fread( $file, 1024*8 ) );
flush();
}
@fclose( $file );
}
return( ( connection_status()==0 ) and !connection_aborted() );
}
}
/*
intercept the GET request, read the `Image` parameter
and call the above function.
*/
if( isset( $_GET['Image'] ) ){
$name=pathinfo( $_GET['Image'], PATHINFO_BASENAME );
$path=__DIR__ . '/'. $_GET['Image'];
# send the file and terminate.
exit( sendfile( $name, $path ) );
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>Download images...</title>
</head>
<body>
<!--
in this example images are located within a sub-directory of the current
working script. The sub-folder is called "carousel" - the images are sequentially
named using numbers (that is not important here however)
-->
<table class='display_img'>
<tr>
<th>Image ID</th>
<th>Cms ID</th>
<th>Images</th>
</tr>
<tr>
<td>1</td>
<td>23</td>
<td><img height="100" src='./images/carousel/01.jpg'></td>
<td><a href='?Image=images/carousel/01.jpg' download><button class="btn-primary download-btn" name="download_btn">Download</a></td>
</tr>
<tr>
<td>2</td>
<td>23</td>
<td><img height="100" src='./images/carousel/02.jpg'></td>
<td><a href='?Image=images/carousel/02.jpg' download><button class="btn-primary download-btn" name="download_btn">Download</a></td>
</tr>
<tr>
<td>3</td>
<td>23</td>
<td><img height="100" src='./images/carousel/03.jpg'></td>
<td><a href='?Image=images/carousel/03.jpg' download><button class="btn-primary download-btn" name="download_btn">Download</a></td>
</tr>
</table>
</body>
</html>
答案2
得分: 0
以下是翻译好的内容:
解决方法非常简单。只需将download
属性添加到<a>
标签中,由于download
属性未指定特定路径,所以整个页面都会被下载。
<a download="<?php echo $path.$image; ?>" href="<?php echo $path.$image; ?>" class="btn-primary download-btn">下载</a>
这个方法非常有效。
谢谢大家,你们花费了宝贵的时间帮助解决了问题。我从你们那里学到了很多东西。如果没有你们,这是不可能实现的。
英文:
the solutions to this was purely simple. only adding the download
attribute to the <a>
was downloading the whole page because there was no specific path given to the download
attribute.
<a download="<?php echo $path.$image; ?>" href="<?php echo $path.$image; ?>" class="btn-primary download-btn">Download</a>
This works like a charm.
Thankyou guys, you gave your precious time to help resolve the issue. I did get to learn a lot of things from you guys. It wouldn't have been possible without you guys.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论