English 中文(简体)
SFTP from PHP - undefined constant CURLOPT_PROTOCOLS and CURLPROTO_SFTP?
原标题:
  • 时间:2009-11-19 21:51:13
  •  标签:
  • php
  • curl
  • sftp

From my php script, i need to be able to upload a csv file to a remote server via sftp. I followed the accepted answer from this question:

SFTP from within PHP

Here s what my code looks like

<?php
    error_reporting(E_ALL);
    ini_set( display_errors , 1);

    $ch = curl_init();
    $localfile =  export-3.csv ;
    $fp = fopen($localfile,  r );

    curl_setopt($ch, CURLOPT_URL,  sftp://user:pass@ftp.remote.com/ .$localfile);
    curl_setopt($ch, CURLOPT_UPLOAD, 1);
    curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
    curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
    curl_exec ($ch);

    $error_no = curl_errno($ch);
    curl_close ($ch);

    if ($error_no == 0) {
            $error =  File uploaded succesfully. ;
    } else {
            $error =  File upload error. ;
    }

    echo $error.   .$error_no;
?>

The output was

Notice: Use of undefined constant CURLOPT_PROTOCOLS - assumed  CURLOPT_PROTOCOLS  in /home/john/public_html/test/test.php on line 9

Notice: Use of undefined constant CURLPROTO_SFTP - assumed  CURLPROTO_SFTP  in /home/john/public_html/test/test.php on line 9
File upload error.1

I am using curl libcurl/7.18.2 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.10 . When I did a sudo apt-get install php5-curl, the computer said i had the most recent version.

What am I doing wrong? How do i sftp upload my files to a remote server from php?

最佳回答

First, your installed libcurl version doesn t seem to be new enough to have the options you try to use.

Then, it might also be so that your libcurl wasn t compiled to support SFTP when whoever built it. libcurl need to be built to use libssh2 for SCP and SFTP transfers to work.

问题回答

If you can t use CURL to do SFTP, you might be able to use this pure-PHP implementation of SFTP:

http://phpseclib.sourceforge.net/





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签