function tripal_file_upload_put

3.x tripal.upload.inc tripal_file_upload_put($filename, $chunk, $user_dir)

Saves the contents of the file being sent to the server.

The file is saved using the filename the chunk number as an extension. This function uses file locking to prevent two jobs from writing to the same file at the same time.

1 call to tripal_file_upload_put()

File

tripal/includes/tripal.upload.inc, line 271

Code

function tripal_file_upload_put($filename, $chunk, $user_dir) {

  // Get the HTTP PUT data.
  $putdata = fopen("php://input", "r");
  $size = $_SERVER['CONTENT_LENGTH'];

  // Store the chunked file in a temp folder.
  $temp_dir = $user_dir . '/temp/' . $filename;
  if (!file_exists($temp_dir)) {
    mkdir($temp_dir, 0700, TRUE);
  }

  // Open the file for writing if doesn't already exist with the proper size.
  $chunk_file = $temp_dir . '/' . $filename . '.chunk.' . $chunk;
  if (!file_exists($chunk_file) or filesize($chunk_file) != $size) {
    // Read the data 1 KB at a time and write to the file
    $fh = fopen($chunk_file, "w");
    // Lock the file for writing. We don't want two different
    // processes trying to write to the same file at the same time.
    if (flock($fh, LOCK_EX)) {
      while ($data = fread($putdata, 1024)) {
        fwrite($fh, $data);
      }
      flock($fh, LOCK_UN);
      fclose($fh);
    }
  }
  fclose($putdata);

  // Get the current log, updated and re-write it.
  $log = tripal_file_upload_read_log($temp_dir);
  $log['chunks_written'][$chunk] = $size;
  tripal_file_upoad_write_log($temp_dir, $log);
}