Skip to content
Advertisement

Deleting image problem with unlink function

I created this method to unlink when I update the image but it doesn’t work with if not empty if I say empty it deletes. it is so strange I tried but didn’t work. I tried this without oop it works in another application. I changed it for it and I checked google but no solution till now. someone used is_uploaded_file on here for the solution but it doesn’t work too.

    public $tmp_path;
    public $upload_directory = "images"; 

public function picture_path(){
      return $this->upload_directory.DS.$this->filename;
    }
      public function set_file($file) { 

          if(empty($file) || !$file || !is_array($file)) {
            $this->errors[] = "There was no file uploaded here";
            return false;
          }elseif($file['error'] !=0) {
            $this->errors[] = $this->upload_errors_array[$file['error']];
            return false;
          } else {
            $this->filename = basename($file['name']);
            $this->filename = preg_replace('#[^a-z.0-9]#i', '', $this->filename); 
            $kaboom = explode(".", $this->filename); // Split file name into an array using the dot
            $fileExt = end($kaboom); // Now target the last array element to get the file extension
            $this->filename = time().rand().".".$fileExt;
            $this->tmp_path = $file['tmp_name'];
            $this->type     = $file['type'];
            $this->size     = $file['size'];
          }
        }

         public function getordelimage(){
                  static::find_by_id($this->id);
                  $target_path = SITE_ROOT.DS. 'admin' . DS . $this->picture_path();
                  if(empty($this->tmp_path)) { 
                      $this->filename;
                      $this->type;
                      $this->size;      
                  } elseif (!empty($this->tmp_path)) {
                    return unlink($target_path) ? true : false;
                  } else {
                    return false;
                  }
                }
    public function move_image(){
          $target_path = SITE_ROOT.DS. 'admin' . DS . $this->upload_directory . DS . $this->filename;
          return move_uploaded_file($this->tmp_path, $target_path);
        }

        public function delete_photo(){
          if ($this->delete()) {
            $target_path = SITE_ROOT.DS. 'admin' . DS . $this->picture_path();
            return unlink($target_path) ? true : false;
          } else {
            return false;
          }
        }

my edit page:

if (empty($_GET['id'])) {
    redirect("photos.php");
} else {
    $photo = Photo::find_by_id($_GET['id']);
    }
if (isset($_POST['update'])) {
    // $photo = Photo::find_by_id($_GET['id']);
    if ($photo) {
        $photo->title = filter_var($_POST['title'], FILTER_SANITIZE_STRING);
        $photo->caption = filter_var($_POST['caption'], FILTER_SANITIZE_STRING);
        $photo->alternate_text = filter_var($_POST['alternate_text'], FILTER_SANITIZE_STRING);
        $photo->description = filter_var($_POST['description'], FILTER_SANITIZE_STRING);


        $photo->set_file($_FILES['filename']);
        $photo->getordelimage();
        $photo->move_image();

        // if ($photo->set_file($_FILES['file'])) {
             $params = [$photo->title, $photo->description, $photo->caption,$photo->alternate_text,
           $photo->filename, $photo->type, $photo->size,
            ];
        // } else {
        //     $params = [$photo->title, $photo->description, $photo->caption,$photo->alternate_text];
        // }

        $photo->save($params);
        $session->message("The {$photo->filename} has been updated");
        redirect("edit_photo.php?id=$photo->id");
    }
}

Advertisement

Answer

your answer is hiding in your question :). probably you have problem in your oop database construct check it.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement