如何使用 php 将上传的图片保存到文件

How to save uploaded image to file using php

我一直在尝试使用 PHP 将上传的图像保存到文件夹中。我一直坚持这一点,因为图像的名称以不同于文件夹的方式保存到数据库中。

数据库:assets/images/profile_pics/john_doe5a153efc8731359393775e3355df0b77n.jpg

文件夹:john_doe_original.5a153efc8731359393775e3355df0b77njpeg

    include("includes/header2.php");

    $profile_id = $user['username'];
    $imgSrc = "";
    $result_path = "";
    $msg = "";

    /***********************************************************
        0 - Remove The Temp image if it exists
    ***********************************************************/
        if (!isset($_POST['x']) && !isset($_FILES['image']['name']) ){
            //Delete users temp image
                $temppath = 'assets/images/profile_pics/'.$profile_id.'_temp.jpeg';
                if (file_exists ($temppath)){ @unlink($temppath); }
        } 


    if(isset($_FILES['image']['name'])){    
    /***********************************************************
        1 - Upload Original Image To Server
    ***********************************************************/    
        //Get Name | Size | Temp Location           
            $ImageName = $_FILES['image']['name'];
            $ImageSize = $_FILES['image']['size'];
            $ImageTempName = $_FILES['image']['tmp_name'];

        //Get File Ext   
            $ImageType = @explode('/', $_FILES['image']['type']);
            $type = $ImageType[1]; //file type  
        //Set Upload directory    
            $uploaddir = $_SERVER['DOCUMENT_ROOT'].'/name/assets/images/profile_pics';
        //Set File name 
            $file_temp_name = $profile_id.'_original.'.md5(time()).'n'.$type; //the temp file name
            $fullpath = $uploaddir."/".$file_temp_name; // the temp file path
            $file_name = $profile_id.'_temp.jpeg'; //$profile_id.'_temp.'.$type; // for the final resized image
            $finalname = $profile_id.md5(time());   
            $fullpath_2 = "assets/images/profile_pics/".$finalname."n.jpg"; //for the final resized image
        //Move the file to correct location
            $move = move_uploaded_file($ImageTempName,$fullpath) ; 
            chmod($fullpath, 0777);  
            //Check for valid uplaod
            if (!$move) { 
                die ('File didnt upload');
            } else { 
                $imgSrc= "assets/images/profile_pics/".$file_name; // the image to display in crop area
                $msg= "Upload Complete!";   //message to page
                $src = $file_name;          //the file name to post from cropping form to the resize        
            } 
        }


    if (isset($_POST['Submit'])){


            //Insert image into database
            $insert_pic_query = mysqli_query($con, "UPDATE users SET profile_pic='$fullpath_2' WHERE username='$userLoggedIn'");

            //header("Location: account.php");

    }

感谢您的帮助。让我知道如何改进我的问题。

我个人使用 imagecreatefromjpeg http://php.net/manual/en/function.imagecreatefromjpeg.php

将临时上传的文件直接传递给此函数。

然后这允许我使用 imagescale 来调整个人资料图片的大小。 http://php.net/manual/en/function.imagescale.php

最后我发现 file-put-contents 是一种相当简洁的保存内容的方法。 http://php.net/manual/en/function.file-put-contents.php

move_uploaded_file() 中使用 $fullpath_2。将上传目录更改为 $uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/Halpper/';

if (isset($_FILES['image']['name'])) {
    /***********************************************************
     * 1 - Upload Original Image To Server
     ***********************************************************/
    //Get Name | Size | Temp Location
    $ImageName = $_FILES['image']['name'];
    $ImageSize = $_FILES['image']['size'];
    $ImageTempName = $_FILES['image']['tmp_name'];

    //Get File Ext
    $ImageType = @explode('/', $_FILES['image']['type']);
    $type = $ImageType[1]; //file type
    //Set Upload directory
    $uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/Halpper/';
    //Set File name
    $file_temp_name = $profile_id . '_original.' . md5(time()) . 'n' . $type; //the temp file name
    $fullpath = $uploaddir . "/" . $file_temp_name; // the temp file path
    $file_name = $profile_id . '_temp.jpeg'; //$profile_id.'_temp.'.$type; // for the final resized image
    $finalname = $profile_id . md5(time());
    $fullpath_2 = "assets/images/profile_pics/" . $finalname . "n.jpg"; //for the final resized image
    //Move the file to correct location
    if (move_uploaded_file($ImageTempName, $uploaddir . $fullpath_2)) {
        chmod($uploaddir . $fullpath_2, 0777);
    }
    //Check for valid uplaod
    if (!$move) {
        die ('File didnt upload');
    } else {
        $imgSrc = "assets/images/profile_pics/" . $file_name; // the image to display in crop area
        $msg = "Upload Complete!";   //message to page
        $src = $file_name;          //the file name to post from cropping form to the resize
    }
}