if(empty($_FILES['img']['name']))
{
$_SESSION['error']['fnm']="Please select Image";
}
else if(!($ext==".JPG"
|| $ext==".PNG"
|| $ext==".GIF"
|| $ext=="JPEG"
|| $ext==".BMP"))
{
$_SESSION['error']['fnm']="Wrong type of image";
}
else if(file_exists("img/".$_FILES['img']['name']))
{
$_SESSION['error']['fnm']="Image Already available";
}
if(!empty($_SESSION['error']))
{
header("location:dbimage.php");
}
else{
$fnm=time()."_".$_FILES['img']['name'];
move_uploaded_file($_FILES['img']['tmp_name'],"img/".$fnm);
$link=mysqli_connect("localhost","root","root","login");
mysqli_select_db($link,"picture");
$q="update picture
set profile_pic='$fnm' where id=1" ;
mysqli_query($link,$q);
header("location:dbimage.php");
}
在这个'img'文件夹中已经存在。我想根据我的time()函数创建文件夹的初始前4位数(想要制作4个文件夹),例如db中的文件名是1518691864_download2.png,我想制作文件夹1/5/1/8然后想要将图像存储在其中,如1/5/1/8/1518691864_download2.png。
答案 0 :(得分:1)
检查我刚刚完成的答案
<?php
$fnm = time()."_".$_FILES['img']['name'];
$str = substr($fnm, 0, 4);
$strlen = strlen($str);
$oldfolder = '';
for($i=$strlen;$i>0;$i--){
$newfoldername = substr($str, -$i,1);
$oldfolder = $oldfolder == '' ? $newfoldername : $oldfolder.'/'.$newfoldername;
if (!file_exists($newfoldername)) {
if($i==$strlen){
mkdir($newfoldername, 0777, true);
} else {
mkdir($oldfolder, 0777, true);
}
}
else
{
echo "Folder already exists <br/>";
}
}
move_uploaded_file($_FILES['img']['tmp_name'],$oldfolder."/".$fnm);
?>