Jump to content

If File Exists Display this else Display that


jnerotrix

Recommended Posts

Whats wrong with my code i want if the file exists to display what it says to display else display other info but it always displays the form heres the code i have so far

 

 

 

<?php


$ip = $_SERVER['REMOTE_ADDR'];

$useripdata = '/ip/' . $ip .'.txt';

if (file_exists($useripdata)) {

foreach (file("/ip/$ip.txt") as $line) {
  echo '$' . $line; 

}

for ($i=0; $i < $numTimes; $i++) {
echo '<object width="$setwidth" height="$setheight""><param name="movie" value="http://www.youtube.com/v/' . $shorturl . '&hl=en&fs=1"></param><param name="allowFullScreen" value="false"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' . $shorturl . '&hl=en&fs=1$autoplay" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="false" width="$setwidth" height="$setheight"></embed></object>';
}

echo "<meta http-equiv='refresh' content='$reloadtime'>";

} else {
   
if (isset($_POST['action'])) {
   
$numTimes = (isset($_POST['times']) && is_numeric($_POST['times']))?$_POST['times']:10;
$shorturl = $_POST['url'];
$autoplay = "&autoplay=1";
$reloadtime = $_POST['reloadtime'];
$setwidth = $_POST['setwidth'];
$setheight = $_POST['setheight'];
$shorturl = $_POST['url'];
$fullurl = "http://www.youtube.com/watch?v=$shorturl";


$myFile = "./ip/$ip.txt";
$fh = fopen($myFile, 'w');
$stringData = "numTimes = 10 \n";
fwrite($fh, $stringData);
$stringData = "shorturl = $shorturl \n";
fwrite($fh, $stringData);
$stringData = "autoplay = $autoplay \n";
fwrite($fh, $stringData);
$stringData = "reloadtime = $reloadtime \n";
fwrite($fh, $stringData);
$stringData = "setwidth = $setwidth \n";
fwrite($fh, $stringData);
$stringData = "setheight = $setheight \n";
fwrite($fh, $stringData);
$stringData = "fullurl = $fullurl \n";
fwrite($fh, $stringData);
fclose($fh);


function check_if_valid($fullurl) {
$res = (($check = @fopen($fullurl, ‘r’)) === false) ? false : @fclose($check);
return ($res == TRUE) ? VIDEO_IS_VALID:VIDEO_IS_INVALID ;
}

if ($HTTP_POST_VARS["submitted"] == "yes"){
echo "<meta http-equiv='refresh' content='$reloadtime'>";
}


for ($i=0; $i < $numTimes; $i++) {
echo '<object width="$setwidth" height="$setheight""><param name="movie" value="http://www.youtube.com/v/' . $shorturl . '&hl=en&fs=1"></param><param name="allowFullScreen" value="false"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' . $shorturl . '&hl=en&fs=1$autoplay" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="false" width="$setwidth" height="$setheight"></embed></object>';
}

}

}

$self = $_SERVER['PHP_SELF'];

echo "<table border='1'>";
echo "<form action='$self' method='post'>";
echo "<tr>";
echo "<td>Video Url:<input type='text' readonly='1' value='http://www.youtube.com/watch?v=' size='30'><input type='Text' name='url'></td>";
echo "</tr>";
echo "<tr>";
echo "<td>(Number)<input type='text' name='times' value='10'> of Times to Display Video in <input type='text' name='reloadtime' value='5'>(Seconds)</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Set Width <input type='text' name='setwidth' value='425'> Set Height <input type='text' name='setheight' value='344'>";
echo "</tr>";
echo "<tr>";
echo "<td align='center' valign='center'><input type='submit' name='action' value='Submit'>";
echo "<input type='hidden' name='submitted' value='yes'>";
echo "</td>";
echo "</tr>";
echo "</form>";
echo "</table>";

?>

I changed the code a bit:

 

$useripdata = "/ip/$ip.txt";

instead of

$useripdata = '/ip/' . $ip .'.txt';

 

This but still doesnt work correctly

 

