最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

download - Another PHP ftp_get() Connecting but not downloading file - Stack Overflow

programmeradmin3浏览0评论

I can upload images to our website no problem, in particular a generated QR code. I use the code to follow an ordered product and match it to the printed personalised message that also has the same QR code. If both QR codes do not match, I either have the wrong product or wrong personalisation to add, has saved me many a time from shipping wrong item with wrong personalisation!

Currently use FILEZILLA to manually download all the generated QR codes to work PC so another process adds the .png image to the printed personalisation.

I thought I would create a simple PHP to automate this process and I have hit a total inexplicable error, it connects, I can see files in folder, I can change the CMOD to check I have all the permissions required but for the life of me it will not download the actual .png file to my PC (client), can anybody help explain why?

Site is on a Hostgator server Running PHP 7.4, is it not possible to download a .PNG image from Server to Client PC even though Filezilla can!

<?PHP


//ERROR REPORTING ON, NO ERRORS SHOW AT ALL
error_reporting(E_ERROR | E_WARNING | E_PARSE); 



    $qrcodigo1="3574975711.png"; // FILENAME I WANT TO TRANSFER

    $source_file = "$qrcodigo1";  //ASSIGN TO SOURCEFILE STRING.
    $destination_file = "C:\Users\*USER*\*MAIN-FOLDER-1*\*SUB-FOLDER-1*\*SKU-FOLDER1*\*PRODUCT-FOLDER1*\*PRODUCT-CODE-FOLDER"; // MY DESIRED HOME PC FILE LOCATION

    $ftp_server = "ftp.XXXXXXXXXX.co.uk"; //SERVER
    $ftp_user_name = "XXXXXX";        // USER
    $ftp_user_pass = "XXXXXXXXXXXXXXXX";  //PASSWORD

// FTP CONNECTION, WORKS AND CONNECTS
$ftp = ftp_connect($ftp_server); 
$login_result = ftp_login($ftp, $ftp_user_name, $ftp_user_pass); 
ftp_pasv($ftp, true); // SET PASSIVE TO AVOID ISSUES

if ((!$ftp) || (!$login_result)) {
    die("FTP connection has failed !");
}

// CHECK IS CONNECTED BY DUMPING DIRECTORY CONTENTS - WORKS FINE
ftp_chdir($ftp, "XXX/XXXXXXXX");
echo "Connected - Current directory is now:".ftp_pwd($ftp)."<br><br>";

$file_list = ftp_nlist($ftp, ".");
var_dump($file_list);
echo "<br><br>";


// CHECK CAN MODIFY FILES TO CONFIRM FILE PERMISSIONS - WORKS FINE
if (ftp_chmod($ftp, 0777, $source_file) !== false)
  {
  echo "Successfully chmoded $source_file to 777.<br><br>";
  }
else
  {
  echo "chmod failed.<br><br>";
  }


// DOWNLOAD FROM SERVER TO LOCAL PC, SAYS HAS DOWNLOADED, BUT NO FILE PRESENT?
// OUTPUT ON SCREEN SAYS "Successfully downloaded 3574975711.png to C:\Users\*USER*\*MAIN-FOLDER-1*\*SUB-FOLDER-1*\*SKU-FOLDER1*\*PRODUCT-FOLDER1*\*PRODUCT-CODE-FOLDER"
// WHERE IS FILE HEN? NO FILE IN FOLDER ??
    
    if
    (ftp_get($ftp, $destination_file, $source_file, FTP_BINARY))
    {
    echo "Successfully downloaded $source_file to $destination_file <br>";
    }
    else
    {
    echo "There was a problem while downloading $source_file <br>";
    }

    // CLOSE CONNECTION
    ftp_close($ftp);
?>

I can upload images to our website no problem, in particular a generated QR code. I use the code to follow an ordered product and match it to the printed personalised message that also has the same QR code. If both QR codes do not match, I either have the wrong product or wrong personalisation to add, has saved me many a time from shipping wrong item with wrong personalisation!

Currently use FILEZILLA to manually download all the generated QR codes to work PC so another process adds the .png image to the printed personalisation.

