gpt4 book ai didi

PHP - 使用 CURL 将图像文件上传到其他域

转载 作者:可可西里 更新时间:2023-11-01 13:45:23 25 4
gpt4 key购买 nike

我有两个域,例如 site1.locsite2.loc。在 site1.loc 我有一个这样的 php 表单文件:

<?php
$c_name = "";
$c_phone = "";

if($_SERVER['REQUEST_METHOD']=="POST"){

$c_name = $_POST['c_name'];
$c_phone = $_POST['c_phone'];
$c_pic = $_FILES['c_pic']['name']; // Image file

// submit target URL
$url = 'http://site2.loc/handler.php';

$fields = array(
'field1'=>$c_name,
'field2'=>$c_phone,
'field3'=>$c_pic
);

$postvars='';
$sep='';
foreach($fields as $key=>$value)
{
$postvars.= $sep.urlencode($key).'='.urlencode($value);
$sep='&';
}


//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);

//execute post
$result = curl_exec($ch);

if(curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
else {
echo $result;
}

//close connection
curl_close($ch);

}


echo '
<form action="" method="post" enctype="multipart/form-data">
Name : <input type="text" name="c_name" value="'.$c_name.'" /> <br />
Phone : <input type="text" name="c_phone" value="'.$c_phone.'" /> <br />
Image : <input type="file" name="c_pic" /> <br />
<input type="submit" />
</form>
';
?>

site2.loc 中的 handler.php 像这样:

<?php
ob_start();
if (!isset($_SESSION)) { session_start(); }

// CONNECT TO DB
$db_con = mysql_connect("localhost", "root", "root");// or die("Could not connect to db.");
if(!mysql_select_db("site2",$db_con)) die("No database selected.");

// POST
if(isset($_POST)){
$c_name = $_POST['field1'];
$c_phone = $_POST['field2'];
$c_pic = $_POST['field3'];

// UPLOAD FILE
/* UPLOAD IMAGE CODE HERE */

// INSERT TO DB
if(mysql_query("INSERT INTO kontak (nama, telpon) VALUES ('$c_name','$c_phone')")){
echo "INSERT SUCCESS";
} else {
echo "INSERT FAILED";
}
}

?>

此脚本用于将数据存储到数据库运行良好,但不能用于上传图像文件。谁能帮我修改上面的脚本以上传图像文件?

先谢谢了。

最佳答案

这个答案中没有代码,只是一个绕过 curl 的工作流程示例:

  1. 用户按照正常方式将包含上传文件的表单发布到“站点 1”。
  2. “站点 1”处理表单和上传的文件。该文件放置在可通过网络访问的临时目录中。
  3. 在“站点 1”上,检查上传的文件后,调用 file_get_contents('http://site2.loc/pullfile.php?f=filename&sec=checkcode') 调用。 pullfile.php 的内容就是这样做的,将文件从“站点 1”拉到“站点 2”。
  4. 可以检查 file_get_contents() 的返回是否有来自“站点 2”pullfile.php 的错误返回,如果有错误,则进行处理。没有错误,删除临时文件。
  5. &sec=checkcode 可用于确认文件已成功上传到“站点 2”。它可以是文件的 MD5 或您想到的其他内容。

只是一个想法。

编辑:一些示例代码可以帮助使事情更清楚,也许:]

// ---- Site 1, formprocess.php ----

// Your form processing goes here, including
// saving the uploaded file to a temp dir.

// Once the uploaded file is checked for errors,
// you need move it to a known temp folder called
// 'tmp'.

// this is the uploaded file name (which you would
// have got from the processed form data in $_FILES
// For this sample code, it is simple hard-coded.
$site1File = 'test.jpg';

$site1FileMd5 = md5_file('./tmp/'.$site1File);

// now make a remote request to "site 2"
$site2Result = file_get_contents('http://'.SITE_2_URL.'/pullfile.php?f='.$site1File.'&md5='.$site1FileMd5);

if ($site2Result == 'done') {
unlink('./tmp/'.$site1File);
} else {
echo '<p>
Uploaded file failed to transfer to '.SITE_2_URL.'
- but will be dealt with later.
</p>';
}

这将是站点 2 上的“pullfile.php”

// ----- Site 2, pullfile.php -----

// This script will pull a file from site 1 and
// place it in '/uploaded'

// used to cross-check the uploaded file
$fileMd5 = $_GET['md5'];
$fileName = basename($_GET['f']);

// we need to pull the file from the './tmp/' dir on site 1
$pulledFile = file_get_contents('http://'.SITE_1_URL.'/tmp/'.$fileName);

// save that file to disk
$result = file_put_contents('./uploaded/'.$fileName,$pulledFile);
if (! $result) {
echo 'Error: problem writing file to disk';
exit;
}

$pulledMd5 = md5_file('./uploaded/'.$fileName);
if ($pulledMd5 != $fileMd5) {
echo 'Error: md5 mis-match';
exit;
}

// At this point, everything should be right.
// We pass back 'done' to site 1, so we know
// everything went smooth. This way, a 'blank'
// return can be treated as an error too.
echo 'done';
exit;

关于PHP - 使用 CURL 将图像文件上传到其他域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16584307/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com