搜索
首页后端开发php教程如何实现图片下载

有这样一个地址 打开这个地址是图片  http://qr.topscan.com/api.php?text=x  
想实现这样的一个功能
1.html

点击下载图片

1.php

     $filename= http://qr.topscan.com/api.php?text=x  
     header('Content-type: image/jpeg'); 
     header("Content-Disposition: attachment; filename='$filename'"); 


这样没办法下载图片,请教一下大家应该怎么写


   


回复讨论(解决方案)

即使能下载,下载的图片也是不显示的

<?php$file = 'test.zip';$filename = '中文.zip';if(file_exists($file)){	$user_agent = $_SERVER['Http_User_agent'];	$encode_filename = rawurlencode($filename);	if(preg_match("/MSIE/", $user_agent)){		header('content-disposition:attachment; filename="'.$encode_filename.'"');	}else if(preg_match("/Firefox/", $user_agent)){		header("content-disposition:attachment; filename*=\"utf8''".$filename.'"');	}else{		header('content-disposition:attachment; filename="'.$filename.'"');	}	readfile($file);}?>

<?php$file = 'test.zip';$filename = '中文.zip';if(file_exists($file)){	$user_agent = $_SERVER['Http_User_agent'];	$encode_filename = rawurlencode($filename);	if(preg_match("/MSIE/", $user_agent)){		header('content-disposition:attachment; filename="'.$encode_filename.'"');	}else if(preg_match("/Firefox/", $user_agent)){		header("content-disposition:attachment; filename*=\"utf8''".$filename.'"');	}else{		header('content-disposition:attachment; filename="'.$filename.'"');	}	readfile($file);}?>


谢谢你的热心回答,但是我试了一下,还是不能下载成功
<?php$filename= 'http://qr.topscan.com/api.php?text=x';    if(file_exists($filename)){    $user_agent = $_SERVER['Http_User_agent'];    $encode_filename = rawurlencode($filename);     if(preg_match("/MSIE/", $user_agent)){        header('content-disposition:attachment; filename="'.$encode_filename.'"');    }else if(preg_match("/Firefox/", $user_agent)){        header("content-disposition:attachment; filename*=\"utf8''".$filename.'"');    }else{        header('content-disposition:attachment; filename="'.$filename.'"');    }	echo 333;    readfile($filename);}// header('Content-type: image/jpeg');  //header("Content-Disposition: attachment; filename='$filename'"); ?>

不明白你们在干什么?
点击下载图片
如果是为了显示图片,则 1.php 为

echo file_get_contents('http://qr.topscan.com/api.php?text=x');

如果是为了下载图片,则 1.php 为
$s = file_get_contents('http://qr.topscan.com/api.php?text=x');Header("Content-type: application/force-download");Header("Accept-Ranges:bytes");Header("Accept-Length:" . strlen($s));Header("Content-Disposition: attachment; filename=x.png");echo $s;

file_exists 函数只用于检查本地文件是否存在

不明白你们在干什么?
点击下载图片
如果是为了显示图片,则 1.php 为

echo file_get_contents('http://qr.topscan.com/api.php?text=x');

如果是为了下载图片,则 1.php 为
$s = file_get_contents('http://qr.topscan.com/api.php?text=x');Header("Content-type: application/force-download");Header("Accept-Ranges:bytes");Header("Accept-Length:" . strlen($s));Header("Content-Disposition: attachment; filename=x.png");echo $s;

file_exists 函数只用于检查本地文件是否存在




谢谢版主的热心回答,是想下载图片来着, ,借这个帖子在请教一个问题,处理数据的时候使用到了事务,模式是这样的

开启事务
select * from user  where id=1 for update
处理数据
如果成功
commit  
失败
rollback
commit  
我是想问一下
如果处理数据失败,rollback 后还需要commit吗? 表示事务处理完成

commit 是提交事务

开启事务
若干条 SQL 指令
如果任意一条失败  rollback
commit

commit 是提交事务

开启事务
若干条 SQL 指令
如果任意一条失败  rollback
commit

  谢谢版主热心回答
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
如何使PHP应用程序更快如何使PHP应用程序更快May 12, 2025 am 12:12 AM

tomakephpapplicationsfaster,关注台词:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

PHP性能优化清单:立即提高速度PHP性能优化清单:立即提高速度May 12, 2025 am 12:07 AM

到ImprovephPapplicationspeed,关注台词:1)启用opcodeCachingwithapCutoredUcescriptexecutiontime.2)实现databasequerycachingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandredececonnection.4 limitsclection.4.4

PHP依赖注入:提高代码可检验性PHP依赖注入:提高代码可检验性May 12, 2025 am 12:03 AM

依赖注入(DI)通过显式传递依赖关系,显着提升了PHP代码的可测试性。 1)DI解耦类与具体实现,使测试和维护更灵活。 2)三种类型中,构造函数注入明确表达依赖,保持状态一致。 3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

PHP性能优化:数据库查询优化PHP性能优化:数据库查询优化May 12, 2025 am 12:02 AM

databasequeryOptimizationinphpinvolVolVOLVESEVERSEVERSTRATEMIESOENHANCEPERANCE.1)SELECTONLYNLYNESSERSAYCOLUMNSTORMONTOUMTOUNSOUDSATATATATATATATATATATRANSFER.3)

简单指南:带有PHP脚本的电子邮件发送简单指南:带有PHP脚本的电子邮件发送May 12, 2025 am 12:02 AM

phpisusedforsenderemailsduetoitsbuilt-inmail()函数andsupportiveLibrariesLikePhpMailerandSwiftMailer.1)usethemail()functionforbasicemails,butithasimails.2)butithasimimitations.2)

PHP性能:识别和修复瓶颈PHP性能:识别和修复瓶颈May 11, 2025 am 12:13 AM

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显着提升PHP应用的性能。

PHP的依赖注入:快速摘要PHP的依赖注入:快速摘要May 11, 2025 am 12:09 AM

依赖性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增强量产生性,可验证性和Maintainability.itallowspasspassingDepentenciesLikEdenceSeconnectionSeconnectionStoclasseconnectionStoclasseSasasasasareTers,interitationApertatingAeseritatingEaseTestingEasingEaseTeStingEasingAndScalability。

提高PHP性能:缓存策略和技术提高PHP性能:缓存策略和技术May 11, 2025 am 12:08 AM

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中