doutou3725 2019-06-24 06:56
浏览 491

如何使用php将图像从服务器上传到另一台服务器

I tried the below code for uploading the image from one server to another server using php. But am unable to get the image uploaded in the destination server. Please let me know if I did any wrong in my code.

Code used in form.php, which is on my source server:

<form enctype="multipart/form-data" encoding='multipart/form-data' method='post' action="form.php">
    <input name="uploadedfile" type="file" value="choose">
    <input type="submit" value="Upload">
   </form>
   <?
   if ( isset($_FILES['uploadedfile']) ) {
   $filename  = $_FILES['uploadedfile']['tmp_name'];
   $handle    = fopen($filename, "r");
   $data      = fread($handle, filesize($filename));
   $POST_DATA = array(
  'file' => base64_encode($data)
   );
   $curl = curl_init();
   curl_setopt($curl, CURLOPT_URL, 'https://www.kanchikart.in/img/handle.php');
   curl_setopt($curl, CURLOPT_TIMEOUT, 30);
   curl_setopt($curl, CURLOPT_POST, 1);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
   $response = curl_exec($curl);
   curl_close ($curl);
   echo "<h2>File Uploaded</h2>";
   }
  ?>

and in my handler.php which is on my destination server:

<?php
  $encoded_file = $_POST['file'];
  $decoded_file = base64_decode($encoded_file);
  /* Now you can copy the uploaded file to your server. */
  file_put_contents('<span style="color: red;">subins</span>', $decoded_file);
  ?>

After trying with the above code I am not getting the image to be stored in my destination server.

  • 写回答

1条回答 默认 最新

  • duanpa1898 2019-06-24 07:20
    关注

    When dealing with https endpoints you need additional options set in the curl request - specifically a valid cacert... you can download your own copy of cacert.pem from curl.haxx.se

    Test the response or use the info ( http_code ) to determine success or failure. The following appeared to work for me and yielded a 200 response code ~ check to see if there is a new file

    <?php
    
        if ( isset($_FILES['uploadedfile']) ) {
    
            $filename  = $_FILES['uploadedfile']['tmp_name'];
            $handle    = fopen($filename, "r");
            $data      = fread($handle, filesize($filename));
    
            $POST_DATA = array(
                'file' => base64_encode( $data )
            );
            $cacert='c:/wwwroot/cacert.pem';
            $url='https://www.kanchikart.in/img/handle.php';
    
            $curl = curl_init();
            if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
                curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
                curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
                curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
            }
    
            curl_setopt($curl, CURLOPT_URL, $url );
            curl_setopt($curl, CURLOPT_TIMEOUT, 30);
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA);
    
            $response = curl_exec($curl);
            $info = (object)curl_getinfo( $curl );
            curl_close ($curl);
    
    
            echo ( $info->http_code==200 ) ? "<h2>File Uploaded</h2>" : "<h2>Bogus</h2>";
    
        }
    ?>
    <!DOCTYPE html>
    <html lang='en'>
        <head>
            <meta charset='utf-8' />
            <title>cURL - Server to Server....</title>
        </head>
        <body>
            <form enctype="multipart/form-data" encoding='multipart/form-data' method='post'>
                <input name="uploadedfile" type="file" value="choose">
                <input type="submit" value="Upload">
            </form>
        </body>
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100