Page 1 of 1

Replacement of Emails in files using php script

Posted: Mon Mar 22, 2010 5:25 am
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.

Re: Replacement of Emails in files using php script

Posted: Mon Mar 22, 2010 12:52 pm
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?

Re: Replacement of Emails in files using php script

Posted: Mon Mar 22, 2010 2:22 pm
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).

Re: Replacement of Emails in files using php script

Posted: Mon Mar 22, 2010 11:19 pm
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...

Re: Replacement of Emails in files using php script

Posted: Tue Mar 23, 2010 3:08 am
by Christopher
So which lines are 33 and 38 in the error messages?

Re: Replacement of Emails in files using php script

Posted: Tue Mar 23, 2010 7:00 am
by nagarajuv
here in the code the lines causing errors are 25,29 and 30.

Re: Replacement of Emails in files using php script

Posted: Tue Mar 23, 2010 12:03 pm
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.

Re: Replacement of Emails in files using php script

Posted: Fri Mar 26, 2010 1:22 am
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.