.fit()
$date = date('m-d-Y_H:i:s');
if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
// Allow certain file formats
//$newfilestring = $date.$lastname.$firstname;
$targetDir = "C:/xampp/htdocs/uploaded-file/";
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir.$fileName;
$extension = pathinfo($targetFilePath,PATHINFO_EXTENSION);
$allowTypes = array('jpg','png','jpeg','gif','pdf','xlsx');
$empid = $_SESSION['id'];
if(in_array($extension, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
// Insert image file name into database
$insert = $con->query("INSERT into fileupload (filename, dateuploaded,empid) VALUES ('".$fileName."', NOW(), '".$empid."')");
if($insert){
$statusMsg = "Your timesheet: ".$fileName. " has been uploaded successfully.";
}else{
$statusMsg = "File upload failed, please try again.";
}
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}else{
$statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, xlsx & PDF files are allowed to upload.';
}
}else{
$statusMsg = 'Please select a file to upload.';
}
这是php&html文件。截至目前,我能够使用文件上传名称进行上传。我的目标是将文件名更改为“ newfilestring”的格式(已注释掉),然后添加已上传文件的扩展名。我已经测试了多种方法和教程,但没有一个起作用。任何反馈将不胜感激。
当前格式-> example.pdf
所需格式-> date-firstname-lastname-extension
答案 0 :(得分:0)
您可以使用
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
$targetFilePath = $targetDir.$date.'-'.$firstName.'-'.$lastname.'.'.$extension;
重命名文件。
答案 1 :(得分:0)
添加类似这样的内容:
$newFileName = $date.$lastname.$firstname.'.'.$extension;
//如果这些变量已在其他位置提供
然后在移动文件时使用它:
if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetDir.$newFileName )){
//等等
尚未对此进行测试,但是希望可以将您带入正确的轨道...
已更新:尝试以下操作:
$date = date('m-d-Y');
if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
// Allow certain file formats
$targetDir = "C:/xampp/htdocs/uploaded-file/";
$fileName = basename($_FILES["file"]["name"]);
$filename_array = explode(".",$fileName ); // get file name
$extension = $filename_array[1]; // second portion of file name - extension
$targetFilePath = $targetDir.$fileName;
$newFileName = $date.$lastname.$firstname.'.'.$extension;
$allowTypes = array('jpg','png','jpeg','gif','pdf','xlsx');
$empid = $_SESSION['id'];
if(in_array($extension, $allowTypes)){
// Upload file to server
if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetDir.$newFileName )){
// Insert image file name into database
$insert = $con->query("INSERT into fileupload (filename, dateuploaded,empid) VALUES ('".$fileName."', NOW(), '".$empid."')");
if($insert){
$statusMsg = "Your timesheet: ".$fileName. " has been uploaded successfully.";
}else{
$statusMsg = "File upload failed, please try again.";
}
}else{
$statusMsg = "Sorry, there was an error uploading your file.";
}
}else{
$statusMsg = 'Sorry, only JPG, JPEG, PNG, GIF, xlsx & PDF files are allowed to upload.';
}
}else{
$statusMsg = 'Please select a file to upload.';
}