Ftp Upload File to Distant Server with Curl and PHP Uploads a Blank File

FTP upload file to distant server with CURL and PHP uploads a blank file

After 2 days of banging my head against the keyboard.. finally i did it..
Here is 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, from where i found the solution

CURL uploads blank file by FTP

Line 3, right now it says:

$localfile = $_FILES['upload']['tmp_name'];

Change it to:

$localfile = $_FILES['userfile']['tmp_name'];

Uploading via CURL - how can I change the destination file name?

get value for mime file using this code

<?php
function getmimefile($file){
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$ftype = finfo_file($finfo,$file);
finfo_close($finfo);
return $ftype;
}

so you can handle mimefile like this

$post["File1"] = new CurlFile($local_path, getmimefile($local_path), $actual_filename);

Getting a file from my local server and uploading it to a remote server using php

Try this:

    # Declare Files
$local_file = "/home/user/public_html/file.php";
$remote_file = "public_html/remote_file.php";

# FTP
$server = "ftp://ftp.yourhost.com/".$remote_file;

# FTP Credentials
$ftp_user = "ftp_username";
$ftp_password = "password";

# Upload File
$ch = curl_init();
$ftp_file = fopen($local_file, 'r');
curl_setopt($ch, CURLOPT_URL, $server);
curl_setopt($ch, CURLOPT_USERPWD, $ftp_user.":".$ftp_password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $ftp_file);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($local_file));
$result = curl_exec($ch);

You can easily put it in a function if this does what you want.

PHP cURL SFTP Not Working

The solution to this is to check your sub folders when connecting to a third party FTP site.

Using FileZilla, even though I didn't specify a default directory to go to (so I presumed the root), it automatically took me to a sub folder: /ebs_data/[my username]/.

When using cURL I was trying to upload to / (root), where I didn't have permission.

FileZilla = /ebs_data/[my username]/ (where I do have permissions)

cURL = / (root - where I don't have permissions)

...because FileZilla jumped me directly to the sub folder, and I missed it.



Related Topics



Leave a reply



Submit