PHP cURL 文件上传
 
本教程将指导如何使用 cURL 和 CURLFile 类将图像文件发送到服务器。
这个想法是将图像文件从一个页面发布到另一个页面上的另一个页面。
- Index.php:首先,我们将文件- image发送到- index.php。其次,我们将文件重定向到- curl.php。
- Curl.php:之后,我们使用- cURLFile class将其移动到上传文件夹。
PHP 中的 cURLFile 上传方法
例如,我们将图像发布到 index.php 页面,然后在 curl 的帮助下发布到 curl.php 页面。
我们通过使用 curl 和 CURLFile 类功能来做到这一点。让我们以有效的语法查看函数参数。
//create curl file
if (function_exists('curl_file_create')) {
//determine file path 
  $tmp_file = curl_file_create($file_name_with_full_path);
} else { // 
  $tmp_file = '@' . realpath($file_name_with_full_path);
}
//store data in an array index
$data = array('extra_info' => '123456789','file_contents'=> $tmp_file);
$init = curl_init();
//function parameteres
curl_setopt($init, CURLOPT_URL,$url);
curl_setopt($init, CURLOPT_POST,1);
curl_setopt($init, CURLOPT_POSTFIELDS, $data);
$res=curl_exec ($init);
curl_close ($init);
上面的示例可以取决于用户定义的条件,因为 URL 可以指向任何服务器位置。
但是,我们现在将创建一个可用于在服务器之间移动文件的工作示例。它将允许你阐明如何使用 curl 文件类。
<!DOCTYPE html>
<html>
<body>
<form action="index.php" method="post" enctype="multipart/form-data" align="center">
<input type="file" name="image" />
<input type="submit" value= "Submit">
</form>
</body>
</html>
通常情况下,HTML 表单收集输入文件名并与 php.ini 中的 $_POST 或 $_REQUEST 变量一起发送。
但是,借助 curl 文件功能,我们可以将其重定向到不同的服务器。
curl.php 而不是其他服务器,因为我们在本地主机上,但它可以在任何远程服务器上。PHP cURL 文件上传工作示例
index.php:
<?php
    
if (isset($_FILES['image']['tmp_name'])){
	//create a handler for curl function 
$curl = curl_init(); //initialzie cURL session
//The CURLFile class 
$cfile = new CURLFile($_FILES['image']['tmp_name'], $_FILES['image']['type'], $_FILES['image']['name']);
//use array to post data to different server or within localhost 
$data = array ("myimage"=>$cfile);
//this url can be directed to any server location
//for the sake of this demo, we are using localhost directory to upload images in the upload folder
//Sets an option on the given cURL session handle
curl_setopt($curl, CURLOPT_URL, "http://localhost:7882/curl/curl.php"); 
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 
	//assign  execute curl to a response varible
	$result = curl_exec($curl);
	//check if the response varible is true 
	if($result == true){
	
		echo "Your image has been uploaded successfully";
	}
else {
	// curl error returns a string comprising of error for the current session
	echo "Error!" . curl_error($curl);
}
}
?>
输出:

output.gif 图像显示文件目录以及文件如何从 index.php 移动到 curl.php。然后移动到上传文件夹。
Curl.php 代码:
<?php
//determine the array index 
if(isset($_FILES['myimage']['tmp_name'])){
//set the path in the path variable
	$path = "uploads/" . $_FILES['myimage']['name'];
	//move the file to the path 
	move_uploaded_file($_FILES['myimage']['tmp_name'], $path); 
?>
结论
我们已经展示了文件和我们如何从一台服务器到另一台服务器执行 CURLFile 类的输出。
用户可以在需要在不同服务器位置上传文件的各种情况下使用此方法。
Sarwan Soomro is a freelance software engineer and an expert technical writer who loves writing and coding. He has 5 years of web development and 3 years of professional writing experience, and an MSs in computer science. In addition, he has numerous professional qualifications in the cloud, database, desktop, and online technologies. And has developed multi-technology programming guides for beginners and published many tech articles.
LinkedIn