Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4116972
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:46:37+00:00 2026-05-20T22:46:37+00:00

Assume we have the following tree list: www _ \_sources_ \ \_dir1 \ \_dir2

  • 0

Assume we have the following tree list:

www _
     \_sources_
      \        \_dir1
       \        \_dir2
        \        \_file
         \_cache

I’m trying to recursively parse each file in the “sources” and copy it into “cache” folder saving the hierarchy, but in my function mkdir() creates a file instead of directory. Outside the function, mkdir() works properly. Here is my function:

function extract_contents ($path)  {
    $handle = opendir($path);
    while ( false !== ($file = readdir($handle)) ) {
    if ( $file !== ".." && $file !== "." ) {
        $source_file = $path."/".$file;
        $cached_file = "cache/".$source_file;
        if ( !file_exists($cached_file) || ( is_file($source_file) && (filemtime($source_file) > filemtime($cached_file)) ) ) {
            file_put_contents($cached_file, preg_replace('/<[^>]+>/','',file_get_contents($source_file)) ); }
        if ( is_dir($source_file) ) {
#  Tried to save umask to set permissions directly – no effect
#           $old_umask = umask(0);
            mkdir( $cached_file/*,0777*/ );
            if ( !is_dir( $cached_file ) ) {
                echo "S = ".$source_file."<br/>"."C = ".$cached_file."<br/>"."Cannot create a directory within cache folder.<br/><br/>"; 
                exit;
                }
# Setting umask back
#           umask($old_umask); 
            extract_contents ($source_file);
            }              
        }
    }
    closedir($handle);
}
extract_contents("sources");

PHP debug gives me nothing but
[phpBB Debug] PHP Notice: in file /var/srv/shalala-tralala.com/www/script.php on line 88: mkdir() [function.mkdir]: ???? ??????????
There is no other lines which are containing mkdir().

ls -l cache/sources looks like
-rw-r--r-- 1 apache apache 8 Mar 31 08:46 file
-rw-r--r-- 1 apache apache 0 Mar 31 08:46 dir1
It’s obvious, that mkdir() creates a directory, but it doesn’t set “d” flag for it. I just can’t understand, why. So at first time, can somebody help and tell me, how to set that flag through octal permissions via chmod(), while i don’t see any better solution? (I’ve already seen man 2 chmod and man 2 mkdir, there is nothing about “d” flag)

addition:
Solved by changind the second if condition to
if ( (!file_exists($cached_file) && is_file($source_file)) || ( is_file($source_file) && (filemtime($source_file) > filemtime($cached_file)) ) )

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T22:46:38+00:00Added an answer on May 20, 2026 at 10:46 pm

    You are using this :

    file_put_contents($cached_file, preg_replace('/<[^>]+>/','',file_get_contents($source_file)) ); }
    

    Which creates a file called $cached_file.

    And, then, calling that :

    mkdir( $cached_file/*,0777*/ );
    

    There, you try to create a directory called $cached_file.

    But there is already an existing file with that name.

    Which means :

    • mkdir fails, as there is an file with that name
    • and you have a file, the one you created previously with file_put_contents.


    Edit after the comment : just as a test, I’ll try creating a file, and a directory with the same name — using command-line, and not from PHP, to make sure PHP doesn’t have any impact on this.

    First let’s create a file :

    squale@shark: ~/developpement/tests/temp/plop 
    $ echo "file" > a.txt
    squale@shark: ~/developpement/tests/temp/plop 
    $ ls
    a.txt
    

    And, now, I try creating a directory with the same name a.txt :

    squale@shark: ~/developpement/tests/temp/plop 
    $ mkdir a.txt
    mkdir: impossible de créer le répertoire «a.txt»: Le fichier existe
    

    The error message (sorry, my system is in french) says “impossible to create the directory a.txt : the file already exists”

    So, are you sure you can create a directory with the same name as an existing file ?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I have the following information in a flat format, for example a list
Assume we have following tree: 1 9 2 13 3 10 4 15 5
Assume I have the following string: Hellotoevryone<img height=115 width=150 alt= src=/Content/Edt/image/b4976875-8dfb-444c-8b32-cc b47b2d81e0.jpg />Iamsogladtoseeall. This
Assume I have the following input in Pig: some And I would like to
Assume I have the following style table, col1 col2 and col3 have same value
Assume I have the following classes class Genre { static hasMany=[author:Author] } class Author{
Lets assume I have the following 3 entities: Customer,Order,Product which interact in the View
Lets assume we have the following code: abstract class Base1 { protected int num;
Let's assume we have the following structure: index.php config.inc.php \ lib \ lib \
Let's assume I have the following code: public class MainClass { public static void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.