0
我有當圖像被粘貼到一個CONTENTEDITABLE場檢測的JavaScript代碼:文件擴展名
$image = $_FILES['newImage'];
$imageFileType = pathinfo($image["name"],PATHINFO_EXTENSION); // obviously doesn't work
$imageId = new ImageId();
$imageName = $imageId.".".$imageFileType;
// [... some checks on the file ...]
$targetFile = "../../images/uploads/{$imageName}";
if($imageOK && move_uploaded_file($image["tmp_name"],$targetFile)){
//[ do stuff ]
:接收用於圖像POST請求
document.onpaste = function(event){
var items=(event.clipboardData || event.originalEvent.clipboardData || event.dataTransfer).items;
for(i in items){
var item=items[i];
if(item.kind==='file' && ~item.type.indexOf("image")){ //is file and is image
event.preventDefault();
var blob=item.getAsFile();
console.log(item, blob);
var data=new FormData();
data.append('fname','imageFromClipboard'); //extension for file name??
data.append('newImage',blob);
sendImage(data); //just a $.POST request
}
}
}
和PHP代碼如何可靠地從blob獲取文件擴展名?我應該用JS還是PHP來做?是否所有可能的圖像文件類型只是在斜線處分割MIME類型(例如「image/png」)並將其用作擴展名?