php - 为什么 mkdir 在没有将递归设置为 true 的情况下不起作用?

标签 php html mysql unix

我在摆弄我的代码,试图编写一些代码来获取通过 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?

虽然我无法理解链接中所说的任何内容。

谢谢!

最佳答案

mkdir() 需要将递归设置为 true,因为您要求它创建不存在的嵌套目录,即:

$upload_path = 'C:\Users\Admin\Desktop\UniServer\www\images\\'.$foldername.'\\'.$cat.'\\'.$sub.'\\'.$itemname.'\\';

因此,由于变量 $foldername 从用户 session 中获取其值,如果用户 session 更改,它也会更改。其余的 $upload_path 部分也是如此,如果其中的某些部分发生变化,您必须创建整个路径。只有路径的最后一部分 ($itemname) 可以在不使用递归选项的情况下更改。

关于php - 为什么 mkdir 在没有将递归设置为 true 的情况下不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17899296/

相关文章:

javascript - 为什么我的编辑页面在 Laravel 中不起作用?我使用了 vue.js

php - php中静态字符串的两种映射类

php - 比较两个数组的两个值

html - 为什么我不能在我的 div 中居中我的图像?我的 div 居中图像不是

php - 页面底部不明空间

php - 使用PHP获取本地PC中的ODBC数据源列表

php - 简单的 RSS 提要

python - Scrapy 的 XPath 中的 OR 条件

php - $result 的 Codeigniter num_rows() 始终返回 0

mysql - 插入时顺序重要吗?