Search code examples
phpftp

PHP ftp_put does'nt copy local file to remote


I have some files in my local and I want to copy this files to remote using FTP connection. But I have error :

Warning: ftp_put(): Can't open that file: No such file or directory in C:\xampp\htdocs\samet\cmsdemo\FTPManager.php on line 59

codes :

<?php

//I defined ftp username password here but deleted for privacy

class FTPManager
{

    public function sendFilesToFtp($libraryPath, $customerName, $branch)
    {

       
            //.....rest of the code

            ftp_pasv($ftpConnection, true);

            $requiredFiles = scandir($libraryPath . "/" . $customerName . "/" . $branch);

            foreach ($requiredFiles as $file) {
                var_dump($file);
                if ($file !== '.' && $file !== '..') {
                    $localFilePath = $libraryPath . "/" . $customerName . "/" . $branch . '/' . $file;
                    $remoteFilePath = $remoteDirectory . '/' . $file;
                    if (ftp_put($ftpConnection, $remoteFilePath, $localFilePath, FTP_BINARY)) {
                        echo "File uploaded:" . $file;
                    } else {
                        echo "Error while uploading file: $file<br/>";
                    }
                }
            }

            ftp_close($ftpConnection);
        } catch (Exception $e) {
            echo 'Error: ' . $e->getMessage() . '<br/>';
        }
    }
}

?>

I have tried allowing files, setting permissions for ftp and other ways, but for some reason I still have this problem.

Note This application is currently running on a remote server. if I run the source code on my device, I copy my files via ftp connection. So it works in locale but not on the server

Detailed error :

remote : public_html/ds/gediz/Bornova/16.html

local : C:/cmslib/artsysdepo/gediz/Bornova/16.html

Warning: ftp_put(): Can't open that file: No such file or directory in C:\xampp\htdocs\samet\cmsdemo\FTPManager.php on line 59


Solution

  • I solved the problem by moving the ftp_pasv line to right after login. before doing any copying, reading, writing, nlist etc in ftp, it is necessary to put it in passive mode on many servers, i was already doing this but the location was wrong. fyi