I have been working on this for a couple of weeks now and yet again I am facing file issues. This sheet of code first gets all the entries existing in a database, then, after obtaining a series of ids, launches the same query with an additional WHERE
clause to search for specific entries belonging to customers.
Finally, compares both resulting arrays in search of any entry that exists in the db but has not been asigned to any customer to print them onto a csv file so the user launching the code can attend the issues preventing those entries from being linked to a customer.
I have been suffering some issues in the past regarding files and permissions, but this time no error appears, no message alerting me that I don't have the authority to open a file in the desired location or anything.
I tried inserting some var_dump()
along the sheet to see if the process really got to the fopen()
which it does. It simply skips it, no file is created and no data written.
I looked at the server logs but found nothing and I have errors and warnings enabled.
This would take all entries from the database.
$stmtOutgoing = $connAccounting->prepare("SELECT a.`zone`, b.`source_customer_billing_zone_id`, b.`source_external_subscriber_id`, count(*) AS llamadas, SUM((CEIL(b.`duration`))) AS duracion, SUM((b.`source_customer_cost`)/100) AS total, SUM((b.`source_customer_cost`)/100)/SUM((CEIL(b.`duration`))) AS precio
FROM `billing`.billing_zones a INNER JOIN cdr b
ON a.`id` = b.`source_customer_billing_zone_id`
WHERE source_provider_id = :reseller_id
GROUP BY b.`source_external_subscriber_id`, a.`zone`
ORDER BY total desc");
$stmtOutgoing->execute(array('reseller_id' => $reseller));
foreach ($stmtOutgoing as $row) {
array_push($totalEntries, $row);
echo "CDR Entry Added ";
}
After obtaining the ids, there would be a loop
to insert all of them inside this query:
foreach ($invoices as $invoice) {
if ($invoice['date'] >= $minDateSearchTstmp && !empty($invoice["customFields"]) && $invoice["customFields"][0]["field"] == "ID_EXTERNAL" && $invoice["customFields"][0]["value"] != "") {
$z = 0;
while ( isset($invoice['customFields'][$z]['field']) && $invoice['customFields'][$z]['field'] == 'ID_EXTERNAL' ) {
$source_external_subscriber_id = $invoice["customFields"][0]["value"];
renewConnection($connAccounting, $stmtOutgoing, $stmtIncoming);
require '../connections/accountingConnection.php';
/*Outgoing*/
$stmtOutgoing = $connAccounting->prepare("SELECT a.`zone`, b.`source_customer_billing_zone_id`, b.`source_external_subscriber_id`, count(*) AS llamadas, SUM((CEIL(b.`duration`))) AS duracion, SUM((b.`source_customer_cost`)/100) AS total, SUM((b.`source_customer_cost`)/100)/SUM((CEIL(b.`duration`))) AS precio
FROM `billing`.billing_zones a INNER JOIN cdr b
ON a.`id` = b.`source_customer_billing_zone_id`
WHERE source_external_subscriber_id = :source_external_subscriber_id AND source_provider_id = :reseller_id
GROUP BY b.`source_external_subscriber_id`, a.`zone`
ORDER BY total desc");
$stmtOutgoing->execute(array('source_external_subscriber_id' => $source_external_subscriber_id, 'reseller_id' => $reseller));
foreach ($stmtOutgoing as $row) {
array_push($holdedEntries, $row);
echo "Holded Entry Added ";
}
}
}
}
Finally, this code was supposed to generate the file and populate it with the info obtained:
foreach ($totalEntries as $tEntry) {
if (!in_array($tEntry, $holdedEntries)) {
array_push($unassignedEntries, $tEntry);
}
}
$fp = fopen('./unassigned_cdr_entries/entradasSinAsignar'.$fileDate.'.csv', 'w');
$delimiter = ',';
$enclosure = '"';
$escape_char = "\\";
foreach ($unassignedEntries as $unassignedEntry) {
var_dump($unassignedEntry);
fputcsv($fp, $unassignedEntry, $delimiter, $enclosure, $escape_char);
}
fclose($fp);
The original permissions for the files were these:
-rw-r--r-- 1 root root 152 Sep 4 13:26 'Resultados diferencias Cdr-Holded'
-rw-r--r-- 1 root root 6358 Sep 4 14:24 fetch_repeated_and_similar_ids.php
-rw-r--r-- 1 root root 3564 Sep 4 13:26 fetch_repeated_ids.php
-rw-r--r-- 1 root root 3733 Sep 4 13:26 fetch_similar_ids.php
-rw-r--r-- 1 root root 6057 Sep 4 13:26 holded-cdr_data_difference.php
drwxr-xr-x 2 root root 4096 Sep 1 16:57 repeated_ids
-rw-r--r-- 1 root root 14661 Sep 4 13:26 'unassignedCdrEntriesTest(Old).php'
drwxr-xr-x 2 root root 4096 Sep 7 10:04 unassigned_cdr_entries
-rw-r--r-- 1 root root 7616 Sep 7 16:23 unassigned_cdr_entries.php
-rw-r--r-- 1 root root 19613 Sep 4 13:26 unassigned_cdr_entries_for_two_months.php
I tried with chmod 766
and chmod 777
to no avail. As I said not even a warning telling me I can't open a file there due to insufficient permissions. You may also see the var_dump()
placed at the last foreach() loop
. The process gets there and the array is not empty as there are two test entries not assigned to anyone. Also as a recap, no other warning or error found using:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Any idea or suggestion would be much appreciated!
I forgot to post the solution I found as an answer. The issue here lied in the fact that everytime I overwrote the old files with new ones, for reasons that I don't know, the owner and permissions of the files changed, thus preventing apache user www-data
from creating files.
The prject folder was being sent via ssh, so whenever I had to update something I would just use chown
and chmod
to restore user and permissions of the folder/file affected to www-data
and 755
respectively.