获取链接但图像未下载。

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

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.

  1. $sql_img = "SELECT * FROM cms
  2. JOIN images ON cms.C_ID = images.C_ID
  3. WHERE B_Name = '$select'";
  4. $sql_img_run = mysqli_query($con, $sql_img);
  5. ?>
  6. <table class="display_img">
  7. <tr>
  8. <th>Image ID</th>
  9. <th>Cms ID</th>
  10. <th>Images</th>
  11. </tr>
  12. <?php
  13. while ($row = mysqli_fetch_array($sql_img_run)) {
  14. $img_id = $row['Img_ID'];
  15. $image = $row['Image'];
  16. $cms_id = $row['C_ID'];
  17. $path = "http://localhost/Airan/api/";
  18. ?>
  19. <tr>
  20. <td><?php echo $img_id ?></td>
  21. <td><?php echo $cms_id ?></td>
  22. <td><?php echo '<img height="100" src=' . $path . $image . '>' ?></td>
  23. <td>
  24. <a href="home.php?Image=<?php echo $path . $image; ?>" class="btn-primary download-btn">Download</a>
  25. </td>
  26. </tr>
  27. <?php } ?>
  28. </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.

  1. $sql_img = &quot;SELECT * FROM cms
  2. JOIN images ON cms.C_ID = images.C_ID
  3. WHERE B_Name = &#39;$select&#39;&quot;;
  4. $sql_img_run = mysqli_query($con, $sql_img);
  5. ?&gt;
  6. &lt;table class=&quot;display_img&quot;&gt;
  7. &lt;tr&gt;
  8. &lt;th&gt;Image ID&lt;/th&gt;
  9. &lt;th&gt;Cms ID&lt;/th&gt;
  10. &lt;th&gt;Images&lt;/th&gt;
  11. &lt;/tr&gt;
  12. &lt;?php
  13. while ($row = mysqli_fetch_array($sql_img_run)) {
  14. $img_id = $row[&#39;Img_ID&#39;];
  15. $image = $row[&#39;Image&#39;];
  16. $cms_id = $row[&#39;C_ID&#39;];
  17. // $path = &quot;C:\xampp\htdocs\Airan\Api\&quot;;
  18. $path = &quot;http://localhost/Airan/api/&quot;;
  19. ?&gt;
  20. &lt;tr&gt;
  21. &lt;td&gt;&lt;?php echo $img_id ?&gt;&lt;/td&gt;
  22. &lt;td&gt;&lt;?php echo $cms_id ?&gt;&lt;/td&gt;
  23. &lt;td&gt;&lt;?php echo &#39;&lt;img height=&quot;100&quot; src=&#39;.$path.$image.&#39;&gt;&#39;?&gt;&lt;/td&gt;
  24. &lt;td&gt;
  25. &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;
  26. &lt;/td&gt;
  27. &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.

  1. error_reporting( E_ALL );
  2. ini_set( 'display_errors', 1 );
  3. # simple function to read file and send the binary contents
  4. function sendfile( $filename=NULL, $filepath=NULL ){
  5. if( file_exists( $filepath ) ){
  6. if( !is_file( $filepath ) or connection_status()!=0 ) return FALSE;
  7. header('Content-Type: application/image');
  8. header('Content-Length:'.filesize( $filepath ) );
  9. header("Content-Length: ".(string)( filesize( $filepath ) ) );
  10. header("Content-Disposition: attachement; filename={$filename}");
  11. header("Content-Transfer-Encoding: binary\n");
  12. if( $file = @fopen( $filepath, 'rb' ) ) {
  13. while( !@feof( $file ) and ( connection_status()==0 ) ) {
  14. print( fread( $file, 1024*8 ) );
  15. flush();
  16. }
  17. @fclose( $file );
  18. }
  19. return( ( connection_status()==0 ) and !connection_aborted() );
  20. }
  21. }
  22. /*
  23. intercept the GET request, read the `Image` parameter
  24. and call the above function.
  25. */
  26. if( isset( $_GET['Image'] ) ){
  27. $name=pathinfo( $_GET['Image'], PATHINFO_BASENAME );
  28. $path=__DIR__ . '/'. $_GET['Image'];
  29. # send the file and terminate.
  30. exit( sendfile( $name, $path ) );
  31. }
  32. ?>
  33. <!DOCTYPE html>
  34. <html lang='en'>
  35. <head>
  36. <meta charset='utf-8' />
  37. <title>Download images...</title>
  38. </head>
  39. <body>
  40. <!--
  41. in this example images are located within a sub-directory of the current
  42. working script. The sub-folder is called "carousel" - the images are sequentially
  43. named using numbers (that is not important here however)
  44. -->
  45. <table class='display_img'>
  46. <tr>
  47. <th>Image ID</th>
  48. <th>Cms ID</th>
  49. <th>Images</th>
  50. </tr>
  51. <tr>
  52. <td>1</td>
  53. <td>23</td>
  54. <td><img height="100" src='./images/carousel/01.jpg'></td>
  55. <td><a href='?Image=images/carousel/01.jpg' download><button class="btn-primary download-btn" name="download_btn">Download</a></td>
  56. </tr>
  57. <tr>
  58. <td>2</td>
  59. <td>23</td>
  60. <td><img height="100" src='./images/carousel/02.jpg'></td>
  61. <td><a href='?Image=images/carousel/02.jpg' download><button class="btn-primary download-btn" name="download_btn">Download</a></td>
  62. </tr>
  63. <tr>
  64. <td>3</td>
  65. <td>23</td>
  66. <td><img height="100" src='./images/carousel/03.jpg'></td>
  67. <td><a href='?Image=images/carousel/03.jpg' download><button class="btn-primary download-btn" name="download_btn">Download</a></td>
  68. </tr>
  69. </table>
  70. </body>
  71. </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.

  1. &lt;?php
  2. error_reporting( E_ALL );
  3. ini_set( &#39;display_errors&#39;, 1 );
  4. # simple function to read file and send the binary contents
  5. function sendfile( $filename=NULL, $filepath=NULL ){
  6. if( file_exists( $filepath ) ){
  7. if( !is_file( $filepath ) or connection_status()!=0 ) return FALSE;
  8. header(&#39;Content-Type: application/image&#39;);
  9. header(&#39;Content-Length:&#39;.filesize( $filepath ) );
  10. header(&quot;Content-Length: &quot;.(string)( filesize( $filepath ) ) );
  11. header(&quot;Content-Disposition: attachement; filename={$filename}&quot;);
  12. header(&quot;Content-Transfer-Encoding: binary\n&quot;);
  13. if( $file = @fopen( $filepath, &#39;rb&#39; ) ) {
  14. while( !@feof( $file ) and ( connection_status()==0 ) ) {
  15. print( fread( $file, 1024*8 ) );
  16. flush();
  17. }
  18. @fclose( $file );
  19. }
  20. return( ( connection_status()==0 ) and !connection_aborted() );
  21. }
  22. }
  23. /*
  24. intercept the GET request, read the `Image` parameter
  25. and call the above function.
  26. */
  27. if( isset( $_GET[&#39;Image&#39;] ) ){
  28. $name=pathinfo( $_GET[&#39;Image&#39;], PATHINFO_BASENAME );
  29. $path=__DIR__ . &#39;/&#39;. $_GET[&#39;Image&#39;];
  30. # send the file and terminate.
  31. exit( sendfile( $name, $path ) );
  32. }
  33. ?&gt;
  34. &lt;!DOCTYPE html&gt;
  35. &lt;html lang=&#39;en&#39;&gt;
  36. &lt;head&gt;
  37. &lt;meta charset=&#39;utf-8&#39; /&gt;
  38. &lt;title&gt;Download images...&lt;/title&gt;
  39. &lt;/head&gt;
  40. &lt;body&gt;
  41. &lt;!--
  42. in this example images are located within a sub-directory of the current
  43. working script. The sub-folder is called &quot;carousel&quot; - the images are sequentially
  44. named using numbers (that is not important here however)
  45. --&gt;
  46. &lt;table class=&#39;display_img&#39;&gt;
  47. &lt;tr&gt;
  48. &lt;th&gt;Image ID&lt;/th&gt;
  49. &lt;th&gt;Cms ID&lt;/th&gt;
  50. &lt;th&gt;Images&lt;/th&gt;
  51. &lt;/tr&gt;
  52. &lt;tr&gt;
  53. &lt;td&gt;1&lt;/td&gt;
  54. &lt;td&gt;23&lt;/td&gt;
  55. &lt;td&gt;&lt;img height=&quot;100&quot; src=&#39;./images/carousel/01.jpg&#39;&gt;&lt;/td&gt;
  56. &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;
  57. &lt;/tr&gt;
  58. &lt;tr&gt;
  59. &lt;td&gt;2&lt;/td&gt;
  60. &lt;td&gt;23&lt;/td&gt;
  61. &lt;td&gt;&lt;img height=&quot;100&quot; src=&#39;./images/carousel/02.jpg&#39;&gt;&lt;/td&gt;
  62. &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;
  63. &lt;/tr&gt;
  64. &lt;tr&gt;
  65. &lt;td&gt;3&lt;/td&gt;
  66. &lt;td&gt;23&lt;/td&gt;
  67. &lt;td&gt;&lt;img height=&quot;100&quot; src=&#39;./images/carousel/03.jpg&#39;&gt;&lt;/td&gt;
  68. &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;
  69. &lt;/tr&gt;
  70. &lt;/table&gt;
  71. &lt;/body&gt;
  72. &lt;/html&gt;

答案2

得分: 0

以下是翻译好的内容:

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

  1. &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:

确定