PHP的问题上传图像到远程服务器

时间:2018-12-09 14:37:52

标签: php

你好,有人可以帮助我解决我的问题。 我正在将基本图像上传到远程服务器,这是我的代码 本地实例:

public function do_upload(){
    $uploadDir = FCPATH."public\\remote_files\\";

    $uploadFile = $uploadDir . basename($_FILES[0]['name']);
    $handle    = fopen($uploadFile, "r");
    $data      = fread($handle, filesize($uploadFile));
    if (move_uploaded_file($_FILES[0]['tmp_name'], $uploadFile)){
       // set array to send data to remote server
        $remoteData = array(
            'fileName' => $_FILES[0]['name'],
            'fileData' => base64_encode($uploadFile)
        );

        // start curl set up for remote file upload
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, 'http://myserver.com/folder/Rest/upload_image');
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $remoteData);
        $response = curl_exec($curl);
        curl_close($curl);
        echo $response;   // set response to server.php file 
    } else {
        echo "Your file not uploaded to server.";
    }

}

这是我的服务器端代码:

public function upload_image(){
    header('Access-Control-Allow-Origin: *');

        if (isset($_POST['fileName']) && $_POST['fileData'])
        {
            // Save uploaded file
            $uploadDir = FCPATH."public/remote_files/";
            file_put_contents(
                $uploadDir. $_POST['fileName'],
                base64_decode($_POST['fileData'])
            );

            // Done
            echo "Success";
        }
    }

图像已上传到我的本地文件夹中,没有任何问题,我还可以看到某些内容正在远程文件夹中上传,但是当您查看图像时,它已损坏。

它说图像“ http://myserver.com/folder/public/remote_files/download.jpg”因为包含错误而无法显示。

有人可以帮我吗?

谢谢。

0 个答案:

没有答案