简体   繁体   中英

remove extension from filename, but keep extension associated to file

I have been having difficulty loading an .xlsx into a directory and not having .xlsx attached to the filename, but keeping it assocaited to the .xlsx file type. This is as close as I could get

$maintenance_dir = "C:/wamp/www/mafrd/maintenance/$siteNAME/";
$maintenance_file = $maintenance_dir . basename($_FILES["loadtrip"] ["name"], ".xlsx") .'_'. date("Y-m-d");
if (!file_exists($maintenance_dir)) {
mkdir($maintenance_dir, 0777, true);
}
$uploadOk = 1;

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES['loadtrip']['tmp_name'], $maintenance_file)) {
    echo "The file ". basename( $_FILES["loadtrip"]["name"], ".xlsx") .'_'. date("Y-m-d"). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}
}  

在此处输入图片说明

It currently comes out as the orange highlighted and I would like it to mirror the yellow.

Under Windows, the file type is associated with the filename. You can't remove the .xlsx and expect it to behave as normal.

The solution I would recommend you is to add the date before the extension:

 $maintenance_file = $maintenance_dir . basename($_FILES["loadtrip"] ["name"], ".xlsx") .'_'. date("Y-m-d") . ".xlsx";

If this is not what you're looking for, you could create a different extension and map it to Excel.

You can still open the file with Excel even if it has a different extension, but it may not open automatically when clicking on the item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM