yii2 saveAs 两个文件,只保存一个
yii2 saveAs two files, saves only one
我不知道为什么,但我在表格中指定了两个上传(预览、细节),当我试图保存它们时,细节被保存但预览没有。 Db 的结果如我所料 - 在 imageSrc 和 imageDetailSrc
列中保存了 $slug
const UPLOAD_FILE_URL = 'uploads/recipes/';
const UPLOAD_FILE_DETAILS_URL = self::UPLOAD_FILE_URL.'details/';
$filePath = self::UPLOAD_FILE_URL . $slug . '.' . $this->imageSrc->extension;
$filePathDetail = self::UPLOAD_FILE_DETAILS_URL . $slug . '.' . $this->imageDetailSrc->extension;
if ($this->imageSrc->saveAs($filePath) && $this->imageDetailSrc->saveAs($filePathDetail)) {
$this->imageSrc = $slug . '.' . $this->imageSrc->extension;
$this->imageDetailSrc = $slug . '.' . $this->imageDetailSrc->extension;
}
if ($this->save(false, ['imageSrc', 'imageDetailSrc'])) {
return true;
}
Yii2 官方文档说 saveAs 函数使用 move_uploaded_file() 函数移动临时文件。因此,当您第一次调用 saveAs 函数时,临时文件会被删除。如果您不想删除 saveAs,那么您应该将 false 作为第二个参数发送到您的 saveAs 函数。
const UPLOAD_FILE_URL = 'uploads/recipes/';
const UPLOAD_FILE_DETAILS_URL = self::UPLOAD_FILE_URL.'details/';
$filePath = self::UPLOAD_FILE_URL . $slug . '.' . $this->imageSrc-
>extension;
$filePathDetail = self::UPLOAD_FILE_DETAILS_URL . $slug . '.' . $this->imageDetailSrc->extension;
if ($this->imageSrc->saveAs($filePath, false) && $this->imageDetailSrc->saveAs($filePathDetail)) {
$this->imageSrc = $slug . '.' . $this->imageSrc->extension;
$this->imageDetailSrc = $slug . '.' . $this->imageDetailSrc->extension;
}
if ($this->save(false, ['imageSrc', 'imageDetailSrc'])) {
return true;
}
我不知道为什么,但我在表格中指定了两个上传(预览、细节),当我试图保存它们时,细节被保存但预览没有。 Db 的结果如我所料 - 在 imageSrc 和 imageDetailSrc
列中保存了 $slugconst UPLOAD_FILE_URL = 'uploads/recipes/';
const UPLOAD_FILE_DETAILS_URL = self::UPLOAD_FILE_URL.'details/';
$filePath = self::UPLOAD_FILE_URL . $slug . '.' . $this->imageSrc->extension;
$filePathDetail = self::UPLOAD_FILE_DETAILS_URL . $slug . '.' . $this->imageDetailSrc->extension;
if ($this->imageSrc->saveAs($filePath) && $this->imageDetailSrc->saveAs($filePathDetail)) {
$this->imageSrc = $slug . '.' . $this->imageSrc->extension;
$this->imageDetailSrc = $slug . '.' . $this->imageDetailSrc->extension;
}
if ($this->save(false, ['imageSrc', 'imageDetailSrc'])) {
return true;
}
Yii2 官方文档说 saveAs 函数使用 move_uploaded_file() 函数移动临时文件。因此,当您第一次调用 saveAs 函数时,临时文件会被删除。如果您不想删除 saveAs,那么您应该将 false 作为第二个参数发送到您的 saveAs 函数。
const UPLOAD_FILE_URL = 'uploads/recipes/';
const UPLOAD_FILE_DETAILS_URL = self::UPLOAD_FILE_URL.'details/';
$filePath = self::UPLOAD_FILE_URL . $slug . '.' . $this->imageSrc-
>extension;
$filePathDetail = self::UPLOAD_FILE_DETAILS_URL . $slug . '.' . $this->imageDetailSrc->extension;
if ($this->imageSrc->saveAs($filePath, false) && $this->imageDetailSrc->saveAs($filePathDetail)) {
$this->imageSrc = $slug . '.' . $this->imageSrc->extension;
$this->imageDetailSrc = $slug . '.' . $this->imageDetailSrc->extension;
}
if ($this->save(false, ['imageSrc', 'imageDetailSrc'])) {
return true;
}