Replacement of Emails in files using php script

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
nagarajuv
Forum Newbie
Posts: 6
Joined: Fri Mar 12, 2010 8:14 am
Location: Hyderabad,India
Contact:

Replacement of Emails in files using php script

Post by nagarajuv »

Hi,

I have the below peace of php script to replace Email ids in file by using regular expression, while executing this code I am getting an error:

[Mon Mar 22 06:17:50 2010] [error] [client 127.0.0.1] PHP Warning: fwrite(): supplied argument is not a valid stream resource in /var/www/html/raju/sourcefiles/replace_email.php on line 33
[Mon Mar 22 06:17:50 2010] [error] [client 127.0.0.1] PHP Warning: fwrite(): supplied argument is not a valid stream resource in /var/www/html/raju/sourcefiles/replace_email.php on line 33
[Mon Mar 22 06:17:50 2010] [error] [client 127.0.0.1] PHP Warning: fwrite(): supplied argument is not a valid stream resource in /var/www/html/raju/sourcefiles/replace_email.php on line 33
[Mon Mar 22 06:17:50 2010] [error] [client 127.0.0.1] PHP Warning: fclose(): supplied argument is not a valid stream resource in /var/www/html/raju/sourcefiles/replace_email.php on line 38

code as follows:

Code: Select all

<?php 
$srcFile = $_REQUEST['filename'];
$destFile = $_REQUEST['filename'];
 
$destFolder = "DestFiles";
$destPath = "$destFolder/$destFile";
 
if(file_exists($destFolder)){
echo $dest_handle = fopen($destPath, 'w');  
}
else{  
mkdir($destFolder);
chmod($destFolder,0777);
echo $dest_handle = fopen($destPath, 'w');
}
 
$src_handle = fopen($srcFile, 'r');  
$dest_handle = fopen($destFile, 'w');  
 
if($_REQUEST['filename']!=""){
    while (!feof($src_handle)) 
    { 
    $data = fgets($src_handle, 512); 
    $newdata = preg_replace('/[a-zA-Z0-9_.-]+@[a-zA-Z0-9.-]+.[a-zA-Z.]{2,5}/', 'nagarajuv@gmail.com', $data);  
    fwrite($dest_handle,$newdata);  
    } 
}
 
fclose($src_handle); 
fclose($dest_handle);
?>
i am using CentOs operating system.
fwrite() function writing zero bytes to the newly created file, i am unable to figure out where is going wrong.

Any help plz

thanks..
Raju.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Replacement of Emails in files using php script

Post by Christopher »

The code you posted only has 31 lines, but the error messages are for lines 33 and 38? What code is causing the error?
(#10850)
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: Replacement of Emails in files using php script

Post by ell0bo »

I'm going to assume it's the fact that you go through all the trouble setting up $destFolder, but completely throw it out when you create the file handler on line 18. Odds are you don't have write privileges to the web directory (which is a good thing), so it's going to fail without $destFolder before your file name, because it's going to try writing to your PWD (present working directory).
nagarajuv
Forum Newbie
Posts: 6
Joined: Fri Mar 12, 2010 8:14 am
Location: Hyderabad,India
Contact:

Re: Replacement of Emails in files using php script

Post by nagarajuv »

I replaced extra spaces in the code that's why the lines not appear in the code.I have changed chmod of $destFolder to 777 and I tried as a root also. Then also its not working fine.


can anyone help me in this issue...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Replacement of Emails in files using php script

Post by Christopher »

So which lines are 33 and 38 in the error messages?
(#10850)
nagarajuv
Forum Newbie
Posts: 6
Joined: Fri Mar 12, 2010 8:14 am
Location: Hyderabad,India
Contact:

Re: Replacement of Emails in files using php script

Post by nagarajuv »

here in the code the lines causing errors are 25,29 and 30.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Replacement of Emails in files using php script

Post by Christopher »

So according to the error messages, $src_handle and $dest_handle do not contain a "valid stream resource". That means your fopen()'s are not working. Again, the problem is either the file path is wrong or permissions are wrong. Remember that the PHP script runs as the user that the webserver runs as -- not always as the user you login as.
(#10850)
nagarajuv
Forum Newbie
Posts: 6
Joined: Fri Mar 12, 2010 8:14 am
Location: Hyderabad,India
Contact:

Re: Replacement of Emails in files using php script

Post by nagarajuv »

Hi,

I changed permissions to 0777 using chmod and I logged in as a root even though the same problem occurs.Do you any php scripts to replace emails in files please post the script.Because I am much need of it.



thanks.,
Raju.
Post Reply