PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
rainbow
Forum Newbie
Posts: 13 Joined: Thu Aug 22, 2002 6:03 am
Location: Finland
Post
by rainbow » Sat Sep 07, 2002 4:14 am
I have some problem with deleting files and folders. I have a script that
deletes the files and folders inside another folder. Here it is:
Code: Select all
<?php
$centre_id = 16;
if(!($result11 = mysql_query("SELECT ct_centre_id, ct_centre_name from ct_centre WHERE ct_centre_id = '$centre_id'", $con)))
{
Error(sprintf("Internal Error %d:%s\n",mysql_errno(),mysql_error()));
exit();
}
$row11 = mysql_fetch_array($result11);
$centre_id = $row11ї"ct_centre_id"];
$centre_name = $row11ї"ct_centre_name"];
if(!($result12 = mysql_query("SELECT student_id from ct_centre_coaches WHERE ct_centre_id = '$centre_id'", $con)))
{
Error(sprintf("Internal Error %d:%s\n",mysql_errno(),mysql_error()));
exit();
}
$num_results = mysql_num_rows($result12);
$count = 0;
$id= 0;
for ($i=0; $i<$num_results; $i++)
{
$row12 = mysql_fetch_array($result12);
$coach_id = $row12ї"student_id"];
if(!($result13 = mysql_query("SELECT ct_cdc_deliv_topic_id from ct_cdc_deliv_topic WHERE ct_centre_id = '$centre_id'", $con)))
{
Error(sprintf("Internal Error %d:%s\n",mysql_errno(),mysql_error()));
exit();
}
// Delete entries from deliverable tables.
while($row13 = mysql_fetch_array($result13))
{
$ct_topic_id = $row13ї"ct_cdc_deliv_topic_id"];
$center_path = "".$cdc_path."/centre".$centre_id."";
$dir2remove = "".$cdc_path."/centre".$centre_id."/coach".$coach_id."/topic".$ct_topic_id."/version".$version."/";
if ($dir = @opendir("$dir2remove"))
{
while (($file = readdir($dir)) != false)
{
if (!($file=="." || $file==".."))
{
chdir($center_path."/coach".$coach_id."/topic".$ct_topic_id."/version".$version."/");
unlink($center_path."/coach".$coach_id."/topic".$ct_topic_id."/version".$version."/".$file."");
chdir($center_path."/coach".$coach_id."/topic".$ct_topic_id."/");
unlink($center_path."/coach".$coach_id."/topic".$ct_topic_id."/".$file."");
chdir($center_path."/coach".$coach_id."/");
unlink($center_path."/coach".$coach_id."/".$file.""); // ??
chdir($center_path."/");
unlink($center_path."/".$file.""); // ??
}
} //close while
closedir($dir);
rmdir($center_path."/coach".$coach_id."/topic".$ct_topic_id."/version".$version."/");
rmdir($center_path."/coach".$coach_id."/topic".$ct_topic_id."/");
rmdir($center_path."/coach".$coach_id."/"); // ??
rmdir($center_path."/"); // ??
}
}
$stat = "center has been deleted";
}
?>
So, when i run the script , it actually works and deletes the stuff, but at the same time it gives the following kind of errors:
Warning: Unlink failed (No such file or directory) in c:\.....\script.php on line 71
Warning: RmDir failed (Permission denied) in c:\...\script.php on line 79
for lines I marked in the code with "??" sign.
Can someone tell what is the problem ?
Takuma
Forum Regular
Posts: 931 Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:
Post
by Takuma » Sat Sep 07, 2002 11:32 am
How about remoivng those code?
Takuma
Forum Regular
Posts: 931 Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:
Post
by Takuma » Sat Sep 07, 2002 11:50 am
Sorry, it might be bacuase of this
Code: Select all
thomasj@superusers.dk (05-Jul-2000 09:42)
if you can't unlink a file it might be, that there is no write permission for the DIRECTORY in which the file resides. Check this out.
And I don't think you need to do chdir();
gite_ashish
Forum Contributor
Posts: 118 Joined: Sat Aug 31, 2002 11:38 am
Location: India
Post
by gite_ashish » Sat Sep 07, 2002 11:53 am
hi,
for unlink()/rmdir() php need to have the permissions on the respective files and directories.
u r getting error for the outer files, directories onyl:
Code: Select all
<?php
unlink($center_path."/coach".$coach_id."/".$file.""); // ??
unlink($center_path."/".$file.""); // ??
rmdir($center_path."/coach".$coach_id."/"); // ??
rmdir($center_path."/"); // ??
?>
try doing from command prompt (change $center_path with actual value):
Takuma
Forum Regular
Posts: 931 Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:
Post
by Takuma » Sat Sep 07, 2002 11:55 am
Isn't that what I said...
Oh well yours have more details
rainbow
Forum Newbie
Posts: 13 Joined: Thu Aug 22, 2002 6:03 am
Location: Finland
Post
by rainbow » Sat Sep 07, 2002 2:26 pm
Thanx people for replies!
I guess i figured out what was the problem. The problem was with while loop. Because actually sometimes it needs to run few times to delete few folders. But it occured so that it ran across empty or none existing folders, because they have been already deleted at first run of loop. And that's why those warnings appeared. Anyways here is a fixed version of code:
Code: Select all
<?phpglobal $centre_id;
$centre_id = 16;
if(!($result11 = mysql_query("SELECT ct_centre_id, ct_centre_name from ct_centre WHERE ct_centre_id = '$centre_id'", $con)))
{
Error(sprintf("Internal Error %d:%s\n",mysql_errno(),mysql_error()));
exit();
}
$row11 = mysql_fetch_array($result11);
$centre_id = $row11ї"ct_centre_id"];
$centre_name = $row11ї"ct_centre_name"];
$center_path = "".$cdc_path."/centre".$centre_id."";
chmod($center_path, 0777);
if(!($result12 = mysql_query("SELECT student_id from ct_centre_coaches WHERE ct_centre_id = '$centre_id'", $con)))
{
Error(sprintf("Internal Error %d:%s\n",mysql_errno(),mysql_error()));
exit();
}
$num_results = mysql_num_rows($result12);
$count = 0;
$id= 0;
for ($i=0; $i<$num_results; $i++)
{
$row12 = mysql_fetch_array($result12);
$coach_id = $row12ї"student_id"];
if(!($result13 = mysql_query("SELECT ct_cdc_deliv_topic_id from ct_cdc_deliv_topic WHERE ct_centre_id = '$centre_id'", $con)))
{
Error(sprintf("Internal Error %d:%s\n",mysql_errno(),mysql_error()));
exit();
}
// Delete entries from deliverable tables.
while($row13 = mysql_fetch_array($result13))
{
$ct_topic_id = $row13ї"ct_cdc_deliv_topic_id"];
// delete topic folders and their contents
$dir2remove = "".$cdc_path."/centre".$centre_id."/coach".$coach_id."/topic".$ct_topic_id."/version".$version."/";
if ($dir = @opendir("$dir2remove"))
{
while (($file = readdir($dir)) != false)
{
if (!($file=="." || $file==".."))
{
// chdir($center_path."/coach".$coach_id."/topic".$ct_topic_id."/version".$version."/");
unlink($center_path."/coach".$coach_id."/topic".$ct_topic_id."/version".$version."/".$file."");
// chdir($center_path."/coach".$coach_id."/topic".$ct_topic_id."/");
unlink($center_path."/coach".$coach_id."/topic".$ct_topic_id."/".$file."");
}
} //close while
closedir($dir);
rmdir($center_path."/coach".$coach_id."/topic".$ct_topic_id."/version".$version."/");
rmdir($center_path."/coach".$coach_id."/topic".$ct_topic_id."/");
}
}
// delete coach folders and their contents
$dir2remove = "".$cdc_path."/centre".$centre_id."/coach".$coach_id."/";
if ($dir = @opendir("$dir2remove"))
{
while (($file = readdir($dir)) != false)
{
if (!($file=="." || $file==".."))
{
unlink($center_path."/coach".$coach_id."/".$file."");
}
} //close while
closedir($dir);
rmdir($center_path."/coach".$coach_id."/");
}
}
// delete centre folder and its contents
$dir2remove = "".$cdc_path."/centre".$centre_id."/";
if ($dir = @opendir("$dir2remove"))
{
while (($file = readdir($dir)) != false)
{
if (!($file=="." || $file==".."))
{
unlink($center_path."/".$file."");
}
} //close while
closedir($dir);
rmdir($center_path."/");
$stat = "center has been deleted";
}
echo "Status: $stat";
?>
Takuma
Forum Regular
Posts: 931 Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:
Post
by Takuma » Sat Sep 07, 2002 2:29 pm
lol, the good this is that you have found the problem by yorself.
P.S. This also mean that I'm useless
rainbow
Forum Newbie
Posts: 13 Joined: Thu Aug 22, 2002 6:03 am
Location: Finland
Post
by rainbow » Sat Sep 07, 2002 3:14 pm
to Takuma:
This also mean that I'm useless
Don't worry.
Actually the idea about the problem came to my mind after you said , that i don't need to use chdir.
So, you are not useless
Takuma
Forum Regular
Posts: 931 Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:
Post
by Takuma » Sat Sep 07, 2002 3:29 pm
OK,