我想上传图片,并在用户的全名后重命名。这很好但我希望通过添加一个数字来命名它们,例如。 Fname_Lname_1.jpg
,Fname_Lname_2.jpg
等。我的问题是,数字增量不会发生。相反发生的是第一个文件被新上传的文件替换,新上传的文件假定第一个文件的名称。
<?php
require 'Connections/Conn.php';
require 'core/init.php';
if(isset($_SESSION["UserID"])){
}else{
header('Location: Login.php');
}
//Parse UserId into variable $userid
$userid = $_SESSION["UserID"];
//Parse User Fname, '_' & Lname into variable userfullname
$userfullname = $_SESSION["LName"].'_'.$_SESSION["FName"];
//Add UserID and userfullname and parse it into foldername
$foldername = $userid.'_'.$userfullname;
//Now save the path into this variable
$enclosedFolder="./Users/" . $foldername . "/Main/";
// Count sent by plugin
$Images =count(isset($_FILES['images']['name'])?$_FILES['images']['name']:0);
$infoImagesUploaded = array();
for($i = 0; $i < $Images; $i++) {
// The name and the temporary file name we're gonna attach
$fileName=isset($_FILES['images']['name'][$i])?$_FILES['images']['name'][$i]:null;
$fileTemp=isset($_FILES['images']['tmp_name'][$i])?$_FILES['images']['tmp_name'][$i]:null;
$numb = 0;
$newfilename = $userfullname.'_'.($numb++).".jpg";
$pathFile=$enclosedFolder.$newfilename;
move_uploaded_file($fileTemp,$pathFile);
$infoImagesUploaded[$i]=array("caption"=>"$fileName","height"=>"120px","url"=>"MainDelete.php","key"=>$fileName);
$ImagesUploaded[$i]="<img height='120px' src='$pathFile' class='file-preview-image'>";
}
$arr = array("file_id"=>0,"overwriteInitial"=>true,"initialPreviewConfig"=>$infoImagesUploaded, "initialPreview"=>$ImagesUploaded);
echo json_encode($arr);
?>