How to upload a file from another server to another server using Curl?

回眸只為那壹抹淺笑 提交于 2019-12-11 02:25:34

问题


How to upload a file from another server to another server using Curl in Shell script and PHP?

I have a txt file is stored in the server where the Shell script stored and now, I want that txt file to be uploaded to the specific folder of another server. The 2 server have already connection. The data of txt file have a thousand rows.

Importing a file in the same server is ok.

PHP:

if (($handle = fopen($directory_root."filename.csv", "r")) !== FALSE){
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){
        $sql = "INSERT INTO tablename(col1,col2,col3,col4,col5)
                VALUES ('".mysql_escape_string($data[0])."',
                        '".mysql_escape_string($data[1])."',
                        '".mysql_escape_string($data[2])."',
                        '".mysql_escape_string($data[3])."',
                        '".mysql_escape_string($data[4])."')";
        $query = mysql_query($sql);
    }
    fclose($handle);
}

$ch = curl_init('http://example.com/');
curl_setopt_array($ch, array(
    CURLOPT_POSTFIELDS => array(
        'files[]' => '@/var/www/html/files/',
    ),
));
if (false === ($res = curl_exec($ch))){
    die("Upload failed: " . curl_error($ch));
}

回答1:


add curl_setopt($ch, CURLOPT_POST,1);

and '@/var/www/html/files/', is a directory ... must be file



来源:https://stackoverflow.com/questions/39505241/how-to-upload-a-file-from-another-server-to-another-server-using-curl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!