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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.