WPDKFilesystem::append PHP Méthode

append() public static méthode

Append data to a file and return TRUE on successfully, FALSE otherwise.
Since: 1.7.0
public static append ( string $data, string $filename ) : boolean
$data string Data to append.
$filename string Complete path filename.
Résultat boolean
    public static function append($data, $filename)
    {
        // Check if filename is writable
        if (is_writable($filename) || !file_exists($filename)) {
            if (!($handle = @fopen($filename, 'a'))) {
                return false;
            }
            if (!fwrite($handle, $data)) {
                return false;
            }
            fclose($handle);
            return true;
        }
    }

Usage Example

Exemple #1
0
 /**
  * Reads the Database table in $table and creates SQL Statements for recreating structure and data then return the
  * DUMP SQL. If you set the $filename params then the dump is store on filesystem too.
  *
  * Taken partially from phpMyAdmin and partially from Alain Wolf, Zurich - Switzerland
  *
  * Website: http://restkultur.ch/personal/wolf/scripts/db_backup/
  *
  * @brief Dump
  *
  * @param string $table    Table name.
  * @param string $filename Optional. Complete path of a filename where store the dump.
  *
  * @return string
  */
 public function dumpWithTable($table, $filename = '')
 {
     // Prepare dump
     $dump = '';
     // Main information on dump
     $dump .= "# --------------------------------------------------------\n";
     $dump .= "# Date:      " . date('j F, Y H:i:s') . "\n";
     $dump .= "# Database:  " . DB_NAME . "\n";
     $dump .= "# Table:     " . $table . "\n";
     $dump .= "# --------------------------------------------------------\n";
     // Add SQL statement to drop existing table
     $dump .= "\n";
     $dump .= "#\n";
     $dump .= "# Delete any existing table `{$table}`\n";
     $dump .= "#\n";
     $dump .= "\n";
     $dump .= "DROP TABLE IF EXISTS " . self::backquote($table) . ";\n";
     // Comment in SQL-file
     $dump .= "\n";
     $dump .= "#\n";
     $dump .= "# Table structure of table `{$table}`\n";
     $dump .= "#\n";
     $dump .= "\n";
     // Get table structure
     $query = 'SHOW CREATE TABLE ' . self::backquote($table);
     $result = $this->mysqli ? mysqli_query($this->dbh, $query) : mysql_query($query, $this->dbh);
     if ($result) {
         // Get num rows
         $num_rows = $this->mysqli ? mysqli_num_rows($result) : mysql_num_rows($result);
         if ($num_rows > 0) {
             $sql_create_arr = $this->mysqli ? mysqli_fetch_array($result) : mysql_fetch_array($result);
             $dump .= $sql_create_arr[1];
         }
         if ($this->mysqli) {
             mysqli_free_result($result);
         } else {
             mysql_free_result($result);
         }
         $dump .= ' ;';
     }
     // Get table contents
     $query = 'SELECT * FROM ' . self::backquote($table);
     /**
      * Filter the query used to select the rows to dump.
      *
      * The dynamic portion of the hook name, $table, refers to the database table name.
      *
      * @param string $query The SQL query.
      */
     $query = apply_filters('wpdk_db_dump_query-' . $table, $query);
     $result = $this->mysqli ? mysqli_query($this->dbh, $query) : mysql_query($query, $this->dbh);
     $fields_cnt = 0;
     $rows_cnt = 0;
     if ($result) {
         $fields_cnt = $this->mysqli ? mysqli_num_fields($result) : mysql_num_fields($result);
         $rows_cnt = $this->mysqli ? mysqli_num_rows($result) : mysql_num_rows($result);
     }
     // Comment in SQL-file
     $dump .= "\n";
     $dump .= "\n";
     $dump .= "#\n";
     $dump .= "# Data contents of table `{$table}` ({$rows_cnt} records)\n";
     $dump .= "#\n";
     $dump .= "\n";
     // Lock table if insert available
     $dump .= empty($rows_cnt) ? '' : "\nLOCK TABLES `{$table}` WRITE;\n";
     /**
      * Filter the addition SQL comment before printing the INSERT rows.
      *
      * The dynamic portion of the hook name, $table, refers to the database table name.
      *
      * @param string $comment Default empty.
      */
     $dump .= apply_filters('wpdk_db_dump_info_before_inserts-' . $table, '');
     // Checks whether the field is an integer or not
     for ($j = 0; $j < $fields_cnt; $j++) {
         if ($this->mysqli) {
             $object = mysqli_fetch_field_direct($result, $j);
             $field_set[$j] = $object->name;
             $type = $object->type;
         } else {
             $field_set[$j] = self::backquote(mysql_field_name($result, $j));
             $type = mysql_field_type($result, $j);
         }
         // Is number?
         $field_num[$j] = in_array($type, array('tinyint', 'smallint', 'mediumint', 'int', 'bigint'));
     }
     // Sets the scheme
     $entries = 'INSERT INTO ' . self::backquote($table) . ' VALUES (';
     $search = array('\\x00', '\\x0a', '\\x0d', '\\x1a');
     //\x08\\x09, not required
     $replace = array('\\0', '\\n', '\\r', '\\Z');
     $current_row = 0;
     $batch_write = 0;
     while ($row = $this->mysqli ? mysqli_fetch_row($result) : mysql_fetch_row($result)) {
         $current_row++;
         // build the statement
         for ($j = 0; $j < $fields_cnt; $j++) {
             if (!isset($row[$j])) {
                 $values[] = 'NULL';
             } elseif ($row[$j] === '0' || $row[$j] !== '') {
                 // a number
                 if ($field_num[$j]) {
                     $values[] = $row[$j];
                 } else {
                     $values[] = "'" . str_replace($search, $replace, self::addslashes($row[$j])) . "'";
                 }
             } else {
                 $values[] = "''";
             }
         }
         $dump .= "\n" . $entries . implode(', ', $values) . ") ;";
         // write the rows in batches of 100
         if ($batch_write === self::DUMP_SQL_FILE_CHUNCK_SIZE) {
             $batch_write = 0;
             // Write on disk
             if (!empty($filename)) {
                 $result = WPDKFilesystem::append($dump, $filename);
                 // TODO Fires an error or filters to stop the execution
                 $dump = '';
             }
         }
         $batch_write++;
         unset($values);
     }
     if ($this->mysqli) {
         mysqli_free_result($result);
     } else {
         mysql_free_result($result);
     }
     // Unlock tables
     $dump .= empty($rows_cnt) ? '' : "\n\nUNLOCK TABLES;\n";
     // Create footer/closing comment in SQL-file
     $dump .= "\n";
     $dump .= "\n";
     $dump .= "#\n";
     $dump .= "# End of data contents of table " . $table . "\n";
     $dump .= "# --------------------------------------------------------\n";
     $dump .= "\n";
     $dump .= "\n";
     // Write on disk
     if (!empty($filename)) {
         $result = WPDKFilesystem::append($dump, $filename);
         // TODO Fires an error
     }
     return $dump;
 }