Curl PHP文件上传 [英] Curl PHP File Upload
问题描述
嘿,试图使用curl发布文件,一切都很好。我有一个问题。我不能在我的post_file()函数之外声明我的文件。
Hey, trying to post a file using curl and all is working great. I have one problem. I can't declare my file outside of my post_file() function. I call this function in my application many times so want it to be reusable.
这样就可以工作了:
function call_me(){
$file_path = "/home/myfile.mov";
$url = "http://myurl.com";
$this->post_file($url, $file_path);
}
function post_file($url, $file_path){
$data['Filedata'] = "@".$file_path;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
return $response;
}
但这不会:
function call_me(){
$file_path = "/home/myfile.mov";
$url = "http://myurl.com";
$data['Filedata'] = "@".$file_path;
$this->post_file($url, $data);
}
function post_file($url, $data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
return $response;
}
任何想法?干杯。
推荐答案
我真的没有看到两个代码集之间的差异(重新可用性)。 #2 的唯一好处是你传递了整个 $ data
对象 - 如果这是一个好处,你的CURL post的安全问题...所以这真的比每次创建一个新的 $ data
对象(每#1 )?使用函数名 post_file
,预期行为将是#1 - 将单个文件发布到URL,而代码#2 可以被利用/用于其他类型的东西。 #1的可用性增强可能是:
I don't really see a difference (for re-usability) between the two code sets. The only benefit of #2, is you're passing in the whole $data
object - if that's a benefit at all... it leads to security issues with your CURL post... so is this really better than creating a new $data
object each time (per #1)? With the function name post_file
the expected behaviour would be per #1 - post a single file to the URL, while code #2 could be exploited/used for other kinds of things. Perhaps a usability enhancement of #1 would be:
function post_files($url,$files) {
//Post 1-n files, each element of $files array assumed to be absolute
// path to a file. $files can be array (multiple) or string (one file).
// Data will be posted in a series of POST vars named $file0, $file1...
// $fileN
$data=array();
if (!is_array($files)) {
//Convert to array
$files[]=$files;
}
$n=sizeof($files);
for ($i=0;$i<$n;$i++) {
$data['file'+$i]="@".$files[$i];
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
return $response;
}
至于为什么它不适合你现在 - 我猜在某处有错别字。这个代码是否完全复制,或者是你为我们改写?在 curl_setopt($ ch,CURLOPT_POSTFIELDS,$ data);
行之前尝试 print_r($ data);
As to why it's not working for you right now - I'm guessing there's a typo in there somewhere. Is this code copied exactly or are you paraphrasing for us? Try a print_r($data);
just before the curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
line.
函数无法知道对象是在函数(#1 )中创建还是传入( 2 )。
There's no way for a function to know if the object was created within the function (#1) or passed in (#2).
这篇关于Curl PHP文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!