skip to Main Content

I am trying to detect status of invoice from a json file, then if the status is a confirmed payment, update the status and write the json to a new location, then unlink the existing json location.

<?php
  // get posted variables or die;
  if (isset($_POST['num'])) {
    $invoice = strip_tags($_POST['num']);
    $filename = $invoice.'.json';
  } else {
    die;
  }
  if (isset($_POST['status'])) {
    $status = strip_tags($_POST['status']);
  } else {
    die;
  }

  // get existing invoice 
  $content = file_get_contents('data/'.$invoice.'.json');
  $data = json_decode($content, true);

  // read json into variables
  $email =  $data['email'];
  $id = $data['id'];
  $addr = $data['tac_address'];
  $os = $data['os'];
  $exp = $data['experience'];
  $hosting = $data['type'];
  if (isset($data['telegram']) && $data['telegram'] != '') { $telegram = $data['telegram']; } else { $telegram = ''; }
  if (isset($data['linkedin']) && $data['linkedin'] != '') { $linkedin = $data['linkedin']; } else { $linkedin = ''; }
  if (isset($data['pay_status']) && $data['pay_status'] != '' && $data['pay_status'] == $status) { $status = $data['pay_status']; }
  $payment_addr = $data['bitcoin'];
  $payment_value = $data['value'];
  $payment =  substr($payment_value, 0, -4);

  // turn variables into json array
  $arr = array(
    'id' => $invoice,
    'email' => $email,
    'tac_address' => $addr,
    'os' => $os,
    'experience' => $exp,
    'type' => $hosting,
    'telegram' => $telegram,
    'linkedin' => $linkedin,
    'bitcoin' => $payment_addr,
    'value' => $payment_value,
    'pay_status' => $status
  );
  $json = json_encode($arr);

  // check status if paid save output to new location and delete old file
  if ($status == 'Confirmed Payment') {
    file_put_contents('paid_data/'.$filename, $json);
    unlink('data/'.$filename);
  }

The problem I am facing is that file_put_contents('paid_data/'.$filename, $json); ends up a file with a bunch of NULL variables. If I remove the unlink the variables save just fine, when I add it back the variables are all NULL.

So how can I verify file_put_contents takes place before the unlinking happens?

Also…. WHY does this happen? Isn’t php supposed to be linear and shouldn’t file_put_contents finish before the next line is carried out? Everything I have read about file_put_contents suggests as much. So why does the unlink take place before writing the content to a new location?

3

Answers


  1. Chosen as BEST ANSWER

    I still hope for a better answer, but so far this is my working solution to the problem. I changed the final if statement to the following. This seems to solve the issue - but there really has to be a better way than this. This feels very "hacky".

    if ($status == 'Confirmed Payment') {
      file_put_contents('paid_data/'.$filename, $json);
      $i = 0;
      while ($i < 1000) {
        $i++;
        if (file_exists('paid_data/'.$filename)) {
          unlink('data/'.$filename);
          break;
        }
      }
    }
    

  2. After mimicking your file structure and seeding a few examples, I was able to execute your code as is with the expected results. However, file_put_contents willreturn false on failure, so you might try something like this:

     if ($status == 'Confirmed Payment') {
      if(!file_put_contents('paid_data/'.$filename, $json);){
        print_r(error_get_last());
        die;
      }
      unlink('data/'.$filename);
    }
    
    Login or Signup to reply.
  3. Your code as originally written should be fine, as far as I can see. Usually when I see the kind of behavior you’re describing, the problem is that the script itself is being called twice (or more) and overlapping calls are manipulating the same file.

    I would definitely put in some debugging statements to verify this; I don’t know your environment, but a simple line written to a log will probably be enlightening.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search