Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete file from second server via first server via php

I have two servers. I want delete file from second server via first server!

For example:

  • first-server.com
  • second-server.com

I have made two php files - file on first server and file on second server.

The file on first server contains

files.php

while($file = mysql_fetch_array($files){
echo $file['file_name'];
echo '<a href="second_server.com/delete.php?file=' . $file['file_name'] . '">Delete</a>';
}

the file on second server contains

delete.php

if($_GET['file']){
if(file_exists($_GET['file']){
unlink($_GET['file'];
//file deleted !
}

No it's ok , but. I want done this job without redirect me or visitor to the second server

For example : ajax or curl or something like that. What is the best way to do that?


Edit.

The codes above is just tests. It's not my real files. Please help in the way to process delete request without redirect to second server php file.

like image 949
Osama Tarek Avatar asked Feb 04 '26 09:02

Osama Tarek


2 Answers

I think a simple file_get_contents is enough:

File on first server:

$result = file_get_contents('second-sercer.com/delete.php?file=text.txt&some_security_token=asd');
//From $result you will know what was the result on the other server

File on second server (delete.php);

if($_GET['some_security_token'] == "asd"){
  if(file_exists($_GET['file']){
    if(unlink($_GET['file'])){
       //File deleted we are cool
       echo 1;
    } else {
       //File deletion failed
       echo 0;
    }
  }else{
    //File don't exists
    echo -1;
  }
}else{
 //bad token
 echo -2;
}

So this way your first server on script level goes to the second server so you can check parameters before that. And the second server sends back error / success codes so you can handle them on first server:

1  - success
0  - failed deletion
-1 - file doesn't even exists
-2 - bad security token

I do not include a way to create a token that both of the servers know. You can hash the file name with some key value for start, but you have to make it expensive to guess. I just try to point out that you need this kind of security too to make it even safer. And you have to find out a way to protect file system from deleting files that important for second-server. For example you only let the deletion of files in some folder only.

You could use cURl too the same way for this. But always try to return info for the first-server.com about the process on the second-server.com

like image 176
Ákos Nikházy Avatar answered Feb 05 '26 21:02

Ákos Nikházy


unset unsets a variable, it doesn't have anything to do with files.
You're looking for unlink.

BTW, you should do some serious validation on what you're going to unlink. Just blindly accepting anything in the URL can have serious consequences.

http://second_server.com/delete.php?file=delete.php
like image 27
deceze Avatar answered Feb 05 '26 21:02

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!