如何使用" file_put_contents"访问文件用PHP?

时间:2017-06-24 11:58:30

标签: php

<?php
require_once(dirname(__FILE__) . '/connectionClass.php');

class webcamClass
{
    public $imageFolder = "ABC";

    //This function will create a new name for every image captured using the current data and time.
    public function getNameWithPath()
    {
        $name = $this->imageFolder . date('D/M/Y') . ".jpg";
        return $name;
    }

    //function will get the image data and save it to the provided path with the name and save it to the database
    public function showImage()
    {
        $file = file_put_contents($this->getNameWithPath(), file_get_contents('php://input'));
        if (!$file) {
            return "ERROR: Failed to write data to " . $this->getNameWithPath() . ", \n";
        } else {
            $this->saveImageToDatabase($this->getNameWithPath()); // this line is for saving image to database
            return $this->getNameWithPath();
        }

    }

    //function for changing the image to base64
    public function changeImagetoBase64($image)
    {
        $path   = $image;
        $type   = pathinfo($path, PATHINFO_EXTENSION);
        $data   = file_get_contents($path);
        $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
        return $base64;
    }

    public function saveImageToDatabase($imageurl)
    {
        $image = $imageurl;
        //        $image=  $this->changeImagetoBase64($image);          //if you want to go for base64 encode than enable this line
        if ($image) {
            $query  = "Insert into snapshot (Image) values('$image')";
            $result = $this->query($query);
            if ($result) {
                return "Image saved to database";
            } else {
                return "Image not saved to database";
            }
        }
    }


}

无法使用file_put_contents访问文件。

1 个答案:

答案 0 :(得分:2)

检查文件路径。例如,您可以访问ABCSat/Jun/2017.jpg!我认为您尝试访问ABC/Sat/Jun/2017.jpg,因此在/末尾添加DIRECTORY_SEPARATOR$image_folder

<?php
require_once(dirname(__FILE__) . '/connectionClass.php');

class webcamClass
{
    public $imageFolder = "ABC" . DIRECTORY_SEPARATOR;

    //This function will create a new name for every image captured using the current data and time.

    public function createDirectories($file_name = ''){
        $directories = explode('/', $file_name);

        $dir_path = '';
        for($i = 0; $i < count($directories) - 1; $i++) {
            $dir_path .= $directories[$i] . DIRECTORY_SEPARATOR;
            if(!file_exists($dir_path)) {
                mkdir($dir_path);
            }
        }
    }

    public function getNameWithPath()
    {
        $name = $this->imageFolder . date('D/M/Y') . ".jpg";
        $this->createDirectories($name);
        return $name;
    }

    //function will get the image data and save it to the provided path with the name and save it to the database
    public function showImage()
    {
        $file = file_put_contents($this->getNameWithPath(), file_get_contents('php://input'));
        if (!$file) {
            return "ERROR: Failed to write data to " . $this->getNameWithPath() . ", \n";
        } else {
            $this->saveImageToDatabase($this->getNameWithPath()); // this line is for saving image to database
            return $this->getNameWithPath();
        }

    }

    //function for changing the image to base64
    public function changeImagetoBase64($image)
    {
        $path   = $image;
        $type   = pathinfo($path, PATHINFO_EXTENSION);
        $data   = file_get_contents($path);
        $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
        return $base64;
    }

    public function saveImageToDatabase($imageurl)
    {
        $image = $imageurl;
        //        $image=  $this->changeImagetoBase64($image);          //if you want to go for base64 encode than enable this line
        if ($image) {
            $query  = "Insert into snapshot (Image) values('$image')";
            $result = $this->query($query);
            if ($result) {
                return "Image saved to database";
            } else {
                return "Image not saved to database";
            }
        }
    }

    public function query($query) {
        $dbObj = new dbObj();
        $conn = $dbObj->getConnstring();
        return mysqli_query($conn, $query);
    }


}