簡體   English   中英

使用 unlink 刪除圖片問題 function

[英]Deleting image problem with unlink function

當我更新圖像時,我創建了這個方法來取消鏈接,但如果我說空它會刪除,它就不能與 if not empty 一起使用。 太奇怪了,我試過了,但沒有用。 我在沒有 oop 的情況下嘗試了它,它在另一個應用程序中工作。 我為此進行了更改,並檢查了谷歌,但直到現在都沒有解決方案。 有人在這里使用 is_uploaded_file 作為解決方案,但它也不起作用。

    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;
          }
        }

我的編輯頁面:

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");
    }
}

您的答案隱藏在您的問題中:)。 可能您的 oop 數據庫構造有問題,請檢查它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM