1

私は自分のコードをいじっていました.POST経由でフォームからアップロードされたデータ(写真を含む)を取得するコードを書き、関連するサブディレクトリを含むディレクトリを作成して画像を保存しようとしました。

コードを書いている間、私はエラーを受け取り続けました

Warning: mkdir(): No such file or directory in C:\Users\Admin\Desktop\UniServer\www\AddItem.php on line 94

ただし、mkdir の resursion を true に設定すると、mkdir が突然機能し、ディレクトリが問題なく作成されます。

私のコード:

if(isset($_FILES['upload']['tmp_name']))
 {
 $numfile=count($_FILES['upload']['tmp_name']);
{
    for($i=0;$i<$numfile;$i++)
    {
        if(is_uploaded_file($_FILES['upload']['tmp_name'][$i]))
        {
            //Conditionals for uploaded file
            $foldername=$_SESSION['UserId'];
            $cat=$_POST['category'];
            $sub=$_POST['subcat'];
            $itemname=$_POST['itemname'];
            $allowed_filetypes=array('.jpg','.gif','.bmp','.png');
            $max_filesize = 2097152; // Maximum filesize in BYTES (currently 2.0MB).
            $upload_path = 'C:\Users\Admin\Desktop\UniServer\www\images\\'.$foldername.'\\'.$cat.'\\'.$sub.'\\'.$itemname.'\\'; // The place the files will be uploaded to.
            //Checks if Folder for User exists
            //If not, A folder for the user is created to store the user's images
            if(!file_exists($upload_path))
            {
                $upload_path=mkdir($upload_path,0644,true);<-- This is the line
            }

            $filename = $_FILES['upload']['name'][$i]; // Get the name of the file (including file extension).
            $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

            // Check if the filetype is allowed, if not DIE and inform the user.
            if(!in_array($ext,$allowed_filetypes))
            {
                die('The file you attempted to upload is not allowed.');
            }

            // Now check the filesize, if it is too large then DIE and inform the user.
            if(filesize($_FILES['upload']['tmp_name'][$i]) > $max_filesize)
            {
                die('The file you attempted to upload is too large.');
            }

            // Check if we can upload to the specified path, if not DIE and inform the user.
            if(!is_writable($upload_path))
            {
                $errormsg="Image Upload Failed.";
            }

            if(!move_uploaded_file($_FILES['upload']['tmp_name'][$i],"$upload_path" . $filename))
            {
                $errormsg= 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; // It worked.
            }

        }
    }
}




 }
   else{echo"Upload failed";}

再帰をtrueに設定したため、コードは機能していますが、なぜそれが機能しているのか正確には理解できません。

私が最も近いのは、Why mkdir fails with recursive option set true? です。

リンクに書かれていることは理解できませんでしたが。

ありがとう!

4

2 に答える 2