PHP fopen不使用目录变量

I have a php code that it creates a random name, then creates the directory, and then I need to create and write a text file inside the directory. It creates the directory, but nothing when it need to create the text file. This is what I have:

This create the random name

$letra1 = chr(rand(65,90));
$letra2 = chr(rand(97,122));
$letra3 = chr(rand(65,90));
$letra4 = chr(rand(97,122));

This join all the characters:

$letras = $letra1 . $letra2 . $letra3 . $letra4;

This creates de directory:

mkdir("a/$letras", 777);

And here, where i think I have the problem, the fopen:

$archt = fopen("a/$letras/text.txt", "w") or die("");
$txt = "";
fclose($archt);

I think it isn't a perms problem, I have given 777 to all directories...

I have tried a lot of stuf that I have been searched from stackoverflow but nothing is working for me and don't know what it will be?

Since the "$letras" hides the variable

$dirLocation = "a/" . $letras;

mkdir($dirLocation, 0777);

$fileLocation = $dirLocation . "/text.txt";

$archt = fopen($fileLocation, "w") or die("");

$txt = "";

fclose($archt);

As I stated in my comment to the questions: you are passing a decimal to the mkdir function as apposed to passing an octal which is what is required. The permissions are not being set correctly as a result.

With the interpreter (php) setup correctly for a development environment your code produced the following error:

PHP Warning: fopen(a/DeOq/text.txt): failed to open stream: Permission denied in /home/dave/Desktop/test/test.php on line 11

To avoid having to dig through comments for details:

The mkdir() documentation here: http://php.net/manual/en/function.mkdir.php refers to the chmod() directory for explanation of mode. Mode is required to be an octal

Note that mode is not automatically assumed to be an octal value, so to ensure the expected operation, you need to prefix mode with a zero (0). Strings such as "g+w" will not work properly.

http://php.net/manual/en/function.chmod.php

In addition to changing 777 to 0777 in the mkdir() function, ensure that the "a" directory actually exists as the mkdir() function will not make a directory "tree". "a" is required to already exist in order to make a directory under it.

This code is tested and works:

<?php
$letra1 = chr(rand(65,90));
$letra2 = chr(rand(97,122));
$letra3 = chr(rand(65,90));
$letra4 = chr(rand(97,122));

$letras = $letra1 . $letra2 . $letra3 . $letra4;

mkdir("a/$letras", 0777);

$archt = fopen("a/$letras/text.txt", "w") or die("Death");
$txt = "";
fclose($archt);

You need to give permissions to the sub-directories too.

<?php
    $letra1 = chr(rand(65,90));
    $letra2 = chr(rand(97,122));
    $letra3 = chr(rand(65,90));
    $letra4 = chr(rand(97,122));
    $letras = $letra1 . $letra2 . $letra3 . $letra4;


    mkdir("a/$letras", 0777, true); //third parameter is important if you want to add perms recursively 


    $archt = fopen("a/".$letras."/text.txt", "w")or die(print_r(error_get_last(),true));

    $txt = "";

    fclose($archt);

    ?>