获取链接但图像未下载。

huangapple go评论50阅读模式
英文:

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 = &quot;SELECT * FROM cms
        	JOIN images ON cms.C_ID = images.C_ID
			WHERE B_Name = &#39;$select&#39;&quot;;
$sql_img_run = mysqli_query($con, $sql_img);

			?&gt;
			
			&lt;table class=&quot;display_img&quot;&gt;
					&lt;tr&gt;
						&lt;th&gt;Image ID&lt;/th&gt;
						&lt;th&gt;Cms ID&lt;/th&gt;
						&lt;th&gt;Images&lt;/th&gt;
					&lt;/tr&gt;

			&lt;?php

			while ($row = mysqli_fetch_array($sql_img_run)) {

				$img_id = $row[&#39;Img_ID&#39;];
				$image = $row[&#39;Image&#39;];
				$cms_id = $row[&#39;C_ID&#39;];
			
				// $path = &quot;C:\xampp\htdocs\Airan\Api\&quot;;
				$path = &quot;http://localhost/Airan/api/&quot;;
				?&gt;
					&lt;tr&gt;
						&lt;td&gt;&lt;?php echo $img_id ?&gt;&lt;/td&gt;
						&lt;td&gt;&lt;?php echo $cms_id ?&gt;&lt;/td&gt;
						&lt;td&gt;&lt;?php echo &#39;&lt;img height=&quot;100&quot; src=&#39;.$path.$image.&#39;&gt;&#39;?&gt;&lt;/td&gt;
						&lt;td&gt;
							&lt;a href=&quot;home.php?Image=&lt;?php echo $path.$image; ?&gt;&quot; class=&quot;btn-primary download-btn&quot;&gt;Download&lt;/a&gt;
						&lt;/td&gt;
					&lt;/tr&gt;

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.

&lt;?php
error_reporting( E_ALL );
ini_set( &#39;display_errors&#39;, 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(&#39;Content-Type: application/image&#39;);
header(&#39;Content-Length:&#39;.filesize( $filepath ) );
header(&quot;Content-Length: &quot;.(string)( filesize( $filepath ) ) );
header(&quot;Content-Disposition: attachement; filename={$filename}&quot;);
header(&quot;Content-Transfer-Encoding: binary\n&quot;);
if( $file = @fopen( $filepath, &#39;rb&#39; ) ) {
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[&#39;Image&#39;] ) ){
$name=pathinfo( $_GET[&#39;Image&#39;], PATHINFO_BASENAME );
$path=__DIR__ . &#39;/&#39;. $_GET[&#39;Image&#39;];
# send the file and terminate.
exit( sendfile( $name, $path ) );
}
?&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang=&#39;en&#39;&gt;
&lt;head&gt;
&lt;meta charset=&#39;utf-8&#39; /&gt;
&lt;title&gt;Download images...&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;!--
in this example images are located within a sub-directory of the current
working script. The sub-folder is called &quot;carousel&quot; - the images are sequentially
named using numbers (that is not important here however)
--&gt;
&lt;table class=&#39;display_img&#39;&gt;
&lt;tr&gt;
&lt;th&gt;Image ID&lt;/th&gt;
&lt;th&gt;Cms ID&lt;/th&gt;
&lt;th&gt;Images&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;23&lt;/td&gt;
&lt;td&gt;&lt;img height=&quot;100&quot; src=&#39;./images/carousel/01.jpg&#39;&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href=&#39;?Image=images/carousel/01.jpg&#39; download&gt;&lt;button class=&quot;btn-primary download-btn&quot; name=&quot;download_btn&quot;&gt;Download&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;23&lt;/td&gt;
&lt;td&gt;&lt;img height=&quot;100&quot; src=&#39;./images/carousel/02.jpg&#39;&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href=&#39;?Image=images/carousel/02.jpg&#39; download&gt;&lt;button class=&quot;btn-primary download-btn&quot; name=&quot;download_btn&quot;&gt;Download&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;23&lt;/td&gt;
&lt;td&gt;&lt;img height=&quot;100&quot; src=&#39;./images/carousel/03.jpg&#39;&gt;&lt;/td&gt;
&lt;td&gt;&lt;a href=&#39;?Image=images/carousel/03.jpg&#39; download&gt;&lt;button class=&quot;btn-primary download-btn&quot; name=&quot;download_btn&quot;&gt;Download&lt;/a&gt;&lt;/td&gt;
&lt;/tr&gt;		
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;

答案2

得分: 0

以下是翻译好的内容:

解决方法非常简单。只需将download属性添加到&lt;a&gt;标签中,由于download属性未指定特定路径,所以整个页面都会被下载。

&lt;a download=&quot;&lt;?php echo $path.$image; ?&gt;&quot; href=&quot;&lt;?php echo $path.$image; ?&gt;&quot; class=&quot;btn-primary download-btn&quot;&gt;下载&lt;/a&gt;

这个方法非常有效。

谢谢大家,你们花费了宝贵的时间帮助解决了问题。我从你们那里学到了很多东西。如果没有你们,这是不可能实现的。

英文:

the solutions to this was purely simple. only adding the download attribute to the &lt;a&gt; was downloading the whole page because there was no specific path given to the download attribute.

&lt;a download=&quot;&lt;?php echo $path.$image; ?&gt;&quot; href=&quot;&lt;?php echo $path.$image; ?&gt;&quot; class=&quot;btn-primary download-btn&quot;&gt;Download&lt;/a&gt;

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.

huangapple
  • 本文由 发表于 2023年5月10日 14:55:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定