如果没有将递归设置为true,为什么mkdir无法工作?

肯尼斯·J

我在摆弄我的代码,尝试编写一些代码,这些代码将通过POST从表单上载数据(包括图片),然后将创建一个包含相关子目录的目录来存储图像。

在编写代码时,我不断收到错误消息

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

但是,当我将mkdir的递归设置为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,因此我的代码可以正常工作了,但是我仍然不知道为什么它可以正常工作,所以如果有人可以解释为什么我的代码确实可以正常工作,我将非常感激。

我最接近的是为什么mkdir在递归选项设置为true时失败?

虽然我不明白链接中所说的任何内容。

谢谢!

约阿尼斯·拉洛普洛斯(Ioannis Lalopoulos)

mkdir()既然你问它来创建嵌套的目录不存在的,即需要递归设置为true:

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

尽管将enumerable设置为true,为什么没有显示带有defineProperty的属性?

来自分类Dev

为什么我的布尔变量没有设置为 True?

来自分类Dev

为什么没有将setuid位设置为'nethogs'?

来自分类Dev

如果没有if语句的else块,为什么这段代码无法正常工作?-Python

来自分类Dev

如果将网络仅设置为IPv6,为什么某些DNS地址无法解析?

来自分类Dev

在php中,当CURLOPT_SSL_VERIFYPEER设置为true时,如果没有设置CURLOPT_CAPATH会发生什么

来自分类Dev

如果您已经拥有资源,为什么将ETag设置为必须的要求?

来自分类Dev

为什么使用!0将值设置为true

来自分类Dev

为什么Solr默认将Multivalued设置为true?

来自分类Dev

为什么使用!0将值设置为true

来自分类Dev

没有双引号变量,BASH脚本mv命令将无法工作。为什么?

来自分类Dev

尽管将透明设置为“true”,但没有透明背景。

来自分类Dev

在Android中将autoConnect设置为true时,为什么应用程序没有重新连接到BLE设备?

来自分类Dev

当在UWP ListView上将CanReorderItems设置为true时,为什么没有得到DragItemsCompleted和DragItemsStarting事件?

来自分类Dev

为什么Windows任务栏自定义任务列表如果没有在Windows 10上固定就无法工作?

来自分类Dev

如果没有registerReceiver()方法来注册他,为什么用于检查电池状态的BroadcastReceiver无法工作?

来自分类Dev

为什么ProgressDialog的背景没有设置为透明?

来自分类Dev

为什么没有为我设置OnTouchListener?

来自分类常见问题

将largeHeap设置为true有什么好处?

来自分类Dev

将变量设置为True或False有什么意义?

来自分类Dev

为什么我的Chrome设置中没有“将Google Chrome设置为默认浏览器”按钮?

来自分类Dev

如果没有打开的RDP连接,为什么Python无法在VM上设置Excel打印区域?

来自分类Dev

检查变量是否为true,false,如果没有则设置为null

来自分类Dev

为什么将空检查后将int设置为0认为没有意义?

来自分类Dev

为什么只有在设计时将TForm.Position设置为poDefault时,TForm.SetBounds才能正常工作

来自分类Dev

为什么在没有特殊代码路径的情况下将LOCAL_ARM_NEON设置为双倍速度?

来自分类Dev

始终设置* {margin:0; padding:0}是一个好主意吗?如果是这样,为什么浏览器没有将其设置为默认值?

来自分类Dev

ENOENT:没有这样的文件或目录,带有{递归:true}的mkdir

来自分类Dev

如果没有,则无法正常工作

Related 相关文章

  1. 1

    尽管将enumerable设置为true,为什么没有显示带有defineProperty的属性?

  2. 2

    为什么我的布尔变量没有设置为 True?

  3. 3

    为什么没有将setuid位设置为'nethogs'?

  4. 4

    如果没有if语句的else块,为什么这段代码无法正常工作?-Python

  5. 5

    如果将网络仅设置为IPv6,为什么某些DNS地址无法解析?

  6. 6

    在php中,当CURLOPT_SSL_VERIFYPEER设置为true时,如果没有设置CURLOPT_CAPATH会发生什么

  7. 7

    如果您已经拥有资源,为什么将ETag设置为必须的要求?

  8. 8

    为什么使用!0将值设置为true

  9. 9

    为什么Solr默认将Multivalued设置为true?

  10. 10

    为什么使用!0将值设置为true

  11. 11

    没有双引号变量,BASH脚本mv命令将无法工作。为什么?

  12. 12

    尽管将透明设置为“true”,但没有透明背景。

  13. 13

    在Android中将autoConnect设置为true时,为什么应用程序没有重新连接到BLE设备?

  14. 14

    当在UWP ListView上将CanReorderItems设置为true时,为什么没有得到DragItemsCompleted和DragItemsStarting事件?

  15. 15

    为什么Windows任务栏自定义任务列表如果没有在Windows 10上固定就无法工作?

  16. 16

    如果没有registerReceiver()方法来注册他,为什么用于检查电池状态的BroadcastReceiver无法工作?

  17. 17

    为什么ProgressDialog的背景没有设置为透明?

  18. 18

    为什么没有为我设置OnTouchListener?

  19. 19

    将largeHeap设置为true有什么好处?

  20. 20

    将变量设置为True或False有什么意义?

  21. 21

    为什么我的Chrome设置中没有“将Google Chrome设置为默认浏览器”按钮?

  22. 22

    如果没有打开的RDP连接,为什么Python无法在VM上设置Excel打印区域?

  23. 23

    检查变量是否为true,false,如果没有则设置为null

  24. 24

    为什么将空检查后将int设置为0认为没有意义?

  25. 25

    为什么只有在设计时将TForm.Position设置为poDefault时,TForm.SetBounds才能正常工作

  26. 26

    为什么在没有特殊代码路径的情况下将LOCAL_ARM_NEON设置为双倍速度?

  27. 27

    始终设置* {margin:0; padding:0}是一个好主意吗?如果是这样,为什么浏览器没有将其设置为默认值?

  28. 28

    ENOENT:没有这样的文件或目录,带有{递归:true}的mkdir

  29. 29

    如果没有,则无法正常工作

热门标签

归档