FTP upload file to remote server with CURL and PHP upload empty file

I am trying to upload a file to a remote server, but it looks like the original file does nothing. All I get is an empty file on the server. My code is:

<?php $c = curl_init(); $file = "\PATHTOFILE\file.txt"; $fp = fopen($file, "r"); curl_setopt($c, CURLOPT_URL, "SERVERPATH/file.txt"); curl_setopt($c, CURLOPT_USERPWD, "USER:PASSWORD"); curl_setopt($c, CURLOPT_UPLOAD, 1); curl_setopt($c, CURLOPT_INFILE, $fp); curl_setopt($c, CURLOPT_INFILESIZE, filesize($file)); curl_exec($c); echo "Success"; curl_close($c); fclose($fp); ?> 
+4
source share
1 answer

After 2 days, he hit his head on the keyboard. Finally I did it. Here's how:

 <?php if (isset($_POST['Submit'])) { if (!empty($_FILES['upload']['name'])) { $ch = curl_init(); $localfile = $_FILES['upload']['tmp_name']; $fp = fopen($localfile, 'r'); curl_setopt($ch, CURLOPT_URL, 'ftp://domain.com/'.$_FILES['upload']['name']); curl_setopt($ch, CURLOPT_USERPWD, "user:pass"); 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; ?> 

Here is the source where I found the solution

+17
source

Source: https://habr.com/ru/post/1403876/


All Articles