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

时间:2017-05-20 22:54:04

标签: php image-uploading

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

数据库:资产/图像/ 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");

    }

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

2 个答案:

答案 0 :(得分:1)

DEF(\x22qwer)中使用$fullpath_2。将上传方向更改为move_uploaded_file()

$uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/Halpper/';

答案 1 :(得分:0)

我个人使用过 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