How to Write Csv To Database table And Upload On Ftp Server In PHP

$ftpUrl = ‘Your Host’;

$ftpUser = ‘Your FTP Username’;

$ftpPassword = ‘Your FTP Password’;
try
 {
    $conn_id = ftp_connect($ftpUrl);
        if (false === $conn_id)
        {
          throw new Exception(‘Unable to connect’);
        }
        if (@ftp_login($conn_id$ftpUser$ftpPassword))
        {
            $dir = ‘Your FilePath/test.csv’;
            $remote_file = basename($dir);
            $myfile = fopen($dir“w”) or die(“Unable to open file!”);
            $title = “ID,Type,Name”;
            fwrite($myfile$title);
            $result = $mysqli -> query(SELECT * From Your_Table_name);
            while($row = mysqli_fetch_array($result))
            {
                $display =‘”‘.$row[ID].‘”,”‘
                .$row[Type].‘”,”‘
                .$row[Name].‘”‘.\n;
                fwrite($myfile,$display);
            }
            $handle = fopen($dir‘r’);
            ftp_pasv($conn_idtrue);
            if (ftp_fput($conn_id$remote_file$handle, FTP_ASCII))
            {
               echo “SuccesFully”;
            }
            else
            {
                echo “Upload Not Upload”;
             }
            fclose($handle);
             fclose($myfile);
            ftp_close($conn_id);
        }
 }
  catch  (Exception $e)
  {
      echo “Failure: “$e->getMessage();
   }

Leave a Reply

Your email address will not be published. Required fields are marked *