Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php] Dopisanie do pliku
Forum PHP.pl > Forum > Przedszkole
mateofi
Witam,

używam funkcji

fopen(PLIK, ARGUMENT);

Chcę zrobić, aby dopisało do początku pliku tekst.

jeżeli użyję argumentu "r+" to dopisuje do początku pliku, lecz zastępuje tekst który jest już w pliku.

czyli np.

jeśli w pliku aa.txt jest:
  1. Tekst w pliku txt.


i jeżeli dopisze wyraz: "AAAAAAAAAA" to w pliku jest:

  1. AAAAAAAAAAiku txt.


jak zrobić aby nie zastępywało??
strife
Dopisuje się do pliku, poprzez drugi argument a, a+. No, ale jak już kombinujesz i chcesz na początku to możesz ten plik odczytać, a następnie nową wartość zapisać na początku, wyglądało by to mniej więcej w taki sposób:

  1. <?php
  2. $file = file_get_contents( 'plik.txt' );
  3. $dopisz = 'Jakis tam tekst';
  4. file_put_contents( 'plik.txt', $dopisz . $file );
  5. ?>
mateofi
  1. <?php
  2. $file = file_get_contents( 'newsy.php' );
  3. $dopisz= $temat.'|||'.$nr.'|||'.$tresc.'|||'.$zrodlo .'|||n';
  4. file_put_contents( 'newsy.php', $dopisz . $file );
  5. ?>


nie działa ten kod.

wyskakuje mi błąd:

Fatal error: Call to undefined function: file_put_contents() in c:\apache\htdocs\wad\news\page\aaa.php on line 4
strife
Cytat(mateofi @ 1.07.2006, 19:27 ) *
Fatal error: Call to undefined function: file_put_contents() in c:\apache\htdocs\wad\news\page\aaa.php on line 4

Zapomniałem dodać, iż jest to opcja tylko w php5, spróbuj tak:

  1. <?php
  2. $file = file_get_contents( 'plik.txt' );
  3. $dopisz = 'Jakis tam tekst';
  4. $handle = fopen( 'plik.txt', "w+" );
  5. fwrite( $handle, $dopisz . $file );
  6. fclose( $handle );
  7. // file_put_contents( 'plik.txt', $dopisz . $file );
  8. ?>

Więcej w manualu winksmiley.jpg
NetJaro
Funkcja file_put_contents() w PHP4:

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | php Version 4 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2004 The php Group |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 3.0 of the php license,  |
  8. // | that is bundled with this package in the file LICENSE, and is |
  9. // | available at through the world-wide-web at  |
  10. // | <a href="http://www.php.net/license/3_0.txt" target="_blank">http://www.php.net/license/3_0.txt</a>. |
  11. // | If you did not receive a copy of the php license and are unable to  |
  12. // | obtain it through the world-wide-web, please send a note to |
  13. // | license@php.net so we can mail you a copy immediately.  |
  14. // +----------------------------------------------------------------------+
  15. // | Authors: Aidan Lister <aidan@php.net> |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: file_put_contents.php,v 1.25 2005/12/05 09:25:15 aidan Exp $
  19.  
  20.  
  21. if (!defined('FILE_USE_INCLUDE_PATH')) {
  22. define('FILE_USE_INCLUDE_PATH', 1);
  23. }
  24.  
  25. if (!defined('LOCK_EX')) {
  26. define('LOCK_EX', 2);
  27. }
  28.  
  29. if (!defined('FILE_APPEND')) {
  30. define('FILE_APPEND', 8);
  31. }
  32.  
  33.  
  34. /**
  35.  * Replace file_put_contents()
  36.  *
  37.  * @category php
  38.  * @package  PHP_Compat
  39.  * @link <a href="http://php.net/function.file_put_contents" target="_blank">http://php.net/function.file_put_contents</a>
  40.  * @author Aidan Lister <aidan@php.net>
  41.  * @version  $Revision: 1.25 $
  42.  * @internal resource_context is not supported
  43.  * @since  php 5
  44.  * @require  php 4.0.0 (user_error)
  45.  */
  46. if (!function_exists('file_put_contents')) {
  47. function file_put_contents($filename, $content, $flags = null, $resource_context = null)
  48. {
  49. // If $content is an array, convert it to a string
  50. if (is_array($content)) {
  51. $content = implode('', $content);
  52. }
  53.  
  54. // If we don't have a string, throw an error
  55. if (!is_scalar($content)) {
  56. user_error('file_put_contents() The 2nd parameter should be either a string or an array',
  57. E_USER_WARNING);
  58. return false;
  59. }
  60.  
  61. // Get the length of data to write
  62. $length = strlen($content);
  63.  
  64. // Check what mode we are using
  65. $mode = ($flags & FILE_APPEND) ?
  66. 'a' :
  67. 'wb';
  68.  
  69. // Check if we're using the include path
  70. $use_inc_path = ($flags & FILE_USE_INCLUDE_PATH) ?
  71. true :
  72. false;
  73.  
  74. // Open the file for writing
  75. if (($fh = @fopen($filename, $mode, $use_inc_path)) === false) {
  76. user_error('file_put_contents() failed to open stream: Permission denied',
  77. E_USER_WARNING);
  78. return false;
  79. }
  80.  
  81. // Attempt to get an exclusive lock
  82. $use_lock = ($flags & LOCK_EX) ? true : false ;
  83. if ($use_lock === true) {
  84. if (!flock($fh, LOCK_EX)) {
  85. return false;
  86. }
  87. }
  88.  
  89. // Write to the file
  90. $bytes = 0;
  91. if (($bytes = @fwrite($fh, $content)) === false) {
  92. $errormsg = sprintf('file_put_contents() Failed to write %d bytes to %s',
  93. $length,
  94. $filename);
  95. user_error($errormsg, E_USER_WARNING);
  96. return false;
  97. }
  98.  
  99. // Close the handle
  100. @fclose($fh);
  101.  
  102. // Check all the data was written
  103. if ($bytes != $length) {
  104. $errormsg = sprintf('file_put_contents() Only %d of %d bytes written, possibly out of free disk space.',
  105. $bytes,
  106. $length);
  107. user_error($errormsg, E_USER_WARNING);
  108. return false;
  109. }
  110.  
  111. // Return length
  112. return $bytes;
  113. }
  114. }
  115.  
  116. ?>


Biblioteka PHP_Compat - zbiór funkcji i klas niedostępnych w starszych wersjach php (poniżej PHP5) ale działających w tych wersjach.
mateofi
działa wersjas strife! Wielkie dzięki za pomoc smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.