Mariana Hernandez
Mariana Hernandez

Reputation: 1969

Upload a file to server via PHP

I create an .ics file on a PHP script on my website, now I am saving that file locally in my computer, but I would like to upload it to my iCal Exchange account so i can share it from there.

I can uploaded via iCal (the app) but I need to do it through PHP. In iCalExchange i have an username and password and the instruction say:

To publish a new calendar from iCal, select the "Publish on a Web server" option, and use one of the URLs:

http://icalx.com/private/zeroan/

http://icalx.com/public/zeroan/

Be sure to enter your new username and password correctly.

I tried this in PHP with no luck:

$ftp_server='74.91.122.152';//serverip
    $conn_id = ftp_connect($ftp_server); 


    // login with username and password
    $user="zeroan";
    $passwd="****";
    $login_result = ftp_login($conn_id, $user, $passwd); 

// check connection
   if ((!$conn_id) || (!$login_result)) { 
        echo "FTP connection has failed!";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
        die; 
    } else {
        echo "<br>Connected to $ftp_server, for user $user<br>";
    }
//directorylike /www.velibaba.com/images
  ftp_chdir($conn_id, "http://icalx.com/public/zeroan/");


//$destination_file=ftp_pwd($conn_id);


$source_file='cale.ics';
$destination_file="calendario.ics";
echo ("<br>");
print $destination_file;

echo ("<br>");

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 
        echo "FTP upload has failed!";
    } else {
        echo "Uploaded $source_file to $ftp_server as $destination_file";
    }

// close the FTP stream 
ftp_close($conn_id); 

Thanks

Upvotes: 0

Views: 224

Answers (1)

rernesto
rernesto

Reputation: 552

Full working example: My /var/www/stackoverflow/curluploader.php file

<?php 
$url = "http://localhost/stackoverflow/processupload.php";
$post_data['name'] = "Foo";
$post_data['file'] = '@'.__DIR__ . '/file.txt';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$response = curl_exec($ch);

echo $response;

Prefixing filepath with @ does the trick.

Now my /var/www/stackoverflow/processupload.php

<?php
echo "<pre>";
print_r($_POST);
print_r($_FILES);

If everthing it's okay you should something like:

Array
(
    [name] => Foo
)
Array
(
    [file] => Array
        (
            [name] => file.txt
            [type] => application/octet-stream
            [tmp_name] => /tmp/phpEBgYXy
            [error] => 0
            [size] => 30
        )

)

Exclude proccessupload authentication, but send user and password the same way you post field 'name', make a little auth on proccessupload to keep things safe. Be sure the apache user has write privileges on remote folder.

Upvotes: 1

Related Questions