我正在嘗試上傳檔案并將其移動到 public/ 檔案夾。檔案上傳到可寫檔案夾沒有問題,但是有問題的是移動到公用檔案夾。
這是我的代碼;
$update_post->move(ROOTPATH.'public/', $update_post.'.'.$fileType);
路徑正確。當我回顯然echo ROOTPATH.'public/'; 后手動復制/粘貼時,我確實到達了目標目錄。
權限正確。public/ 目錄有我的權限:
drwxr-xr-x 9 www-data www-data 4096 Jan 30 01:08 public
任何提示表示贊賞。
uj5u.com熱心網友回復:
原因:
這是因為move(string $targetPath, ?string $name = null, bool $overwrite = false)方法的 $name引數無效。
$update_post->move( ... , $update_post.'.'.$fileType);
解釋:
連接class CodeIgniter\Files\File extends SplFileInfo實體呼叫繼承SplFileInfo類的__toString()方法,該方法將檔案的路徑作為字串回傳。
請注意,它不會回傳您感興趣的檔案名。
解決方案:
您應該改為傳入基本名稱。
$update_post->move(
ROOTPATH . 'public/',
$update_post->getBasename()
);
或者,由于您沒有 更改目標檔案名,因此不傳入該move(...)方法的第二個引數會更簡潔。IE:
$update_post->move(
ROOTPATH . 'public'
);
附錄:
如果您希望將目標檔案名更改為新名稱,請嘗試以下操作:
猜測擴展()
嘗試根據受信任的
getMimeType()方法確定檔案擴展名。如果 mime 型別未知,將回傳 null。這通常比簡單地使用檔案名提供的擴展名更受信任。使用app/Config/Mimes.php中的值來確定擴展名:
$newFileName = "site_logo"; // New filename without suffixing it with a file extension.
$fileExtension = $update_post->guessExtension();
$update_post->move(
ROOTPATH . 'public',
$newFileName . (empty($fileExtension) ? '' : '.' . $fileExtension)
);
筆記:
該move(...)方法為重新定位的檔案回傳一個新的 File 實體,因此如果需要結果位置,您必須捕獲結果:$newRelocatedFileInstance = $update_post->move(...);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/431266.html
上一篇:Ci4/影像上傳和操作錯誤/finfo_file(/tmp/phpSrVWUZ):無法打開流:沒有這樣的檔案或目錄
