MkDir Failed creating directory PHP

☆樱花仙子☆ 提交于 2019-12-13 03:22:57

问题


I am trying to create a directory using PHP this works:

<?php
$uid = "user_615";
$thisdir = getcwd(); 

if(mkdir($thisdir ."/userpics/" . $uid , 0777)) 
{ 
   echo "Directory has been created successfully..."; 
} 
else 
{ 
   echo "Failed to create directory..."; 
} 
?>

but this does not work

<?php
session_start();
$uid = $_SESSION['username'];
$thisdir = getcwd(); 

if(mkdir($thisdir ."/userpics/" . $uid , 0777)) 
{ 
   echo "Directory has been created successfully..."; 
} 
else 
{ 
   echo "Failed to create directory..."; 
} 
?>

Yes the session variable is populated with the exact same thing as above 'user_615' so why would the second one be failing?

EDIT:

So I took the suggestion of @stefgosselin and re-designed the code to be

<?php
session_start();
$uid = $_SESSION['username'];
$thisdir = getcwd() . "/userpics" . $uid; 

if(mkdir($thisdir , 0777)) 
{ 
   echo "Directory has been created successfully..."; 
} 
else 
{ 
   echo "Failed to create directory..."; 
   echo "Your thisdir Variable is:'" . $thisdir . "'" ;
} 
?>

And the output is

Failed to create directory...Your thisdir Variable is:'/unified/b/bis/www.mysite.com/jou/userpics/user_615

Any other ideas on what would cause the a Session variable not to be able to used in creating a directory?


回答1:


As a small tip, I would simply put all of $thisdir in a variable and check if the output of that adds up to the result you are expecting.

IE: Having $thisdir ."/userpics/" . $uid defined in a variable would give you the possibility to easily output and validate the argument value you are passing to mkdir.

Edit: Adjusted minor phrasing for better english translation. Sorry above wasn't clear, Wesley understood the simple point I was clumsily trying to make.



来源:https://stackoverflow.com/questions/7101840/mkdir-failed-creating-directory-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!