Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Folder creation and removal 2

Status
Not open for further replies.

newbiepg

Programmer
Joined
Nov 6, 2002
Messages
181
Location
IN
I need to create a folder with a unique name everytime a user submits some data for upload. If the administrator decides to reject the submission , the folder with all its contents should be deleted.

How can I do this safely in php 4.11?
I am thinking of giving a timestamp for the folder name , I am confused about the rest.
What permissions should I set?
How do we remove a folder with all its contents?
 
The function for making a directory is mkdir() ( The function for removing a directory is rmdir() ( [caveat: rmdir will only remove a empty directory. You'll have to empty it using rmdir() and unlink() on subelements before you can remove it]. unlink() ( will remove a file. You could also issue the remove commands through external execution operations (
The permissions you will require and the way to set them are dependent on the OS on which you are running PHP.

PHP is going to place all file uploads in the defined temporary. You will have to create the scripts necessary to create the holding directory, use the information provided by PHP in the $_FILES array to move the files to the holding directory. For more info:
Want the best answers? Ask the best questions: TANSTAAFL!
 
sleipnir
thanks
I still have a problem, the user is given a choice to upload upto 3 photographs
It seems that before I can delete the directory, I have to empty it
I am going to name the photos serially e.g. a.jpg, b.jpg and c.jpg
So how will I make the code to check if these files exist
in the folder before I unlink them?
 
u'll do something like:

$dirname = "./dir that u want to delete";

if ($dir = opendir($dirname)) {
while ($file = readdir($dir)) {
if (($file != ".") && ($file != "..")) {
unlink($file); // deletes all files inside dir. caution, the dir may not contain folders, as they are seen as normal files and unlinking them won't work
}
}
closedir($dir);
}

after u unlinked all files inside $dirname, rmdir it :)
 
Outside of that, you already know what the names of the files are. All that data is provided to you by PHP in the $_FILES superglobal array.

Take a look at the online manual page under "Handling File Uploads". The link is in my earlier post.

Want the best answers? Ask the best questions: TANSTAAFL!
 
jamesp0tter thanks, things are working fine now
sleipnir
thanks to you too
for telling me about the unlink part
$_FILES superglobal array may not work in the 4.11 version
that was why I asked you a second time
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top