I thought I would create a simple PHP to automate this process and I have hit a total inexplicable error, it connects, I can see files in folder, I can change the CMOD to check I have all the permissions required but for the life of me it will not download the actual .png file to my PC (client), can anybody help explain why?

Site is on a Hostgator server Running PHP 7.4, is it not possible to download a .PNG image from Server to Client PC even though Filezilla can!

<?PHP


//ERROR REPORTING ON, NO ERRORS SHOW AT ALL
error_reporting(E_ERROR | E_WARNING | E_PARSE); 



    $qrcodigo1="3574975711.png"; // FILENAME I WANT TO TRANSFER

    $source_file = "$qrcodigo1";  //ASSIGN TO SOURCEFILE STRING.
    $destination_file = "C:\Users\*USER*\*MAIN-FOLDER-1*\*SUB-FOLDER-1*\*SKU-FOLDER1*\*PRODUCT-FOLDER1*\*PRODUCT-CODE-FOLDER"; // MY DESIRED HOME PC FILE LOCATION

    $ftp_server = "ftp.XXXXXXXXXX.co.uk"; //SERVER
    $ftp_user_name = "XXXXXX";        // USER
    $ftp_user_pass = "XXXXXXXXXXXXXXXX";  //PASSWORD

// FTP CONNECTION, WORKS AND CONNECTS
$ftp = ftp_connect($ftp_server); 
$login_result = ftp_login($ftp, $ftp_user_name, $ftp_user_pass); 
ftp_pasv($ftp, true); // SET PASSIVE TO AVOID ISSUES

if ((!$ftp) || (!$login_result)) {
    die("FTP connection has failed !");
}

// CHECK IS CONNECTED BY DUMPING DIRECTORY CONTENTS - WORKS FINE
ftp_chdir($ftp, "XXX/XXXXXXXX");
echo "Connected - Current directory is now:".ftp_pwd($ftp)."<br><br>";

$file_list = ftp_nlist($ftp, ".");
var_dump($file_list);
echo "<br><br>";


// CHECK CAN MODIFY FILES TO CONFIRM FILE PERMISSIONS - WORKS FINE
if (ftp_chmod($ftp, 0777, $source_file) !== false)
  {
  echo "Successfully chmoded $source_file to 777.<br><br>";
  }
else
  {
  echo "chmod failed.<br><br>";
  }


// DOWNLOAD FROM SERVER TO LOCAL PC, SAYS HAS DOWNLOADED, BUT NO FILE PRESENT?
// OUTPUT ON SCREEN SAYS "Successfully downloaded 3574975711.png to C:\Users\*USER*\*MAIN-FOLDER-1*\*SUB-FOLDER-1*\*SKU-FOLDER1*\*PRODUCT-FOLDER1*\*PRODUCT-CODE-FOLDER"
// WHERE IS FILE HEN? NO FILE IN FOLDER ??
    
    if
    (ftp_get($ftp, $destination_file, $source_file, FTP_BINARY))
    {
    echo "Successfully downloaded $source_file to $destination_file <br>";
    }
    else
    {
    echo "There was a problem while downloading $source_file <br>";
    }

    // CLOSE CONNECTION
    ftp_close($ftp);
?>
Share Improve this question edited Jan 19 at 15:21 jonrsharpe 122k30 gold badges266 silver badges473 bronze badges asked Jan 19 at 15:18 Sweets-n -StuffSweets-n -Stuff 91 bronze badge 1
  • Thanks to Martins help it appears that FTP is not the issue here. There is some other server/webhosting/file access issue at play. It may help others to know that if FTP is not working, it could actually be something entirely different! I shall continue to research and post back any effective solution. – Sweets-n -Stuff Commented Jan 30 at 15:48
Add a comment  | 

1 Answer 1

Reset to default 0

While it's not completely clear from your question, the *PRODUCT-CODE-FOLDER trailing part of the $destination_file suggests that it's a path to a destination folder, not a file (despite the variable name).

The ftp_get takes paths to files, not paths to folders, in both its arguments.

So this should do:

ftp_get($ftp, $destination_file . "\\" . $qrcodigo1, $source_file, FTP_BINARY)
发布评论

评论列表(0)

  1. 暂无评论