2017-06-24 3 views
2
<?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"; 
      } 
     } 
    } 


} 

Mit file_put_contents kann nicht auf eine Datei zugegriffen werden.Zugriff auf eine Datei mit "file_put_contents" in PHP?

+0

Bitte lesen Sie [Unter welchen Umständen kann ich „dringend“ oder andere ähnliche Ausdrücke auf meine Frage hinzufügen, um schneller zu erhalten Antworten?] (// meta.stackoverflow.com/q/326569) - Die Zusammenfassung ist, dass dies kein idealer Weg ist, um Freiwillige anzusprechen und ist wahrscheinlich kontraproduktiv, um Antworten zu erhalten. Bitte unterlassen Sie das Hinzufügen zu Ihren Fragen. – halfer

Antwort

2

Überprüfen Sie Ihren Dateipfad. Zum Beispiel Zugriff auf ABCSat/Jun/2017.jpg! Ich glaube, Sie versuchen ABC/Sat/Jun/2017.jpg zuzugreifen, so fügen / oder DIRECTORY_SEPARATOR am Ende $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); 
    } 


} 
+0

Warnung: file_put_contents (ABC/Sat/Jun/2017.jpg): Fehler beim Öffnen des Streams: Keine solche Datei oder kein Verzeichnis in C: \ xampp \ htdocs \ A \ captureupload \ webcamClass.php in Zeile 15 FEHLER: Fehler beim Schreiben Daten zu ABC/Sat/Jun/2017.jpg. Das ist mein Fehler. – Karthik

+0

Dieser Fehler ist aufgetreten, weil das Verzeichnis nicht existiert. Ich habe meine Antwort aktualisiert und die createDirectories-Funktion hinzugefügt. Bitte versuchen Sie es. @Karthik –

+0

Ich habe ein Verzeichnis oder einen Ordner erstellt, aber immer noch nicht auf eine Datei oder einen Ordner zugreifen können: Warnung: file_put_contents (imageSat/Jun/2017.jpg): Fehler beim Öffnen des Streams: Keine solche Datei oder Verzeichnis in C: \ xampp \ htdocs \ A \ captureupload \ webcamClass.php in Zeile 32 FEHLER: Fehler beim Schreiben von Daten in imageSat/Jun/2017.jpg, – Karthik

Verwandte Themen