unable to write a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

unable to write a file

Post by eshban »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am facing a problem in 'FILE WRITING' in PHP . I have just open the file, read its contents, replaces some string. But when  i write the file again, it will write the new string at the end of the file. I want to replace the new string with the old string at the same position. What can i do

Code: Select all

$filename = "test.html";
	$handle = fopen($filename, "a");
	$contents = fread($handle, filesize($filename));

		$pattern = '!<img src="([^"]+)"!';
		$replace = '<img src="x/y/z/$1"';
	
		str_replace($pattern, $replace, $file[1]);
		
		fwrite($handle,"$replace");
CODE FOR TEST.html

Code: Select all



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>


<table width="200" border="1">
  <tr>
    <td><img src="Water_lilies.jpg" width="100" height="100">cvcxvcxv</td>
    <td><img src="Winter.jpg" width="100" height="100"></td>
  </tr>
  <tr>
    <td>This is Row 3 </td>
    <td>This is Row 4 </td>
  </tr>
</table>



what can i do . please help


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Ind007
Forum Newbie
Posts: 14
Joined: Fri Dec 01, 2006 12:39 am
Location: India

Post by Ind007 »

try this

Code: Select all

$handle=fopen($filename,"R+")
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

str_replace will not take regular expression patterns. To use patterns, you must use preg_replace.
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Post by eshban »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Everything is ok, New write has been created successfully. but the new file has many spaces in it
like

[syntax="html"]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<html>


<head>


<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">


<title>Untitled Document</title>


</head>





<body>








<table width="200" border="1">


  <tr>


    <td><img src="c:/a/c:/a/Water_lilies.jpg" width="100" height="100">cvcxvcxv</td>


    <td><img src="c:/a/c:/a/Winter.jpg" width="100" height="100"></td>


  </tr>


  <tr>


    <td>This is Row 3 </td>


    <td>This is Row 4 </td>


  </tr>


</table>


I don't want these spaces. How can i do that.


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Replace the new line characters using regular expression.
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Please try this

Code: Select all

$contents = preg_replace ('/^[\t\n\r\f\v]+$/', '', $contents);
Not tested.
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Post by eshban »

Can you modify my regular expression to avoid free spaces appear in code. Here are my regular expression

Code: Select all

$pattern = '!<img src="([^"]+)"!';
                $replace = '<img src="x/y/z/$1"';
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Dear eshban,

I think preg_replace for removing the newline or tabs are not needed.

I tried the following and worked fine for me.

Code: Select all

<?php
$filename = "test.html";
$file_to_write = "test1.html";
$handle = fopen($filename, "r");
$handle2 = fopen($file_to_write, "w+");
$contents = fread($handle, filesize($filename)); 

$pattern = '!<img src="([^"]+)"!';
$replace = '<img src="x/y/z/$1"';

$text = preg_replace($pattern, $replace, $contents);
fwrite($handle2, $text); 

?>
Input file html :

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>


<table width="200" border="1">
  <tr>
    <td><img src="Water_lilies.jpg" width="100" height="100">cvcxvcxv</td>
    <td><img src="Winter.jpg" width="100" height="100"></td>
  </tr>
  <tr>
    <td>This is Row 3 </td>
    <td>This is Row 4 </td>
  </tr>
</table>
Output file html:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>


<table width="200" border="1">
  <tr>
    <td><img src="x/y/z/Water_lilies.jpg" width="100" height="100">cvcxvcxv</td>    <td><img src="x/y/z/Winter.jpg" width="100" height="100"></td>
  </tr>
  <tr>
    <td>This is Row 3 </td>
    <td>This is Row 4 </td>
  </tr>
</table>
Please try this code. Hope that is what you wanted.
Post Reply