【问题标题】:How to save uploaded image to file using php如何使用php将上传的图像保存到文件
【发布时间】:2018-01-10 17:19:27
【问题描述】:

我一直在尝试使用 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");

    }

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

【问题讨论】:

    标签: php image-uploading


    【解决方案1】:

    在您的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
        }
    }
    

    【讨论】:

    • 添加 $fullpath_2 后,我在第 44 行收到错误(警告:chmod():没有此类文件或目录)。我怎样才能解决这个问题?感谢您的帮助。
    • 是的,我将 $move 更改为您推荐的那个。编辑:它起作用了。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多