我正在嘗試 (1) 上傳影像檔案 (2) 調整大小并將其居中并 (3) 更改檔案名并 (4) 將其移動到 /public 目錄。
這是我的代碼的一部分;
$path = $this->request->getFile('dh_site_logo')->store();
$temp_file_path = WRITEPATH.'uploads/' . $path;
service('image')
->withFile($temp_file_path) // must include full path
->fit(250,150,'center')
->save($temp_file_path);
$public_file_path = ROOTPATH.'public';
$file_to_upload = 'site_logo.'.$update_post->guessExtension();;
$overwrite = true;
$update_post->move($public_file_path, $file_to_upload, $overwrite);
我假設在writable/uploads移動到之前我必須在目錄中進行影像處理/public
我似乎無法鎖定已使用新的隨機名稱上傳的檔案,操作然后移動。
這個也試過
// example of return $path = "20220130/1643544458_5a528551d1fe83c88e02.gif"
$path = $this->request->getFile('dh_site_logo')->store('site_logo', 'site_logo.gif');
$temp_file_path = WRITEPATH.'uploads/' . $path;
service('image')
->withFile($temp_file_path) // must include full path
->fit(250,150,'center')
->save($temp_file_path);
$public_file_path = ROOTPATH.'public';
$new_file_name = 'site_logo.gif';
$overwrite = true;
$update_post->move($public_file_path, $new_file_name, $overwrite);
以上給我一個錯誤,The uploaded file has already been moved
uj5u.com熱心網友回復:
Ci4 / 影像上傳和操作錯誤 / finfo_file(/tmp/phpSrVWUZ): 無法打開流: 沒有這樣的檔案或目錄
問題1:
由于以下代碼行,您收到上述錯誤:
$file_to_upload = 'site_logo.'.$update_post->guessExtension();;
解釋一:
在下面的第一行代碼中:
$path = $this->request->getFile('dh_site_logo')->store();
您正在呼叫CodeIgniter\HTTP\Files\UploadedFile::store(?string $folderName = null, ?string $fileName = null)方法,它將上傳的檔案 ( /tmp/phpSrVWUZ) 保存到新位置。
即:該store(...)方法將上傳的檔案從服務器的默認臨時目錄(/tmp)移動到專案的上傳目錄(WRITEPATH . 'uploads/')。
然后,您嘗試呼叫$update_post->guessExtension()忘記UploadedFile實體$update_post仍然參考舊的不存在路徑 ( /tmp/phpSrVWUZ),因此出現錯誤。
更具體地說,方法CodeIgniter\HTTP\Files\UploadedFile::guessExtension()呼叫另一個方法CodeIgniter\Files\File::getMimeType()。該方法getMimeType(...)嘗試使用下面的代碼片段在不存在的檔案上檢索 mime 型別,從而導致錯誤:
finfo_file(finfo_open(FILEINFO_MIME_TYPE), "/tmp/phpSrVWUZ");
// PHP Warning: finfo_file(/tmp/phpSrVWUZ): Failed to open stream: No such file or directory in ...
=========
問題2:
這個也試過
...
以上給我一個錯誤,
The uploaded file has already been moved
解釋二:
您通常會收到此錯誤,因為您多次嘗試將上傳的檔案移動到新位置。
當您多次呼叫CodeIgniter\HTTP\Files\UploadedFile::store(...)or方法時會發生這種情況。CodeIgniter\HTTP\Files\UploadedFile::move(...)
摘自 CI 4.x源代碼。
/**
* Move the uploaded file to a new location.
* ...
* If this method is called more than once, any subsequent calls MUST raise
* an exception.
* ...
*/
public function move(...): bool {
// ...
if ($this->hasMoved) {
throw HTTPException::forAlreadyMoved();
}
// ...
}
更具體地說,每個UploadedFile實體都有一個名為的屬性,一旦上傳的檔案從服務器的默認臨時目錄成功移動,該屬性protected $hasMoved = false;就會更新:true
摘自 CI 4.x源代碼。
/**
* Returns whether the file has been moved or not. If it has,
* the move() method will not work and certain properties, like
* the tempName, will no longer be available.
*/
public function hasMoved(): bool;
解決方案 A:
如果您不關心原始上傳的檔案并且您只對駐留在“公共”路徑中的最終轉換/調整大小的檔案感興趣,這適用。
public function createThumbnail(\CodeIgniter\HTTP\Files\UploadedFile $uploadedFile, ?string $newThumbnailFileName = null, int $width = 250, int $height = 150, string $position = "center"): ?\CodeIgniter\Files\File
{
if (!$uploadedFile->isValid()) {
return null;
}
$newThumbnailFileName = $newThumbnailFileName
? ((($point = strrpos($newThumbnailFileName, ".")) === false) ? $newThumbnailFileName : substr($newThumbnailFileName, 0, $point)) . $uploadedFile->guessExtension()
: $uploadedFile->getRandomName();
$targetPath = ROOTPATH . 'public' . DIRECTORY_SEPARATOR . $newThumbnailFileName;
\Config\Services::image()
->withFile($uploadedFile->getRealPath() ?: $uploadedFile->__toString())
->fit($width, $height, $position)
->save($targetPath);
return new \CodeIgniter\Files\File($targetPath, true);
}
上面的函式基本上保持服務器默認臨時目錄中的原始上傳檔案不變,并處理保存在專案的“公共”路徑中的新影像。
解決方案 A 的用法:
上面的函式回傳一個代表新轉換影像的CodeIgniter\Files\File實體。IE:
$requestFileName = "dh_site_logo";
$uploadedFile = $this->request->getFile($requestFileName);
// Generates a thumbnail in the 'public' path with a uniquely generated filename.
$thumbnail = $this->createThumbnail(
uploadedFile: $uploadedFile
);
// OR
// Generates a thumbnail in the 'public' path with a custom filename ('site_logo').
$thumbnail = $this->createThumbnail(
uploadedFile: $uploadedFile,
newThumbnailFileName: "site_logo"
);
// OR
// You can modify the default parameters as well.
$thumbnail = $this->createThumbnail(
uploadedFile: $uploadedFile,
width: 100,
height: 150,
position: "left"
);
解決方案 B:
這適用于由于某種原因您想要轉換/調整大小并將修改后的影像保存在類似于解決方案 A的“公共”路徑中,并且仍然保留/保留或將原始上傳的檔案從服務器的臨時目錄移動到專案writable/uploads檔案夾以供將來參考或目的。
提示:如果該檔案沒有被移走或重命名,則該檔案將在請求結束時從臨時目錄中洗掉。-摘自 PHP 檔案: 示例 #2 驗證檔案上傳
腳步:
- 根據需要生成盡可能多的縮略圖(轉換后的檔案)。
- 最后,將上傳的檔案從服務器的默認臨時目錄移動到專案的
writable/uploads檔案夾中。
$requestFileName = "dh_site_logo";
$uploadedFile = $this->request->getFile($requestFileName);
// 1. Generate as many thumbnails (transformed files) as you need.
// Generates a thumbnail in the 'public' path with
// a uniquely generated filename.
$thumbnail = $this->createThumbnail(
uploadedFile: $uploadedFile
);
// 2. Lastly, move the uploaded file from the server's default temporary
// directory to the project's 'writable/uploads' folder (I.e: $uploadedFile->store()).
if (!$uploadedFile->hasMoved()) {
// The moved uploaded file.
$file = new \CodeIgniter\Files\File(WRITEPATH . 'uploads/' . $uploadedFile->store(), true);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/431265.html
下一篇:CI4-嘗試移動影像但出現錯誤“無法將檔案php6WkH2s移動到/var/www/example.com/development.example.com/app_dir/public/()
