By Sebastiaan de Jonge, published on Tuesday, January 22, 2013 at 07:30

This short snippet will demonstrate how you can upload a file onto a WebDav server using cURL and PHP.

Why cURL?

cURL is a command-line tool that will transfer data with URL syntax. It supports many different protocols such as HTTP, FTP, POP3, IMAP, LDAP and so on. Additionally it also supports many other features like uploading, authentication and proxies, making an ideal tool. And luckily, it also comes as a PHP module!

Uploading to WebDav

With cURL, not so hard as it may sound. WebDav is basically an extended HTTP protocol which allows stuff like PUT (file upload). In the following snippet I will demonstrate how to upload a single file. Inside the I've placed comments to explain my actions. And to make it a little more useful, I've added some authentication to it as well. 

// The user credentials I will use to login to the WebDav host
$credentials = array(
	'username',
	'password'
);
// Prepare the file we are going to upload
$filename = 'test.txt';
$filepath = '/path/to/my/local/file/'.$filename;
$filesize = filesize($filepath);
$fh = fopen($filepath, 'r');
// The URL where we will upload to, this should be the exact path where the file
// is going to be placed
$remoteUrl = 'https://webdav.myservice.com/path/to/upload/to/';
// Initialize cURL and set the options required for the upload. We use the remote
// path we specified together with the filename. This will be the result of the
// upload.
$ch = curl_init($remoteUrl . $filename);
// I'm setting each option individually so it's easier to debug them when
// something goes wrong. When your configuration is done and working well
// you can choose to use curl_setopt_array() instead.
// Set the authentication mode and login credentials
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, implode(':', $credentials));
// Define that we are going to upload a file, by setting CURLOPT_PUT we are
// forced to set CURLOPT_INFILE and CURLOPT_INFILESIZE as well.
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);
// Execute the request, upload the file
curl_exec($ch);
// Close the file handle
fclose($fh);

Comments

Fabrazer
Fabrazer - Monday, August 26, 2013 at 09:56

I had to remove line 27, but then it worked very well! Thanks! 🙂

Adriano Afonso
Adriano Afonso - Friday, June 19, 2015 at 13:17

Hello thereI'm trying to deploy your script on a project, but I need, not to upload a file that is already on server, but from an upload form. Can you help me here? Here is what I already had done:

/ FILE UPLOAD SCRIPT /

			if (isset($_FILES["img"])) {
			$file = $_FILES["img"];
			$file_name = $file["name"]; // pesca o nome do ficheiro
			$ext = pathinfo($file_name, PATHINFO_EXTENSION);
				// verifica se o ficheiro tem a extensão JPG ou PDF
				if ($ext!="jpg" && $ext!="pdf" && $ext!="JPG" && $ext!="PDF") {
					die('O ficheiro tem de estar em formato JPG ou PDF.');
				}
			$saveto_path_and_name = 'upload/'.$file_name; // Where you want to save the file '.$_GET['id_ot_ext'].'
			move_uploaded_file($file["tmp_name"], $saveto_path_and_name); //$file["tmp_name"],$file_name
			echo $saveto_path_and_name;
			/* TILL HERE I CAN PUT THE FILE IN THE WEB SERVER*/
			/* WEBDAV SCRIPT */
			// The user credentials I will use to login to the WebDav host
			$credentials = array(
					'username',
					'password'
			);
			// Prepare the file we are going to upload
			$filename = $file;
			$filepath = $saveto_path_and_name;
			$filesize = filesize($filepath); //$filepath
			$fh = fopen($file, 'r'); //
			// The URL where we will upload to, this should be the exact path where the file
			// is going to be placed
			$remoteUrl = 'https://myowncloudserver';
Denis
Denis - Wednesday, November 4, 2015 at 17:21

Thank You, It was so simple.Everything worked perfectly the first time.

Great!

Denis