servercheck redirect
ik zit een beetje te puzzelen.
ik heb dit script, waarbij ik standaard wil kijken of in dit geval poort 200 van google openstaat en reageert.
wanneer deze online is zou die naar site1 moeten gaan op poort 80
is google offline in dit geval op poort 200, dan zou die naar site 2 moeten doorlinken op poort 80.
echter dat wil niet zo...
(oja google heb ik maar even als naam gebruikt net als de poortnummers.)
Waar komt $result vandaan op regel 3?
http://nl3.php.net/manual/en/function.exec.php
return_var
If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.
return_var
If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.
tnx voor de info.
Code (php)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
<?php
if(@fsockopen('www.google.com', 8080, $errno, $errstr, 1)) {
//echo 'Online';
header('Location: http://www.site1.nl/'); /* Stuur de browser naar www.site1.nl als de gecontroleerde server online is*/
} else {
//echo 'Offline';
header('Location: http://www.phphulp.nl/'); /* Stuur de browser naar www.site2.nl als de gecontroleerde server offline is*/
}
?>
if(@fsockopen('www.google.com', 8080, $errno, $errstr, 1)) {
//echo 'Online';
header('Location: http://www.site1.nl/'); /* Stuur de browser naar www.site1.nl als de gecontroleerde server online is*/
} else {
//echo 'Offline';
header('Location: http://www.phphulp.nl/'); /* Stuur de browser naar www.site2.nl als de gecontroleerde server offline is*/
}
?>
Toevoeging op 03/03/2012 16:16:47:
Hey mensen.
Ik doe het toch eens proberen.
hier heb ik een upload script die checkt of een upload server online is.
daarbij kun je vele dingen checken van wat je wil uploaden.
wanneer die niet online is zal die kiezen voor een backup server na keuze.
Ik weet het is geen top10 script, maar heb mn best erop gedaan en ben er best blij mee..
ik heb het zelf niet geheel geschreven , alleen maar samengevoegd en aanpassingen gedaan.
Ik begin met de index.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<head>
<meta http-equiv="Content-Language" content="nl">
<style type="text/css">
.style1 {
text-align: center;
}
</style>
</head>
<div class="style1">
<br>
<br>
<iframe name="I1" id="I1" src="servertest.php" style="width: 578px; height: 359px" scrolling="no" border="0" frameborder="0">
<br>
</iframe><br>
</div>
<meta http-equiv="Content-Language" content="nl">
<style type="text/css">
.style1 {
text-align: center;
}
</style>
</head>
<div class="style1">
<br>
<br>
<iframe name="I1" id="I1" src="servertest.php" style="width: 578px; height: 359px" scrolling="no" border="0" frameborder="0">
<br>
</iframe><br>
</div>
In bovenstaande roep ik dan de “servertest.php”aan.
Code (php)
En zoals je ziet word dan (afhankelijk of de eerste server online is) een van de twee servers aangeroepen om het bestand te uploaden.
indexuploadserver.php
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
error_reporting(E_ALL);
$m_cnfg['max_size'] = 5120000; // size per file in bytes - 0 is unlimited
$m_cnfg['max_files'] = 0; // max files to upload - 0 is unlimited
$m_cnfg['ext_oke'] = array('rar', 'zip'); // allowed extensions, empty allow all
// $m_cnfg['ext_oke'] = array('png','jpg','gif');
$m_cnfg['upload_dir'] = $_SERVER['DOCUMENT_ROOT'] . "/test/uploads/"; // upload dir
$m_cnfg['overwrite'] = False; // allow files overwrite on true
$m_cnfg['IMG_'] = "timestamp"; // file rename "" or "timestamp" or set a rename prefix
$m_cnfg['inputname'] = "bestanden"; // input file form array name
$m_cnfg['imggd'] = array('jpg'); // resize with GD
$m_cnfg['imgw'] = 900; // max width for resize - 0 is no resize
$m_cnfg['imgh'] = 290; // max height for resize - 0 is no resize
$m_log = multiup($m_cnfg);
function multiup($m_cnfg) {
$upmsg = array();
if ($m_cnfg['max_size'] != 0) {
$maxdisp = ($m_cnfg['max_size'] >= 1024000) ? round($m_cnfg['max_size'] / 10240) / 100 . "
Mb" : round($m_cnfg['max_size'] / 10.24) / 100 . " Kb";
$upmsg['rules'][] = "Max file size: " . $maxdisp;
}
if (!empty($m_cnfg['ext_oke'])) {
$upmsg['rules'][] = "File types: " . implode(" ,", $m_cnfg['ext_oke']);
}
if ($m_cnfg['max_files'] != 0) {
// $upmsg['rules'][] = "Simultanious: " . $m_cnfg['max_files'];
}
if ($_SERVER["REQUEST_METHOD"] == 'POST' &&
!empty($_FILES[$m_cnfg['inputname']]['name'])) {
if (is_writable($m_cnfg['upload_dir'])) {
$filedir = str_replace("//", "/", $m_cnfg['upload_dir'] . "/");
$gdtrue = extension_loaded('gd') ? true : false;
$gd201 = function_exists('imagecopyresampled') ? true : false;
$gd2082 = $gd201 && function_exists('imagecreatefromgif') ? true : false;
$maxup = 1;
while(list($filenum, $filename) = each($_FILES[$m_cnfg['inputname']]['name'])) {
$uptrue = false;
if (!empty($filename)) {
$fileparts = pathinfo($filename);
$fileext = strtolower($fileparts['extension']);
// filter filename - adjust to needs - replace spaces by
// _ a-z A-Z - 0-9 _ . allowed - extension length 2-5 chars
$filename = str_replace("%20", "_", str_replace(" ", "_", $filename));
$uptrue = preg_match("/^[-a-z0-9_\.]+\.[a-z0-9]{2,5}$/i", $filename) ? true : false;
$uptrue = $uptrue && $m_cnfg['max_files'] != 0 && $m_cnfg['max_files'] < $maxup ? false : true;
$uptrue = $uptrue && (($fileext != "" && in_array($fileext, $m_cnfg['ext_oke'])) || empty($m_cnfg['ext_oke'])) ? true : false;
$uptrue = $uptrue && ($_FILES['bestanden']['size'][$filenum] < $m_cnfg['max_size'] || $m_cnfg['max_size'] == 0) ? true : false;
if (!$uptrue) {
$upmsg['errors'][] = $filename;
}
}
if ($uptrue) {
$maxup++;
$newname = "";
if ($m_cnfg['overwrite'] !== true || $m_cnfg['newname'] != "") {
$tmpname = $m_cnfg['newname'] != "" ? $m_cnfg['newname'] : str_replace ("." .
$fileext, "", $filename);
if ($m_cnfg['newname'] == "timestamp") {
list($usec) = explode(" ", microtime());
$tmpname = date('YmdHis') . (string)substr($usec,2,6);
}
$fileloc = $filedir . $tmpname . "." . $fileext;
$newname = $tmpname . "." . $fileext;
$a = 1;
if ($m_cnfg['overwrite'] !== true) {
while (file_exists($fileloc)) {
$a++;
$fileloc = $filedir . $tmpname . "_" . $a . "." . $fileext;
$newname = $tmpname . "_" . $a . "." . $fileext;
}
}
} else {
$fileloc = $filedir . $filename;
}
if (move_uploaded_file($_FILES[$m_cnfg['inputname']]['tmp_name'][$filenum], $fileloc))
{
if ($newname == $filename || $newname == "") {
$upmsg['uploads'][] = $filename;
} else {
$upmsg['uploads'][] = $filename . " as " . $newname;
}
if ($gdtrue && in_array($fileext, $m_cnfg['imggd']) && $m_cnfg['imgw'] > 0 &&
$m_cnfg['imgh'] > 0) {
list($curw, $curh) = getimagesize($fileloc);
$p_x = $curw / $m_cnfg['imgw'];
$p_y = $curh / $m_cnfg['imgh'];
if ($p_x > 1 || $p_y > 1) {
$pfactor = ($p_x > $p_y) ? $pfactor = $p_x : $pfactor = $p_y;
$neww = round($curw / $pfactor);
$newh = round($curh / $pfactor);
$imgorg = false;
if ($fileext == "jpg" || $fileext =='jpeg') {
$imgorg = imagecreatefromjpeg($fileloc);
} else if ($gd2082 && $fileext == "gif") {
$imgorg = imagecreatefromgif($fileloc);
} else if ($fileext == 'png') {
$imgorg = imagecreatefrompng($fileloc);
}
if ($imgorg) {
if ($gd201) {
$imgnew = imagecreatetruecolor($neww, $newh);
if ($fileext == "gif" || $fileext == "png") {
$bgcolor = imagecolorallocate($imgorg, 0, 0, 0);
imagepalettecopy($imgnew, $imgorg);
imagefill($imgnew, 0, 0, $bgcolor);
imagecolortransparent($imgnew, $bgcolor);
}
imagecopyresampled($imgnew, $imgorg, 0, 0, 0, 0, $neww, $newh, $curw,
$curh);
} else {
$imgnew = imagecreate($neww, $newh);
imagecopy($imgnew, $imgorg, 0, 0, 0, 0, $neww, $newh, $curw, $curh);
}
imagedestroy($imgorg);
if ($fileext == 'jpg' || $fileext =='jpeg') {
imagejpeg($imgnew, $fileloc, 80);
} else if ($gd2082 && $fileext == 'gif') {
imagegif($imgnew, $fileloc);
} else if ($fileext == 'png') {
imagepng($imgnew, $fileloc);
}
imagedestroy($imgnew);
list($neww, $newh) = getimagesize($fileloc);
array_pop($upmsg['uploads']);
if ($newname == $filename || $newname == "") {
$upmsg['uploads'][] = $filename . " - size: " . $neww . "px - " . $newh . "px";
} else {
$upmsg['uploads'][] = $filename . " as " . $newname . " - New Size: " . $neww .
px - " . $newh . "px";
}
}
}
}
}
}
if (file_exists($_FILES[$m_cnfg['inputname']]['tmp_name'][$filenum])) {
unlink($_FILES[$m_cnfg['inputname']]['tmp_name'][$filenum]);
}
}
} else {
$upmsg['errors'][] = "Dir error: " . $m_cnfg['upload_dir'];
}
}
return $upmsg;
}
?>
<html>
<head>
<title>Upload-multi</title>
<style>
body {
font-family: arial;
font-size: 12px;
margin-left: 50px;
margin-top: 50px;
}
</style></head>
<body>
<script type="text/javascript"><!--
// specify max_files to upload when used apart from upload multi
// 0 = unlimited - adjust in your config also
var max_files = <?php echo $m_cnfg['max_files']; ?>;
var count_files = 1;
function bestanden() {
if (max_files == 0 || max_files > count_files) {
var holder =
document.getElementById('bestanden').getElementsByTagName('tr')[0].parentNode;
var taginp = document.createElement('input');
taginp.setAttribute('type','file');
taginp.setAttribute('name','bestanden[]');
var tagtr = document.createElement('tr');
var tagtd = document.createElement('td');
var tagspan = document.createElement('span');
tagspan.style.cursor = 'pointer';
tagspan.onclick = function() {
this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
count_files--;
}
tagspan.appendChild(document.createTextNode(' Delete'));
tagtd.appendChild(taginp);
tagtd.appendChild(tagspan);
tagtr.appendChild(tagtd);
holder.appendChild(tagtr);
count_files++;
}
}
//--></script>
<form method="POST" action="[code]<?php echo $_SERVER['SCRIPT_NAME']; ?>" enctype="multipart/form-data">
<table id="bestanden">
<tr><td><input type="file" name="bestanden[]"> <span onClick="bestanden()"
style="cursor:pointer;">Add to server 1</span></td></tr>
</table>
<input type="submit" value="Upload">
</form>
<?php
if (!empty($m_log['rules'])) {
echo "<b>Upload rules</b> <br />";
echo implode("<br />", $m_log['rules']);
}
if (!empty($m_log['uploads'])) {
echo "<br /><br /><b>Uploaded</b> <br />";
echo implode("<br />", $m_log['uploads']);
}
if (!empty($m_log['errors'])) {
echo "<br /><br /><b>Upload failed for</b> <br />";
echo implode("<br />", $m_log['errors']);
}
?>
</body>
</html>
[size=xsmall][i]Toevoeging op 03/03/2012 16:17:26:[/i][/size]
Be gentle....
error_reporting(E_ALL);
$m_cnfg['max_size'] = 5120000; // size per file in bytes - 0 is unlimited
$m_cnfg['max_files'] = 0; // max files to upload - 0 is unlimited
$m_cnfg['ext_oke'] = array('rar', 'zip'); // allowed extensions, empty allow all
// $m_cnfg['ext_oke'] = array('png','jpg','gif');
$m_cnfg['upload_dir'] = $_SERVER['DOCUMENT_ROOT'] . "/test/uploads/"; // upload dir
$m_cnfg['overwrite'] = False; // allow files overwrite on true
$m_cnfg['IMG_'] = "timestamp"; // file rename "" or "timestamp" or set a rename prefix
$m_cnfg['inputname'] = "bestanden"; // input file form array name
$m_cnfg['imggd'] = array('jpg'); // resize with GD
$m_cnfg['imgw'] = 900; // max width for resize - 0 is no resize
$m_cnfg['imgh'] = 290; // max height for resize - 0 is no resize
$m_log = multiup($m_cnfg);
function multiup($m_cnfg) {
$upmsg = array();
if ($m_cnfg['max_size'] != 0) {
$maxdisp = ($m_cnfg['max_size'] >= 1024000) ? round($m_cnfg['max_size'] / 10240) / 100 . "
Mb" : round($m_cnfg['max_size'] / 10.24) / 100 . " Kb";
$upmsg['rules'][] = "Max file size: " . $maxdisp;
}
if (!empty($m_cnfg['ext_oke'])) {
$upmsg['rules'][] = "File types: " . implode(" ,", $m_cnfg['ext_oke']);
}
if ($m_cnfg['max_files'] != 0) {
// $upmsg['rules'][] = "Simultanious: " . $m_cnfg['max_files'];
}
if ($_SERVER["REQUEST_METHOD"] == 'POST' &&
!empty($_FILES[$m_cnfg['inputname']]['name'])) {
if (is_writable($m_cnfg['upload_dir'])) {
$filedir = str_replace("//", "/", $m_cnfg['upload_dir'] . "/");
$gdtrue = extension_loaded('gd') ? true : false;
$gd201 = function_exists('imagecopyresampled') ? true : false;
$gd2082 = $gd201 && function_exists('imagecreatefromgif') ? true : false;
$maxup = 1;
while(list($filenum, $filename) = each($_FILES[$m_cnfg['inputname']]['name'])) {
$uptrue = false;
if (!empty($filename)) {
$fileparts = pathinfo($filename);
$fileext = strtolower($fileparts['extension']);
// filter filename - adjust to needs - replace spaces by
// _ a-z A-Z - 0-9 _ . allowed - extension length 2-5 chars
$filename = str_replace("%20", "_", str_replace(" ", "_", $filename));
$uptrue = preg_match("/^[-a-z0-9_\.]+\.[a-z0-9]{2,5}$/i", $filename) ? true : false;
$uptrue = $uptrue && $m_cnfg['max_files'] != 0 && $m_cnfg['max_files'] < $maxup ? false : true;
$uptrue = $uptrue && (($fileext != "" && in_array($fileext, $m_cnfg['ext_oke'])) || empty($m_cnfg['ext_oke'])) ? true : false;
$uptrue = $uptrue && ($_FILES['bestanden']['size'][$filenum] < $m_cnfg['max_size'] || $m_cnfg['max_size'] == 0) ? true : false;
if (!$uptrue) {
$upmsg['errors'][] = $filename;
}
}
if ($uptrue) {
$maxup++;
$newname = "";
if ($m_cnfg['overwrite'] !== true || $m_cnfg['newname'] != "") {
$tmpname = $m_cnfg['newname'] != "" ? $m_cnfg['newname'] : str_replace ("." .
$fileext, "", $filename);
if ($m_cnfg['newname'] == "timestamp") {
list($usec) = explode(" ", microtime());
$tmpname = date('YmdHis') . (string)substr($usec,2,6);
}
$fileloc = $filedir . $tmpname . "." . $fileext;
$newname = $tmpname . "." . $fileext;
$a = 1;
if ($m_cnfg['overwrite'] !== true) {
while (file_exists($fileloc)) {
$a++;
$fileloc = $filedir . $tmpname . "_" . $a . "." . $fileext;
$newname = $tmpname . "_" . $a . "." . $fileext;
}
}
} else {
$fileloc = $filedir . $filename;
}
if (move_uploaded_file($_FILES[$m_cnfg['inputname']]['tmp_name'][$filenum], $fileloc))
{
if ($newname == $filename || $newname == "") {
$upmsg['uploads'][] = $filename;
} else {
$upmsg['uploads'][] = $filename . " as " . $newname;
}
if ($gdtrue && in_array($fileext, $m_cnfg['imggd']) && $m_cnfg['imgw'] > 0 &&
$m_cnfg['imgh'] > 0) {
list($curw, $curh) = getimagesize($fileloc);
$p_x = $curw / $m_cnfg['imgw'];
$p_y = $curh / $m_cnfg['imgh'];
if ($p_x > 1 || $p_y > 1) {
$pfactor = ($p_x > $p_y) ? $pfactor = $p_x : $pfactor = $p_y;
$neww = round($curw / $pfactor);
$newh = round($curh / $pfactor);
$imgorg = false;
if ($fileext == "jpg" || $fileext =='jpeg') {
$imgorg = imagecreatefromjpeg($fileloc);
} else if ($gd2082 && $fileext == "gif") {
$imgorg = imagecreatefromgif($fileloc);
} else if ($fileext == 'png') {
$imgorg = imagecreatefrompng($fileloc);
}
if ($imgorg) {
if ($gd201) {
$imgnew = imagecreatetruecolor($neww, $newh);
if ($fileext == "gif" || $fileext == "png") {
$bgcolor = imagecolorallocate($imgorg, 0, 0, 0);
imagepalettecopy($imgnew, $imgorg);
imagefill($imgnew, 0, 0, $bgcolor);
imagecolortransparent($imgnew, $bgcolor);
}
imagecopyresampled($imgnew, $imgorg, 0, 0, 0, 0, $neww, $newh, $curw,
$curh);
} else {
$imgnew = imagecreate($neww, $newh);
imagecopy($imgnew, $imgorg, 0, 0, 0, 0, $neww, $newh, $curw, $curh);
}
imagedestroy($imgorg);
if ($fileext == 'jpg' || $fileext =='jpeg') {
imagejpeg($imgnew, $fileloc, 80);
} else if ($gd2082 && $fileext == 'gif') {
imagegif($imgnew, $fileloc);
} else if ($fileext == 'png') {
imagepng($imgnew, $fileloc);
}
imagedestroy($imgnew);
list($neww, $newh) = getimagesize($fileloc);
array_pop($upmsg['uploads']);
if ($newname == $filename || $newname == "") {
$upmsg['uploads'][] = $filename . " - size: " . $neww . "px - " . $newh . "px";
} else {
$upmsg['uploads'][] = $filename . " as " . $newname . " - New Size: " . $neww .
px - " . $newh . "px";
}
}
}
}
}
}
if (file_exists($_FILES[$m_cnfg['inputname']]['tmp_name'][$filenum])) {
unlink($_FILES[$m_cnfg['inputname']]['tmp_name'][$filenum]);
}
}
} else {
$upmsg['errors'][] = "Dir error: " . $m_cnfg['upload_dir'];
}
}
return $upmsg;
}
?>
<html>
<head>
<title>Upload-multi</title>
<style>
body {
font-family: arial;
font-size: 12px;
margin-left: 50px;
margin-top: 50px;
}
</style></head>
<body>
<script type="text/javascript"><!--
// specify max_files to upload when used apart from upload multi
// 0 = unlimited - adjust in your config also
var max_files = <?php echo $m_cnfg['max_files']; ?>;
var count_files = 1;
function bestanden() {
if (max_files == 0 || max_files > count_files) {
var holder =
document.getElementById('bestanden').getElementsByTagName('tr')[0].parentNode;
var taginp = document.createElement('input');
taginp.setAttribute('type','file');
taginp.setAttribute('name','bestanden[]');
var tagtr = document.createElement('tr');
var tagtd = document.createElement('td');
var tagspan = document.createElement('span');
tagspan.style.cursor = 'pointer';
tagspan.onclick = function() {
this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
count_files--;
}
tagspan.appendChild(document.createTextNode(' Delete'));
tagtd.appendChild(taginp);
tagtd.appendChild(tagspan);
tagtr.appendChild(tagtd);
holder.appendChild(tagtr);
count_files++;
}
}
//--></script>
<form method="POST" action="[code]<?php echo $_SERVER['SCRIPT_NAME']; ?>" enctype="multipart/form-data">
<table id="bestanden">
<tr><td><input type="file" name="bestanden[]"> <span onClick="bestanden()"
style="cursor:pointer;">Add to server 1</span></td></tr>
</table>
<input type="submit" value="Upload">
</form>
<?php
if (!empty($m_log['rules'])) {
echo "<b>Upload rules</b> <br />";
echo implode("<br />", $m_log['rules']);
}
if (!empty($m_log['uploads'])) {
echo "<br /><br /><b>Uploaded</b> <br />";
echo implode("<br />", $m_log['uploads']);
}
if (!empty($m_log['errors'])) {
echo "<br /><br /><b>Upload failed for</b> <br />";
echo implode("<br />", $m_log['errors']);
}
?>
</body>
</html>
[size=xsmall][i]Toevoeging op 03/03/2012 16:17:26:[/i][/size]
Be gentle....
Gewijzigd op 03/03/2012 15:21:46 door Danny A-noniem