Hi!

I want to check if some file on some other server exists. I have this code (but something is wrong with it):

<?php
$notworking=array();
$false=0;
$link="http://uploads.mp3songurls.com/";
$files=glob("genre/*.xml");
foreach($files as $file){
$xml=new SimpleXMLElement($file,null,true);
foreach($xml as $info){
if(fopen($link.$info->link.".mp3","r")==false){
$false++;
$notworking[$false]=$info->name." => ".$info->author;
}
}
}
if($false>0){
$file=fopen("notworking.txt","w");
for($i=1;$i<=$false;$i++){
fwrite($file,$notworking[$i]."\r\n");
}
fclose($file);
echo "(some songs don't exist: <a href=\"javascript:void(0)\" onclick=\"window.open('notexist.php','popupWindow','height=500,width=600,left=30,top=40,toolbar=no,location=no,status=no,menubar=no,scrollbars=no');\">$false</a>)";
}else{
if(file_exists("notworking.txt"))
unlink("notworking.txt");
echo "(everything's OK)";
}
?>

I'm getting "(everything's OK)", but one file isn't OK (it doesn't exist). Is there some error in code or what? Thanks a lot! (PS. And with XML file is everything OK)

Maybe, because you do not close the open file in the first loop.

No, it's not that. But when I open this file, it automatically redirects me to home page. Is this problem because of redirection, or?

Possible. I think fopen follows redirects. IIRC there may be a way to set maximum number of redirects to follow. You'd have to google for it.

I found something, but it doesn't work.

<?php
$redirect=array('http'=>array('max_redirects'=>'0')); //I tried to add only 0
$context=stream_context_create($redirect);
$file=fopen("http://uploads.mp3songurls.com/1101746.mp3","r",false,$context);
if($file==false)
echo "false";
else echo "true";
?>

I'm getting true. What's mistake?

According to this page, max_redirects should be set to 1. Apparantly it denotes requests, not redirects.

Mistake is in ==, it must be ===. So:

<?php
$options=array('max_redirects'=>1);
$context=stream_context_create(array('http'=>$options));
$file=fopen("http://uploads.mp3songurls.com/1101746.mp3","r",false,$context);
if($file===true)
echo "true";
else echo "false";
?>

Thanks a lot for helping me!
There's still a mistake. For existing files it also returns false (eg. http://uploads.mp3songurls.com/1102493.mp3)

eff you for trying to steal my files asswipe

Nevermind!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.