一个php中upload file的小问题

2020-04-29  本文已影响0人  芒鞋儿

在PHP中用move_upload_file 这个函数上传文件的时候,发现一个问题,
上传一直失败,查了一些文档:

[三种可能的错误方式]
(https://blog.csdn.net/xiaoping0915/article/details/50980660)

1)权限的问题都解决了,但还是不对。
2)文件的size也没问题。
3)可能还是文件路径问题,
用 [_FILES['image']['error']]去查了错误代码,发现错误码为0,也就是没有错误。 一开始不相信是文件路径,因为我打印了image_path,是对的,
最后发现是文件设定相对路径的时候出问题了。
因为文件是先被上传到php.ini里设定的临时文件夹,然后从文件夹中移动到server的目标文件夹,因此该目的文件夹如果要设定相对路径,需要以系统设定的临时文件夹为起点。
查了php.ini


;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads=On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
;upload_tmp_dir =
upload_tmp_dir="/Applications/XAMPP/xamppfiles/temp/"
; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize=40M

因此,必须以"/Applications/XAMPP/xamppfiles/temp/"为起点去设定目标文件夹,
否则就找不到。
code 参考:

            if($_FILES['image']['name'] != ''){
            $image_name = $_FILES['image']['name'];
            $image_tmp = $_FILES['image']['tmp_name'];
            $image_size = $_FILES['image']['size'];
            $image_error = $_FILES['image']['error'];
            $image_ext = pathinfo($image_name,PATHINFO_EXTENSION);
            //$image_path = '../images/'.$image_name; 
            // here if you set up a relative path, you need to check the upload temporary path in php.ini, which is the base path to set.
            $image_path = '/Applications/XAMPP/htdocs/php/cms_blog/image/'.$image_name;
            $image_db_path = 'images/'.$image_name;

            
        
            if($image_size < 2097152){
                if($image_ext == 'jpg' || $image_ext == 'png' || $image_ext == 'gif'){
                    if(move_uploaded_file($image_tmp,$image_path)){
                        $ins_sql = "INSERT INTO posts (title, description, image, category, date, author) VALUES ('$title', '$description', '$image_db_path', '$_POST[category]', '$date', '$_SESSION[user]')";
                        if(mysqli_query($conn,$ins_sql)){
                            header('Location: post_list.php');
                        }else {
                            $error = '<div class="alert alert-danger">The Query Was not Working!</div>';
                        }
                    }else{
                        $error = '<div class="alert alert-danger">Sorry, Unfortunately Image hos not been uploaded!</div>';
                    }


                    
                } else {
                    $error = '<div class="alert alert-danger">Image Format was not Correct!</div>';
                }
                
            } else {
                $error = '<div class="alert alert-danger">Image File Size is much bigger then Expect!</div>';
            }
        

            
        } else {
            $ins_sql = "INSERT INTO posts (title, description, category, date, author) VALUES ('$title', '$description', '$_POST[category]', '$date', '$_SESSION[user]')";
            if(mysqli_query($conn,$ins_sql)){
                header('Location: post_list.php');
            }else {
                $error = '<div class="alert alert-danger">There is SQL error!</div>';
            }
        }
上一篇下一篇

猜你喜欢

热点阅读