問(wèn)題描述
當(dāng)我測(cè)試帶有此擴(kuò)展名 .JPG 的圖像文件時(shí),我正在制作上傳腳本,我不知道 jpg 或 jpeg 之間有什么區(qū)別,但似乎 $_FILES 無(wú)法識(shí)別這種文件類型.>
我讀過(guò)幾個(gè)線程,說(shuō) $_FILES 在 mime 類型方面并不可靠,所以我決定使用 php 的 mime 類型函數(shù) mime_content_type()
,php 的 getimagesize()
, pathinfo()
,雖然路徑信息返回文件名和類型,但我需要不存在的文件路徑,所有函數(shù)都通過(guò) $_FILES['file']['tmp_name'] 作為參數(shù).
所以當(dāng)我決定上傳一個(gè)圖像文件時(shí)出現(xiàn)了這個(gè)問(wèn)題,例如 sample.JPG,我認(rèn)為這些文件中的大部分都是來(lái)自相機(jī)的原始文件 <-- 這就是我的想法,但更重要的是我可以上傳 .JPG、.jpg、jpeg、.png.除了 .JPG 之外,所有這些都可以正常工作.
主要問(wèn)題是在上傳.JPG 時(shí)$_FILES 中的字段['tmp_name'] 沒有值.
遇到此問(wèn)題的任何人都請(qǐng)分享您的解決方法或您是如何做到的"之類的事情.
如果 $_FILES[$field]['tmp_name']
為空,則文件尚未上傳.您應(yīng)該查看 $_FILES[$field]['error']
以了解原因.
FWIW,據(jù)我所知,$_FILES[]
中的 MIME 類型是由瀏覽器提供的.
更新:這里有一些處理所有文件上傳錯(cuò)誤的代碼:
$message = '上傳文件時(shí)出錯(cuò)';開關(guān)($_FILES['newfile']['error']){情況 UPLOAD_ERR_OK:$消息=假;;休息;案例 UPLOAD_ERR_INI_SIZE:案例 UPLOAD_ERR_FORM_SIZE:$message .= ' - 文件太大('.get_max_upload().'字節(jié)限制).';休息;案例 UPLOAD_ERR_PARTIAL:$message .=' - 文件上傳未完成.';休息;案例 UPLOAD_ERR_NO_FILE:$message .=' - 上傳的文件長(zhǎng)度為零.';休息;默認(rèn):$message .= ' - 內(nèi)部錯(cuò)誤 #'.$_FILES['newfile']['error'];休息;}如果(!$消息){if(!is_uploaded_file($_FILES['newfile']['tmp_name'])) {$message = '上傳文件時(shí)出錯(cuò) - 未知錯(cuò)誤.';} 別的 {//讓我們看看我們是否可以移動(dòng)文件...$dest .='/'.$this_file;if( !move_uploaded_file($_FILES['newfile']['tmp_name'], $dest) ) {//沒有錯(cuò)誤支持,所以我們可以看到潛在的錯(cuò)誤.$message = '上傳文件時(shí)出錯(cuò) - 無(wú)法保存上傳(這可能是 '.$dest.' 中的權(quán)限問(wèn)題)';} 別的 {$message = '文件上傳正常.';}}}
i was making an upload script when i tested an image file wit this extension .JPG, i don't know whats the difference between jpg or jpeg, but it seems that $_FILES don't recognize this file type.
I've read several threads that $_FILES ins't that reliable when it comes to mime type, so i decided to used the php's mime type function mime_content_type()
, php's getimagesize()
, pathinfo()
, though pathinfo returns a file name, and type, but i need the path of the file which is NOT present, all of the functions are being passed with $_FILES['file']['tmp_name'] as parameters.
So this problem came up when i decided to upload an image file e.g sample.JPG, i think most of this files are raw from the camera <-- that's what i think though but nevertheless what is more important is that i can upload them .JPG, .jpg, jpeg, .png. all of them works fine except for .JPG.
Main problem is that field ['tmp_name'] in $_FILES has no values when .JPG is to be uploaded.
Any of you guys who have encountered this problem please do share your workaround or "how did you do it" kind of thing.
If $_FILES[$field]['tmp_name']
is empty then the file hasn't been uploaded. You should look at $_FILES[$field]['error']
to see why.
FWIW, and as far as I understand it, the mime-type in $_FILES[]
is provided by the browser.
Update: here is a bit of potted code to handle all file upload errors:
$message = 'Error uploading file';
switch( $_FILES['newfile']['error'] ) {
case UPLOAD_ERR_OK:
$message = false;;
break;
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
$message .= ' - file too large (limit of '.get_max_upload().' bytes).';
break;
case UPLOAD_ERR_PARTIAL:
$message .= ' - file upload was not completed.';
break;
case UPLOAD_ERR_NO_FILE:
$message .= ' - zero-length file uploaded.';
break;
default:
$message .= ' - internal error #'.$_FILES['newfile']['error'];
break;
}
if( !$message ) {
if( !is_uploaded_file($_FILES['newfile']['tmp_name']) ) {
$message = 'Error uploading file - unknown error.';
} else {
// Let's see if we can move the file...
$dest .= '/'.$this_file;
if( !move_uploaded_file($_FILES['newfile']['tmp_name'], $dest) ) { // No error supporession so we can see the underlying error.
$message = 'Error uploading file - could not save upload (this will probably be a permissions problem in '.$dest.')';
} else {
$message = 'File uploaded okay.';
}
}
}
這篇關(guān)于$_FILES 字段 'tmp_name' 對(duì) .JPG 文件擴(kuò)展名沒有值的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!