Reputation: 47
<?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";
}
}
}
}
Not able to access a file using file_put_contents.
Upvotes: 2
Views: 652
Reputation: 3354
Check your file path. For example you access to ABCSat/Jun/2017.jpg
! I think you try to access ABC/Sat/Jun/2017.jpg
, so add /
or DIRECTORY_SEPARATOR
at the end of $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);
}
}
Upvotes: 2