Skip to content
WJunction - Webmaster Forum

Need a programmer

Status
Not open for further replies.
Hello guys,

Maybe someone can help me...
I have thousands of files to edit in my server and this will consume me lots of hours editing all files manually one by one.
So, I had an idea but I don't know if is possible to make... to create a tools to edit all my files automatically.

This is the tree view of directory/files of my website:
Code:
directory3
directory3/file.php
...
directory4
directory4/file.php
...
links
link/sitename.php
...

This is the content of /directory3/ and /directory4/ files:
Code:
<?php
echo '<a rel="nofollow" href="http://www.domain.com/link/sitenameA.php" target="_blank"><span>Site Name</span></a>
<a rel="nofollow" href="http://www.domain.com/link/sitenameB.php" target="_blank"><span>Site Name</span></a>
<a rel="nofollow" href="http://www.domain.com/link/sitenameC.php" target="_blank"><span>Site Name</span></a>';
?>

This is the content of all files inside the links directory:
Example: http://www.domain.com/link/sitenameA.php
Code:
<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://affdomain.com/?id=code" );
?>

What I need to change is the URL inside of the href="" of all /directory3/ and /directory4/ files.

From:
Code:
<a rel="nofollow" href="http://www.domain.com/link/sitenameA.php" target="_blank"><span>Site Name</span></a>

To:
Code:
<a rel="nofollow" href="http://affdomain.com/?id=code" target="_blank"><span>Site Name</span></a>

....................

So, is possible to create some kind of script that grabs the Location of the link/sitenameA.php file:
Code:
http://affdomain.com/?id=code

And replace with the URL inside of href="" of all /directory3/ and /directory4/ files?
Code:
http://www.domain.com/link/sitenameA.php

Sorry if the post is a little confuse but is a little difficult to explain.
But if you didnt understood, please let me know and I will try to explain better.

Waiting for your replies, thanks! :-)
 

5 comments

Make sure to backup your folder, I have not tested below code but it should work, change your files path and values in code:
PHP:
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/path/to/your/folder')) as $file)        
{            
 $file_contents = file_get_contents($file);            
 $fh = fopen($file, "w");            
 $file_contents = str_replace('http://www.domain.com/link/sitenameA.php','http://affdomain.com/?id=code',$file_contents);           
  fwrite($fh, $file_contents);           
  fclose($fh);               
  echo "$file updated\n";        
}
 
Last edited:
Hi mRAza, thank you very much for your reply and for your time helping me :)

But I think that this will not work for me, like the way that I want.

This because I have thousands of this files:
Code:
http://www.domain.com/link/sitenameA.php
http://www.domain.com/link/sitenameB.php
http://www.domain.com/link/sitenameC.php
http://www.domain.com/link/sitenameD.php
http://www.domain.com/link/sitenameE.php
http://www.domain.com/link/sitenameF.php
etc...

And have hundreds of this files:
Code:
http://www.domain.com/directory3/fileA.php
http://www.domain.com/directory3/fileB.php
http://www.domain.com/directory3/fileC.php
http://www.domain.com/directory3/fileD.php
http://www.domain.com/directory3/fileE.php
http://www.domain.com/directory3/fileF.php
etc...

So, I think that If I use your code, I still need to edit all of them one by one right?
Because each file /link/sitenameX.php have a different URL.

That's why I asked:

Is possible to create some kind of script that grabs the Location of the link/sitenameA.php file:
Code:
http://affdomain.com/?id=code

And replace with the URL inside of href="" of all /directory3/ files?
Code:
http://www.domain.com/link/sitenameA.php

What I need to change is the URL inside of the href="" of all /directory3/ files.

From:
Code:
<a rel="nofollow" href="http://www.domain.com/link/sitenameA.php" target="_blank"><span>Site Name</span></a>

To:
Code:
<a rel="nofollow" href="http://affdomain.com/?id=code" target="_blank"><span>Site Name</span></a>

Hope you understood my idea :-)
Let me know if you have any questions and If this is possible to do.

Thank you buddy! :)
 
Above script will do same thing as you are asking. Change '/path/to/your/folder'' to your directory3 folder full location , like if your folder is on linux in /var/www folder then '/var/www/directory3'. it will capture all files within directory3 folder and sub-folders and replace given word in all files. if your files contain exact href tags html code you can do something like this

PHP:
$file_contents = str_replace('href="http://www.domain.com/link/sitenameA.php"','href="http://affdomain.com/?id=code"',$file_contents);

or if you want to change all that url everywhere just use code without href tags. for more complex you can check at preg_replace function.
 
Hi buddy, thank you again for your reply.


But I think that you didn't understood my idea, or, I didn't understood yours :-)


So here is what I think that you're saying:
- I need to create a new php file with the code that you said above
- When I run the code, the old URL will automatically replaced with the new one.
- I need to edit the new php file thousands of times to replace all lines that I want since I have thousands of files with different URLS.


Is this correct?


My idea is to use an tool that automatically grabs the correct URL of each file and replace them.


Something like:
Code:
<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: %" );
?>

Code:
<a rel="nofollow" href="%" target="_blank"><span>Site Name</span></a>
 
- I need to create a new php file with the code that you said above
- When I run the code, the old URL will automatically replaced with the new one.
Yes
- I need to edit the new php file thousands of times to replace all lines that I want since I have thousands of files with different URLS.
No, You dont need to edit php file thousands of times, above script will change URL in all files at once. Just set the path to your folder in RecursiveDirectoryIterator i.e " /var/www/directory3"

 
Status
Not open for further replies.

About the author

D
Active Member · Joined
806
Messages
18
Reactions
18
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom