我正在摆弄我的代码,试图编写一些代码来获取通过 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 failed with recursive option set true?
虽然我无法理解链接中所说的任何内容。
谢谢!