$ _FILES字段“tmp_name”在.JPG文件扩展名上没有值 [英] $_FILES field 'tmp_name' has no value on .JPG file extension

查看:154
本文介绍了$ _FILES字段“tmp_name”在.JPG文件扩展名上没有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个上传脚本时,我测试了一个图像文件,这个扩展名为.JPG,我不知道jpg或者jpeg是什么区别,但是似乎$ _FILES不能识别这个文件类型。 p>

我已经阅读了几个$ _FILES的线程,说起来mime类型不可靠,所以我决定使用php的mime类型函数 mime_content_type(),php的 getimagesize() pathinfo(),尽管pathinfo返回一个文件名和类型,但是我需要的文件路径不存在,所有的函数都以$ _FILES ['file'] ['tmp_name']作为参数传递。



所以这个问题出现了,当我决定上传一个图像文件,例如sample.JPG,我认为这些文件大部分都是原始的相机< - 这就是我认为虽然但仍然是什么重要的是我可以上传他们.JPG,.jpg,jpeg,.png。除了.JPG,他们都能正常工作。

主要的问题是当$ _FILES中的字段['tmp_name']在上传.JPG时没有值。



您遇到这个问题的人,请分享您的解决方法或您是如何做的。


<如果 $ _ FILES [$ field] ['tmp_name'] 是空的,那么文件还没有上传。你应该看看 $ _ FILES [$ field] ['error'] 来查看原因。

FWIW据我了解,浏览器提供了 $ _ FILES [] 中的mime类型。

更新:这里有一些盆栽的代码来处理所有的文件上传错误:

  $ message ='错误上传文件'; 
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。=' - 文件上传未完成';
break;
case UPLOAD_ERR_NO_FILE:
$ message。=' - 零长文件上传。
break;
默认值:
$ message。=' - internal error#'。$ _ FILES ['newfile'] ['error'];
break; (!$ message){
if(!is_uploaded_file($ _ FILES ['newfile'] ['tmp_name'])){
$ message ='错误上传文件 - 未知错误。
} else {
//让我们看看我们是否可以移动文件...
$ dest。='/'.$this_file;
if(!move_uploaded_file($ _ FILES ['newfile'] ['tmp_name'],$ dest)){//没有错误supporession,所以我们可以看到底层的错误。
$ message ='上传文件错误 - 无法保存上传(这可能是'。dest。'中的权限问题)';
} else {
$ message ='File uploaded okay。';
}
}
}


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.';
                }
            }
        }

这篇关于$ _FILES字段“tmp_name”在.JPG文件扩展名上没有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