Couple 'o php questions.
- How can I tell what files/folders are in a directory?
- How can I tell if a file exists without making two calls to fopen()? (assuming I then want to open it for read)
Couple 'o php questions.
So, I’m trying to remove a directory here…
Windows XP system running Apache 2.0.50 with PHP 4.3.9. (Yes, I know that 2.0.52 is out. Upgrade is coming soon.) I’m trying to remove a directory which is located relative to the current one at …/pages/<dir>. I’m able to delete all the files in the directory, but on trying to remove the directory itself I get a “Permission denied” error.
Here’s the exact code which applies:
...
// Ensure the directory only contains the struct file (and the current and parent dir references)
if(!(count(scandir("$dir/pages/$_POST[cat]")) <= 3))
{
header('Location: /index.php?section=admin&page=failure');
die();
}
// Remove struct file
if(file_exists("$dir/pages/$_POST[cat]/struct") && !unlink("$dir/pages/$_POST[cat]/struct"))
{
header('Location: /index.php?section=admin&page=failure');
die();
}
// Remove the directory
if(!rmdir("$dir/pages/$_POST[cat]"))
{
header('Location: /index.php?section=admin&page=failure');
die();
}
...
The struct file is just a file which is used by other parts of the site to maintain the directory structure. (The entire site is just a pre-alpha type thing for learning PHP and eventually some of the code will go into our team’s site. The main thing is a user can login and edit content pages over the web.) The struct file does get deleted successfully; I see it disappear in my little Windows Explorer window. $dir contains a path which is brings the path back to the root of the site before going deeper in. In this case, it equals “./…”. (The reason for the ./ at the beginning is how I generated it. I was too lazy to remove that part of it, as it shouldn’t make a difference. Does it?)
I’m using pretty much the ship configuration which is in the php.ini-dist.recommended (or whatever that file was called) file.
Ideas? Was I unclear?
And maybe this is related:
Using the code below (I had something in a different file, but I wrote this stuff out just for testing. I get the same results with both things.) I get… nothing. The page just appears again without the slightest hint it did anything. Actually, on the other one, I have more error checking and it catches that the move_uploaded_file() fails and reports it appropriately.
Here’s the test code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<?php
if(isset($_POST'image']))
{
echo $_FILES'image']'tmp_name'];
move_uploaded_file($_FILES'image']'tmp_name'], "./$_POST[name]");
echo "<img src='./$_POST[name]' />";
echo "<img src='./$_FILES[image][tmp_name]' />";
}
?>
<form method="post" action="test.php" enctype="multipart/form-data">
<table>
<tr>
<td>File:</td>
<td><input type="file" name="image" /><input type="hidden" name="MAX_FILE_SIZE" value="40000" /></td>
</tr>
<tr>
<td>Save as:</td>
<td><input type="text" name="name" /></td>
</tr>
</table>
<input type="submit" value="Upload" />
</form>
</body>
</html>
–EDIT–
Acutally, after adding the enctype=“multipart/form-data” to the form, I now get nothing. It just reloads.