So the Code Looks Like This

 

<?php


$ip = $_SERVER['REMOTE_ADDR'];

$useripdata = "/ip/$ip.txt";

if (file_exists($useripdata)) {

foreach (file("/ip/$ip.txt") as $line) {
  echo '$' . $line; 

}

for ($i=0; $i < $numTimes; $i++) {
echo '<object width="$setwidth" height="$setheight""><param name="movie" value="http://www.youtube.com/v/' . $shorturl . '&hl=en&fs=1"></param><param name="allowFullScreen" value="false"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' . $shorturl . '&hl=en&fs=1$autoplay" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="false" width="$setwidth" height="$setheight"></embed></object>';
}

echo "<meta http-equiv='refresh' content='$reloadtime'>";

} else {
   
if (isset($_POST['action'])) {
   
$numTimes = (isset($_POST['times']) && is_numeric($_POST['times']))?$_POST['times']:10;
$shorturl = $_POST['url'];
$autoplay = "&autoplay=1";
$reloadtime = $_POST['reloadtime'];
$setwidth = $_POST['setwidth'];
$setheight = $_POST['setheight'];
$shorturl = $_POST['url'];
$fullurl = "http://www.youtube.com/watch?v=$shorturl";


$myFile = "./ip/$ip.txt";
$fh = fopen($myFile, 'w');
$stringData = "numTimes = 10 \n";
fwrite($fh, $stringData);
$stringData = "shorturl = $shorturl \n";
fwrite($fh, $stringData);
$stringData = "autoplay = $autoplay \n";
fwrite($fh, $stringData);
$stringData = "reloadtime = $reloadtime \n";
fwrite($fh, $stringData);
$stringData = "setwidth = $setwidth \n";
fwrite($fh, $stringData);
$stringData = "setheight = $setheight \n";
fwrite($fh, $stringData);
$stringData = "fullurl = $fullurl \n";
fwrite($fh, $stringData);
fclose($fh);


function check_if_valid($fullurl) {
$res = (($check = @fopen($fullurl, ‘r’)) === false) ? false : @fclose($check);
return ($res == TRUE) ? VIDEO_IS_VALID:VIDEO_IS_INVALID ;
}

if ($HTTP_POST_VARS["submitted"] == "yes"){
echo "<meta http-equiv='refresh' content='$reloadtime'>";
}


for ($i=0; $i < $numTimes; $i++) {
echo '<object width="$setwidth" height="$setheight""><param name="movie" value="http://www.youtube.com/v/' . $shorturl . '&hl=en&fs=1"></param><param name="allowFullScreen" value="false"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' . $shorturl . '&hl=en&fs=1$autoplay" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="false" width="$setwidth" height="$setheight"></embed></object>';
}

}

}

$self = $_SERVER['PHP_SELF'];

echo "<table border='1'>";
echo "<form action='$self' method='post'>";
echo "<tr>";
echo "<td>Video Url:<input type='text' readonly='1' value='http://www.youtube.com/watch?v=' size='30'><input type='Text' name='url'></td>";
echo "</tr>";
echo "<tr>";
echo "<td>(Number)<input type='text' name='times' value='10'> of Times to Display Video in <input type='text' name='reloadtime' value='5'>(Seconds)</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Set Width <input type='text' name='setwidth' value='425'> Set Height <input type='text' name='setheight' value='344'>";
echo "</tr>";
echo "<tr>";
echo "<td align='center' valign='center'><input type='submit' name='action' value='Submit'>";
echo "<input type='hidden' name='submitted' value='yes'>";
echo "</td>";
echo "</tr>";
echo "</form>";
echo "</table>";

?>

the first thing I can see wrong with this appears to be your foreach statment.

 

foreach (file("/ip/$ip.txt") as $line) {
  echo '$' . $line; 

}

 

You look as though you are redoing a step.  couldnt you put

 

foreach (file($useripdata) as $line) {
  echo '$' . $line; 

}

 

as $useripdata is already formatted to the string you are looking for?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.