KelloKitty Posted February 5, 2010 Share Posted February 5, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/191002-check-if-url-exists/ Share on other sites More sharing options...
Alex Posted February 5, 2010 Share Posted February 5, 2010 file_exists Quote Link to comment https://forums.phpfreaks.com/topic/191002-check-if-url-exists/#findComment-1007178 Share on other sites More sharing options...
KelloKitty Posted February 5, 2010 Author Share Posted February 5, 2010 Don't i have to check to see if the url exists before the file or else I will get an opendir error? Or you are saying I can use file_exists function on a url? Quote Link to comment https://forums.phpfreaks.com/topic/191002-check-if-url-exists/#findComment-1007179 Share on other sites More sharing options...
Alex Posted February 5, 2010 Share Posted February 5, 2010 Check if the url exists? You mean the directory? file_exists, if you read documentation, checks files and directories. Quote Link to comment https://forums.phpfreaks.com/topic/191002-check-if-url-exists/#findComment-1007180 Share on other sites More sharing options...
Errant_Shadow Posted February 5, 2010 Share Posted February 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/191002-check-if-url-exists/#findComment-1007182 Share on other sites More sharing options...
KelloKitty Posted February 5, 2010 Author Share Posted February 5, 2010 Awesome totally works thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/191002-check-if-url-exists/#findComment-1007183 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.