cURL is a best library for file transfer via different types of protocols. cURL supports the transport via POST, GET, FTP upload and much more.
cURL is also able to authenticate on the destination server or website.
In this tutorial we want to upload a file to other (password protected) remote FTP server using a web form.
<?php
if (isset($_POST['Submit'])) {
if (!empty($_FILES['file']['name'])) {
$ch = curl_init();
$uploadedfile = $_FILES['file']['tmp_name'];
$fp = fopen($uploadedfile , 'r');
curl_setopt($ch, CURLOPT_URL, 'ftp://target_ftp_login:[email protected]/'.$_FILES['file']['name']);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($uploadedfile ));
curl_exec ($ch);
$error_msg = curl_errno($ch);
curl_close ($ch);
if ($error_msg == 0) {
$response = 'Yours File uploaded successfully.';
} else {
$response = 'Error into upload file.';
}
} else {
$response = 'Please select a file.';
}
echo $response ;
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
<div>
<label>Select file</label>
<input name="file" type="file" />
<input type="submit" name="Submit" value="Upload File" />
</div>
</form>
</body>
</html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php if (isset($_POST['Submit'])) { if (!empty($_FILES['file']['name'])) { $ch = curl_init(); $uploadedfile = $_FILES['file']['tmp_name']; $fp = fopen($uploadedfile , 'r'); curl_setopt($ch, CURLOPT_UPLOAD, 1); curl_setopt($ch, CURLOPT_INFILE, $fp); curl_setopt($ch, CURLOPT_INFILESIZE, filesize($uploadedfile )); curl_exec ($ch); $error_msg = curl_errno($ch); curl_close ($ch); if ($error_msg == 0) { $response = 'Yours File uploaded successfully.'; } else { $response = 'Error into upload file.'; } } else { $response = 'Please select a file.'; } echo $response ; } ?> <!DOCTYPE html> <html> <body> <form action="" method="post" enctype="multipart/form-data"> <div> <label>Select file</label> <input name="file" type="file" /> <input type="submit" name="Submit" value="Upload File" /> </div> </form> </body> </html> |