#1
  1. Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Inida - Gujarat - Rajkot
    Posts
    389
    Rep Power
    0

    Question How to Unlink (Remove) Multiple Files???


    Hello Friends

    I have one folder of image Folder name is "products" Now in this folder there is many different files Some are .txt some are .jpg,.gif,.xml etc...

    Now i want to remove all files from the folder.. I want full folder empty.. So how can i remove all files at a time???

    NOTE : I don't want to remove FOLDER..

    I want to remove only all files inside folder...
  2. #2
  3. No Profile Picture
    Loves Diesel Generators
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    Lincs - UK
    Posts
    661
    Rep Power
    84
    create an array containing the filenames, then use foreach in combination with unlink()

    Should work I think.
    If I helped you by some stroke of luck, please add to my reputation by clicking on the icon at the top of my post!
    Perkins Diesel Generators Volvo Diesel Generators xxl.me.uk (my Site)
  4. #3
  5. Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Inida - Gujarat - Rajkot
    Posts
    389
    Rep Power
    0

    Lightbulb I can't create array !!!


    Hello Friend

    I can't create an array.. Because all files are dynamically generating.. Even I also don't know how much file and what is the name of all file...

    So for that I want to remove all files from folders...
  6. #4
  7. No Profile Picture
    Loves Diesel Generators
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    Lincs - UK
    Posts
    661
    Rep Power
    84
    Hang on.. the files are dynamic? You mean they dont exist? If you mean they do not exist then you havn't got aproblem have you?

    Or do you mean they are dynamically generated? If they are generated dymanically and exist in the file system, then use something like this to create and array:

    PHP Code:
    function getdirarray($dir$return_folders) {
    opendir($dir);
    while (
    $indir readdir()) {
    if (
    is_dir($dir.$indir)==false||$return_folders==true&&($indir!='.'&&$indir!='..')) { $array[] = $indir; }
    }
    closedir();
    return 
    $array;

    Thats a function I use on my site. The $return_folders only tell it if it should return the folders in the file list,which you should set to false if you only want a file list.

    Does that cust the mustard for you?
    If I helped you by some stroke of luck, please add to my reputation by clicking on the icon at the top of my post!
    Perkins Diesel Generators Volvo Diesel Generators xxl.me.uk (my Site)
  8. #5
  9. Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Inida - Gujarat - Rajkot
    Posts
    389
    Rep Power
    0

    Question Sorry Friend


    Thanks for the replies...

    Dynamically File is already existing in my folders. And for that Now i want to remove...
    But still I didnt get you..

    Is there anyway which we can do in foreach

    something like

    foreach (...)
    {
    unlink($file);

    // OR //

    unlink("*.jpg");
    }

    I want something like above.. which we can remove easily...
    Is there any way like above ???
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Dec 2003
    Posts
    1,037
    Rep Power
    26
    If you just want to remove all of the files that match a pattern like .jpg you could use glob() to get an array of those files.
  12. #7
  13. No Profile Picture
    Loves Diesel Generators
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    Lincs - UK
    Posts
    661
    Rep Power
    84
    yes, use my above function to list put your files into an array and then use

    PHP Code:
    $array getdirarray($dirfalse);
    foreach (
    $array as $key => $value) {

    unlink($dir.$array[$key]);

    If I helped you by some stroke of luck, please add to my reputation by clicking on the icon at the top of my post!
    Perkins Diesel Generators Volvo Diesel Generators xxl.me.uk (my Site)
  14. #8
  15. Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Inida - Gujarat - Rajkot
    Posts
    389
    Rep Power
    0

    Exclamation Thanx


    Thanx For the Repliess..

    I will try it and reply u...
  16. #9
  17. Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    Inida - Gujarat - Rajkot
    Posts
    389
    Rep Power
    0

    Exclamation I found


    Hello Friends

    I found easiest way which i want...

    it is very simple

    foreach (glob($dirPath."*.*") as $removeFile)
    {
    unlink($removeFile);
    }

    That's it !!!
  18. #10
  19. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Posts
    1,742
    Rep Power
    15
    Should add some error handling to that function or you may end up with a nasty surprise.

    Even just @unlink($removeFile); will help a bit but not always.

    Problem is, if you refresh that script, then there are no more files to unlink and some setups will go into a fit about it and start sending header error message and permissions error messages.
    All totally unrelated to the real problem but hard to see through till you've experienced it a few times.

IMN logo majestic logo threadwatch logo seochat tools logo