There is a PHP Pear Package called HTTP_WebDAV_Client which allows PHP to access a WebDav disk (like an iDisk!)
The following is the typical example posted to several forums which does NOT work to upload a file:
$local_path = 'local/filepath';
$remote_path = "webdavs://idisk.me.com/username/path/tofile";
$mode = 'w';
$options = array();
$opend_path = array();
$webdav_client = new HTTP_WebDAV_Client_Stream();
$status = $webdav_client->stream_open( $remote_path, $mode, $options, $opend_path );
if ( $status === false )
{
error_log( 'stream_open failed' );
exit;
}
$handle = fopen( $local_path, 'rb' );
$contents = fread( $handle, filesize( $local_path ) );
fclose( $handle );
$status = $webdav_client->stream_write( $contents );
if ( $status === false ) {
error_log( 'stream_write failed' );
exit;
}
$webdav_client->stream_close();
This will fail every time. Firstly there is no way within the package to set the username and password which are essential to connect to a disk with authentication. Secondly the stream_open function attempts to check WebDav options on the (not yet existent) file you wish to upload. While it IS typically bad manners to simply run on into a package and edit stuff willy nilly it is sort of necessary in this case as the author has dropped off the face of the planet, the package is not fully functional, and no one has picked up the project. (No I don’t plan to pick it up either I have enough to do thanks).
Thus to get it working I did 3 things:
1. I added a ‘set_user’ function to the Stream.php found within the package. You should be able with very very basic PHP skills figure out how to do this.
2. I added a ‘set_password’ function to the Stream.php found within the package. You should be able with very very basic PHP skills figure out how to do this.
3. I commented out ‘if (!$this->_check_options()) return false;’ in the stream_open function.
The fixed client code is thus:
$local_path = 'local/filepath';
$remote_path = "webdavs://idisk.me.com/username/path/tofile";
$mode = 'w';
$options = array();
$opend_path = array();
$webdav_client = new HTTP_WebDAV_Client_Stream();
$webdav_client->set_user('username');
$webdav_client->set_password('password');
$status = $webdav_client->stream_open( $remote_path, $mode, $options, $opend_path );
if ( $status === false )
{
error_log( 'stream_open failed' );
exit;
}
$handle = fopen( $local_path, 'rb' );
$contents = fread( $handle, filesize( $local_path ) );
fclose( $handle );
$status = $webdav_client->stream_write( $contents );
if ( $status === false ) {
error_log( 'stream_write failed' );
exit;
}
$webdav_client->stream_close();
This should work great. Feel free to leave a comment or shoot me an email if this solution doesn’t do the trick for you!