开发者

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.


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


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


<a href="/?file=$file_on_second_server">Delete file</a>
<?php if ($foo = $_GET['file']) {
echo "<img src=\"http://second_server.com/delete.php?file=$foo\" style=\"display:none;\"><script>alert('deleted');</script>"; }
?>


First of all, you want a security code or token or the like, so that unauthorised people do not delete files from your server.

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

and in first_server.com/delete.php, put this:

file_get_contents('second-server.com/delete.php?file=' . $_GET['file'] . '&securitycode=thisisasecuritycode');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