search
HomeBackend DevelopmentPHP Tutorial200分CURL设置HTTPHEADER上传文件有关问题!(补充提问)

200分求助CURL设置HTTPHEADER上传文件问题!(补充提问)

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$boundary   = md5(time());$postStr  = "";$postStr .="--".$boundary."\r\n";$postStr .="Content-Disposition: form-data; name=\"uptxt[]\"; filename=\"index_1.html\"\r\n";$postStr .="Content-Type: text/html\r\n\r\n";$postStr .=$uploadFile."\r\n"; #这里是部分文件内容$postStr .="--".$boundary."\r\n";


$postStr .="Content-Disposition: form-data; name=\"uptxt[]\"; filename=\"index_1.html\"\r\n";
$postStr .="Content-Type: text/html\r\n\r\n";

改为

$postStr .="Content-Disposition: form-data; name=\"uptxt[]\"; filename=\"index_1.html\""; //删除\r\n
$postStr .="Content-Type: text/html"; //删除\r\n

之后还是可以上传成功 


而且

$postStr .="Content-Disposition: form-data; name=\"uptxt[]\"; filename=\"index_1.html\"\r\n";
$postStr .="Content-Type: text/html\r\n\r\n";

改为

$postStr .='Content-Disposition: form-data; name="uptxt[]"; filename="index_1.html"\r\n';//改为单引号,\r\n不再转义
$postStr .='Content-Type: text/html\r\n\r\n'; //改为单引号,\r\n不再转义

也可以上传成功,求解

------解决方案--------------------
如你所改,我测试,接收端,没有获取到期望的信息

或贴完整代码看看?
------解决方案--------------------
协议就是要来遵守的,如果不遵守,后果的不可预知的。
你能请求成功,但是会让web服务器解析http时乱套,/号和换行符都是重要的符号标志。

比方说现在boundary块如下,注意Content-type:text~html,去除\r\n且把/换成~
PHP code
$boundary   = md5(time());$postStr  = ""; $postStr .="--".$boundary."\r\n";$postStr .="Content-Disposition: form-data; name=\"uptxt[]\"; filename=\"index_1.html\"";$postStr .="Content-Type: text~html";                                                                                                       $postStr .="123\r\n"; #这里是部分文件内容$postStr .="--".$boundary."\r\n";<br><font color="#e78608">------解决方案--------------------</font><br>不是用curl吗?网上找了一段是这样的:<br>
Perl code
<?php $url  = 'http://www.myheritage.cn/FP/Company/tryFaceRecognition.php';//target url $fields['file'] = '@'.'C:\Users\lzyy\Desktop\3431821560_5e18c02221.jpg';$fields['lang'] = 'ZH';$fields['morph'] = '1';$fields['popup'] = 'PJSLVMMT';$fields['loadMethod'] = 'myFiles'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url );curl_setopt($ch, CURLOPT_POST, 1 );curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt ($ch, CURLOPT_REFERER, "http://www.myheritage.cn/");curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);//get redirect content curl_exec( $ch );//$rs = curl_exec($ch); if ($error = curl_error($ch) ) {          die($error);}curl_close($ch);//print_r(htmlspecialchars($rs));echo 'ok';?><br><font color="#e78608">------解决方案--------------------</font><br>
探讨
协议就是要来遵守的,如果不遵守,后果的不可预知的。
你能请求成功,但是会让web服务器解析http时乱套,/号和换行符都是重要的符号标志。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools