sjxx Posted August 23, 2007 Share Posted August 23, 2007 Hey, To start off , I am a total noob to PHP but I have done a fair share of research on it and tried making my own script. The script I am trying to make is supposed to do the following Check a website ( www.example.com ) for files Ex: It should check for this www.random.com/1.wmv www.random.com/1.avi -------------------- www.random.com/2.wmv www.random.com/2.avi -------------------- www.random.com/3.wmv www.random.com/3.avi ---------------------- ETC..... The script should do this all the way up to a 100 and then echo back to me with information whether the file/video exists or not Here's something I got so far <?php for ($i=1; $i<101; $i++) { echo "www.seajist.100webspace.net/".$i.".wmv<br>/n"; echo "www.seajist.100webspace.net/".$i.".avi<br>/n"; } ?> Then I assume I should combine it with something like this <?php $filename = 'www.seajist.100webspace.net/71.avi'; if (file_exists($filename)) { echo "The file $filename exists"; } else { echo "The file $filename does not exist"; } ?> The problem is, I don't know how to sort of combine the two to work in the way I need it. Could anyone possibly help =) Thanks, -SJ Quote Link to comment Share on other sites More sharing options...
matthewhaworth Posted August 23, 2007 Share Posted August 23, 2007 <?php for ($i=1; $i<101; $i++) { $filename_avi = 'www.seajist.100webspace.net/'.$i.'.avi'; $filename_wmv = 'www.seajist.100webspace.net/'.$i.'.wmv'; if (file_exists($filename_avi)) { echo "www.seajist.100webspace.net/".$i.".avi<br />/n"; } else { echo "The file ".$filename." does not exist<br />/n"; } if (file_exists($filename_wmv)) { echo "www.seajist.100webspace.net/".$i.".wmv<br />/n"; } else { echo "The file ".$filename." does not exist<br />/n"; } } ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2007 Share Posted August 23, 2007 http://us2.php.net/file_exists file_exists is only for files on your server, it won't (AFAIK) work for URLS (which btw, need to begin with http://) Try get_headers perhaps? http://us2.php.net/manual/en/function.get-headers.php Quote Link to comment Share on other sites More sharing options...
sjxx Posted August 23, 2007 Author Share Posted August 23, 2007 I appreciate the quick reply but I am trying to check for files on a remote server so I would have to use the fopen() Would this work? <?php for ($i=1; $i<101; $i++) { $filename_avi = 'www.seajist.100webspace.net/'.$i.'.avi'; $filename_wmv = 'www.seajist.100webspace.net/'.$i.'.wmv'; if (fopen($filename_avi)) { echo "www.seajist.100webspace.net/".$i.".avi<br />/n"; } else { echo "The file ".$filename." does not exist<br />/n"; } if (fopen($filename_wmv)) { echo "www.seajist.100webspace.net/".$i.".wmv<br />/n"; } else { echo "The file ".$filename." does not exist<br />/n"; } } ?> Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 23, 2007 Share Posted August 23, 2007 you will have a long code using that try this first http://us2.php.net/file_exists file_exists is only for files on your server, it won't (AFAIK) work for URLS (which btw, need to begin with http://) Try get_headers perhaps? http://us2.php.net/manual/en/function.get-headers.php Quote Link to comment Share on other sites More sharing options...
matthewhaworth Posted August 23, 2007 Share Posted August 23, 2007 If PHP has decided that filename specifies a local file, then it will try to open a stream on that file. The file must be accessible to PHP, so you need to ensure that the file access permissions allow this access. If you have enabled safe mode, or open_basedir further restrictions may apply. If PHP has decided that filename specifies a registered protocol, and that protocol is registered as a network URL, PHP will check to make sure that allow_url_fopen is enabled. If it is switched off, PHP will emit a warning and the fopen call will fail. Thought it might interest you.. http://uk.php.net/manual/en/function.fopen.php Quote Link to comment Share on other sites More sharing options...
teng84 Posted August 23, 2007 Share Posted August 23, 2007 why do you need to use fopen when all you want is to check if that thing exist????????????????? Quote Link to comment Share on other sites More sharing options...
sjxx Posted August 23, 2007 Author Share Posted August 23, 2007 I already looked at that website but I am not sure how to associate the fopen with my needs =/ Quote Link to comment Share on other sites More sharing options...
sjxx Posted August 23, 2007 Author Share Posted August 23, 2007 Ya, I don't need to open/stream the file, I just need a verification that "File number ## Exists" Quote Link to comment Share on other sites More sharing options...
sjxx Posted August 23, 2007 Author Share Posted August 23, 2007 b Quote Link to comment Share on other sites More sharing options...
trq Posted August 23, 2007 Share Posted August 23, 2007 <?php for ($i=1; $i<=100; $i++) { $filename_avi = 'www.seajist.100webspace.net/'.$i.'.avi'; $filename_wmv = 'www.seajist.100webspace.net/'.$i.'.wmv'; $ok = @get_headers($filename_avi); if (preg_match("|200|", $ok[0])) { echo "$filename_avi<br />/n"; } else { echo "The file ".$filename_avi." does not exist<br />/n"; } $ok = @get_headers($filename_wmv); if (preg_match("|200|", $ok[0])) { echo "$filename_wmv<br />/n"; } else { echo "The file ".$filename_wmv." does not exist<br />/n"; } } ?> Quote Link to comment Share on other sites More sharing options...
sjxx Posted August 23, 2007 Author Share Posted August 23, 2007 ^I tried the above script but the page is just all blank, which I assume is incorrect because it should have found the 77.avi on the server Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2007 Share Posted August 23, 2007 Did you try it with http://www...I think it has to be http:// not just www. Quote Link to comment Share on other sites More sharing options...
sjxx Posted August 23, 2007 Author Share Posted August 23, 2007 I added in http://www. but the page is still blank Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2007 Share Posted August 23, 2007 Take out the www. Look at the URL you're trying to access. www.seajist.100webspace.net/77.avi Actually go to it in your browser. Is anything there? No. Google "subdomains". Your site is not www.seajist.100webspace.net/ it is http://seajist.100webspace.net/ http://seajist.100webspace.net/77.avi HAS a file. Quote Link to comment Share on other sites More sharing options...
sjxx Posted August 23, 2007 Author Share Posted August 23, 2007 Yes I know but it isn't my site I am trying to look for files, it was just an example. Here is the script I am currently using modified to my needs <?php for ($i=1; $i<=100; $i++) { $filename_avi = 'http://www.xspand.ca/'.$i.'.avi'; $filename_wmv = 'http://www.xspand.ca/'.$i.'.wmv'; $ok = @get_headers($filename_avi); if (preg_match("|200|", $ok[0])) { echo "$filename_avi<br />/n"; } else { echo "The file ".$filename_avi." does not exist<br />/n"; } $ok = @get_headers($filename_wmv); if (preg_match("|200|", $ok[0])) { echo "$filename_wmv<br />/n"; } else { echo "The file ".$filename_wmv." does not exist<br />/n"; } } ?> Either way, the PHP page returns blank Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2007 Share Posted August 23, 2007 Take out the error supression sybmol: @ everywhere. The file: http://www.xspand.ca/77.avi Doesn't exist there either. Do you have an example of a url that actually exists? Quote Link to comment Share on other sites More sharing options...
sjxx Posted August 23, 2007 Author Share Posted August 23, 2007 No I don't have an actual example The example I was using to test was the seajist.100webspace.net/77.avi Here is the error after removing @ Fatal error: Call to undefined function: get_headers() in /home/www/seajist.100webspace.net/omg.php on line 6 Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2007 Share Posted August 23, 2007 Are you using PHP4? get_headers is a PHP5 function only. http://us.php.net/manual/en/function.get-headers.php If you scroll down through the comments it looks like someone has posted a function to emulate it, that will work in 4. Quote Link to comment Share on other sites More sharing options...
sjxx Posted August 23, 2007 Author Share Posted August 23, 2007 Yes I am, The free service from 100webspace I am using says PHP 4/5 Support Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2007 Share Posted August 23, 2007 You'll probably need to contact them to see how to get it switched to PHP5 for all your pages. This is how I force PHP5 on my server on MT. It may/may not work for you, but it's worth a shot. Save this code as simply .htaccess and upload it to your site: AddHandler php5-script .php Quote Link to comment Share on other sites More sharing options...
sjxx Posted August 23, 2007 Author Share Posted August 23, 2007 I added that and it made it so I couldn't view my php file, it automatically asks me to download it if I try to view it Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2007 Share Posted August 23, 2007 You need to contact your host then. If it says the function is not found, then it sounds like you're running PHP4, not PHP5. You can also try changing the file extension from .php to .php5 but I think it will have the same result. (Remove the .htaccess file tho.) Quote Link to comment Share on other sites More sharing options...
sjxx Posted August 23, 2007 Author Share Posted August 23, 2007 nevermind, got it working =D Quote Link to comment 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.