Jump to content

Check if URL exists


KelloKitty

Recommended Posts

Can someone tell me how I would check to see if the url exists before i go into displaying stuff. So that on the page it will display something like "doesnt exit" instead of "Warning: readdir(): supplied argument is not a valid Directory resource"

 

Thanks!

 


if( !empty($_POST['year']) ) {
        $year = $_POST['year'];
        $month = $_POST['month'];
        $day = $_POST['day'];
        $hourFrom = $_POST['hourFrom'];
        $hourTo = $_POST['hourTo'];
        
        $url = "/bla/bla/$year$month$day/";
    
        // check if url exists here?
    
       $dir = opendir($url);

      while ($file = readdir($dir)) { 
       //display files

Link to comment
https://forums.phpfreaks.com/topic/191002-check-if-url-exists/
Share on other sites

there's 2 ways I can think of off the top of my head

 

if( !empty($_POST['year']) )
{
$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
$hourFrom = $_POST['hourFrom'];
$hourTo = $_POST['hourTo'];

$url = "/bla/bla/$year$month$day/";

if (!$dir = opendir($url)) // if the operation returns nothing...
{
	echo '<p>No Such Directory ('.$url.')</p>';
}
else
{
	while ($file = readdir($dir))
	{ 
		// display files
	}
}
}

 

or

 

if( !empty($_POST['year']) )
{
$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
$hourFrom = $_POST['hourFrom'];
$hourTo = $_POST['hourTo'];

$url = "/bla/bla/$year$month$day/";

if (!file_exists($url)) // if the specific file does not exist on that server...
{
	echo '<p>No Such File ('.$url.')</p>';
}
else
{
	$dir = opendir($url);
	while ($file = readdir($dir))
	{ 
		// display files
	}
}
}

 

though I've never tested that second one on files not on my own server.

Link to comment
https://forums.phpfreaks.com/topic/191002-check-if-url-exists/#findComment-1007182
Share on other sites

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.