Resolved:cURL and HTTP File Upload

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Resolved:cURL and HTTP File Upload

Post by thomas777neo »

Good Day

I am trying to upload a file from my local server to a remote Linux server. I managed to put this together after browsing the forum:

Code: Select all

$url = "http://mydomain/curl";
$localfile = "test.txt";
 
$fp = fopen ($localfile, "r");
$ch = curl_init();
 
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
 
$http_result = curl_exec($ch);
$error = curl_error($ch);
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE);
 
curl_close($ch);
 
fclose($fp);
 
print $http_code;
 
print "<br /><br />$http_result";
 
if ($error) 
{
   print "<br /><br />$error";
} // if ($error)
However I am getting the following message:

301 Moved Permanently

I am a bit lost as to why it doesn't upload the file and it returns this message. Any help would be appreciated.

EDIT: If I put a / in the url like this
It then returns this message: 405 Method Not Allowed

Still don't know what is causing the error? Perhaps a web server setting? Using Apache/2.0.54
Last edited by Benjamin on Tue Apr 28, 2009 1:03 pm, edited 3 times in total.
Reason: Changed code type from text to php.
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Re: cURL and HTTP File Upload

Post by user___ »

This should work

Code: Select all

<?php
 
error_reporting(E_ALL);
 
if (isset($_POST["Submit"]))
{
    if (!empty($_FILES["upload"]["name"]))
    {
        $ch = curl_init();
        $localfile = $_FILES["upload"]["name"];
        $fp = fopen($localfile, "r");
        curl_setopt($ch, CURLOPT_URL, "ftp://ftp_login:password@ftp.domain.com/".$_FILES["upload"]["name"]);
        curl_setopt($ch, CURLOPT_UPLOAD, 1);
        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.";
        }
    }
    else
    {
        $error = "Please select a file.";
    }
}
echo $error;
?>
Tell us whether it is fin e with you. It uses FTP. You can use one line ssh command. If you need it with ssh, ask.
Last edited by Benjamin on Tue Apr 28, 2009 1:03 pm, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Re: cURL and HTTP File Upload

Post by thomas777neo »

Thanks for the reply, unfortunately 90% of my clients are unable to access ftp. I would need to get it to work using http only.

I did notice however that if I change the url to http://mydomain/curl/get.php

It returns a code of 200, I have code in the get.php to check if any GET or POST vars are found, but none are posted.

Do the curl functions create the file on the remote server? Or do I have to rebuild the file on the other side.

Seems like I am missing something small somewhere.
user___
Forum Contributor
Posts: 297
Joined: Tue Dec 05, 2006 3:05 pm

Re: cURL and HTTP File Upload

Post by user___ »

If you need HTTP, why do you use CURL? Why do not you use a simple upload form and an upload script running on the remote server.
If you insist on you CURL, check this out http://www.codingforums.com/archive/ind ... 48555.html
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Re: cURL and HTTP File Upload

Post by thomas777neo »

I have an automated process, that is why I am not using an upload page. I had a look at the linked example, I did bump into that during my Google searches, however it is someone looking for help and the example doesn't work.

NOTE: I am able to post URL variables to the get.php and view them on the remote server

Code: Select all

curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
However I don't know how to save the file posted on the other side. Or that it is posted correctly
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Re: cURL and HTTP File Upload

Post by thomas777neo »

Here are the results of viewing the information:
  • url = http://<mydomain>/curl/get.php
    content_type = text/html; charset=ISO-8859-1
    http_code = 200
    header_size = 247
    request_size = 234
    filetime = -1
    ssl_verify_result = 0
    redirect_count = 0
    total_time = 0.12
    namelookup_time = 0.001
    connect_time = 0.041
    pretransfer_time = 0.041
    size_upload = 0
    size_download = 0
    speed_download = 0
    speed_upload = 0
    download_content_length = 0
    upload_content_length = 0
    starttransfer_time = 0.12
    redirect_time = 0
Post Reply