";
// Log in to FTP server
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);
if (!$login_result) {
ftp_close($conn_id);
$error = error_get_last();
die("Couldn't log in as $ftp_username. Error: " . $error['message']);
}
// Set passive mode (Try both true and false if one doesn't work)
ftp_pasv($conn_id, true);
// Change to the desired directory
$directory = "/" . $projectNamePlans; // Replace with your folder path
if (!ftp_chdir($conn_id, $directory)) {
echo "Failed to change directory to $directory";
ftp_close($conn_id);
exit;
}
// Retrieve directory listing
$files = ftp_nlist($conn_id, ".");
$pdfFiles = [];
// Check if listing was successful
if ($files === false) {
// echo "Failed to retrieve directory listing.";
} else {
//echo "PDF files in the directory " . $directory . ":
";
foreach ($files as $file) {
// Check if the file has a .pdf extension
if (pathinfo($file, PATHINFO_EXTENSION) === 'pdf') {
$pdfFiles[] = $file;
//$fileUrl = "download.php?file=" . urlencode($file);
// echo "$file
";
}
}
}
// Closing connection
ftp_close($conn_id);
// GET RECEIPTS
$conn_id = ftp_connect($ftp_server, $ftp_port); // Pass the port as the second parameter
if (!$conn_id) {
$error = error_get_last();
die("Couldn't connect to $ftp_server on port $ftp_port. Error: " . $error['message']);
}
//echo "Connected to FTP server.
";
// Log in to FTP server
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);
if (!$login_result) {
ftp_close($conn_id);
$error = error_get_last();
die("Couldn't log in as $ftp_username. Error: " . $error['message']);
}
// Set passive mode (Try both true and false if one doesn't work)
ftp_pasv($conn_id, true);
// Change to the desired directory
$directory = "/" . $projectNamePlans . "/Receipts"; // Replace with your folder path
if (!ftp_chdir($conn_id, $directory)) {
echo "Failed to change directory to $directory";
ftp_close($conn_id);
exit;
}
// Retrieve directory listing
$files = ftp_nlist($conn_id, ".");
$ReceiptspdfFiles = [];
// Check if listing was successful
if ($files === false) {
// echo "Failed to retrieve directory listing.";
} else {
//echo "PDF files in the directory " . $directory . ":
";
foreach ($files as $file) {
// Check if the file has a .pdf extension
if (pathinfo($file, PATHINFO_EXTENSION) === 'pdf') {
if (!$file == ""){
$ReceiptspdfFiles[] = $file;
}
//$fileUrl = "download.php?file=" . urlencode($file);
// echo "$file
";
}
}
}
// Closing connection
ftp_close($conn_id);
$response = [
'successfull' => true,
'approvedfiles' => $pdfFiles,
'submissionreceipts' => $ReceiptspdfFiles
];
// Encode the response array into JSON
echo json_encode($response);
} else {
$successfull = "Not Successfull";
echo json_encode(['successfull' => $successfull]);
}
}
?>