当我更新图像时,我创建了这个方法来取消链接,但如果我说空它会删除,它就不能与 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;
}
}
30秒到达战场