<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 文件附件 ### 文件附件 模型可以使用[多態關系](https://octobercms.com/docs/database/relations#polymorphic-relations)的子集來支持文件附件。該`$attachOne`或`$attachMany`關系是專為一個文件鏈接到名為“附件”的數據庫記錄。在幾乎所有情況下,`System\Models\File`都使用模型來安全保持這種關系,其中對文件的引用作為記錄存儲在`system_files`表中,并且與父模型具有多態關系。 在下面的示例中,模型具有單個Avatar附件模型和許多Photo附件模型。 單個文件附件: ~~~ public $attachOne = [ 'avatar' => 'System\Models\File' ]; ~~~ 多個文件附件: ~~~ public $attachMany = [ 'photos' => 'System\Models\File' ]; ~~~ 注意:在以上示例中,使用的密鑰名稱與文件上載字段名稱相同。在模型與`System\Models\File`模型之間創建多態關系時,如果具有與文件上載字段名稱同名的列,則可能導致意外結果。 受保護的附件被上載到應用程序的**uploads / protected**目錄,該目錄不能從Web直接訪問。受保護的文件附件是通過將*public*參數設置為來定義的`false`: ~~~ public $attachOne = [ 'avatar' => ['System\Models\File', 'public' => false] ]; ~~~ ### [](https://octobercms.com/docs/database/attachments#creating-attachments)創建新附件 對于奇異的附件關系(`$attachOne`),您可以直接通過模型關系來創建附件,`Input::file`方法是使用方法設置附件的值,該方法從輸入的上載中讀取文件數據。 ~~~ $model->avatar = Input::file('file_input'); ~~~ 您也可以將字符串傳遞給`data`包含本地文件絕對路徑的屬性。 ~~~ $model->avatar = '/path/to/somefile.jpg'; ~~~ 有時`File`直接從(原始)數據創建實例也可能有用: ~~~ $file = (new System\Models\File)->fromData('Some content', 'sometext.txt'); ~~~ 對于多個附加關系(`$attachMany`),您可以`create`改為在關系上使用方法,請注意文件對象已與`data`屬性關聯。如果愿意,也可以將這種方法用于單數關系。 ~~~ $model->avatar()->create(['data' => Input::file('file_input')]); ~~~ 或者,您可以事先準備文件模型,然后在以后手動關聯關系。請注意,`is_public`必須使用此方法顯式設置屬性。 ~~~ $file = new System\Models\File; $file->data = Input::file('file_input'); $file->is_public = true; $file->save(); $model->avatar()->add($file); ~~~ 您也可以從URL添加文件。要使用此方法,您需要安裝cURL PHP Extension。 ~~~ $file = new System\Models\File; $file->fromUrl('https://example.com/uploads/public/path/to/avatar.jpg'); $user->avatar()->add($file); ~~~ 有時您可能需要更改文件名。您可以通過使用第二個方法參數來實現。 ~~~ $file->fromUrl('https://example.com/uploads/public/path/to/avatar.jpg', 'somefilename.jpg'); ~~~ ### [](https://octobercms.com/docs/database/attachments#viewing-attachments)查看附件 該`getPath`方法返回上載的公共文件的完整URL。以下代碼將顯示類似**example.com/uploads/public/path/to/avatar.jpg的內容** ~~~ echo $model->avatar->getPath(); ~~~ 返回多個附件文件路徑: ~~~ foreach ($model->photos as $photo) { echo $photo->getPath(); } ~~~ 該`getLocalPath`方法將返回本地文件系統中上載文件的絕對路徑。 ~~~ echo $model->avatar->getLocalPath(); ~~~ 要直接輸出文件內容,請使用`output`方法,其中將包含用于下載文件的必要標頭: ~~~ echo $model->avatar->output(); ~~~ 您可以使用`getThumb`方法調整圖像大小。該方法采用3個參數-圖像寬度,圖像高度和options參數。在“[圖像調整大小”](https://octobercms.com/docs/services/image-resizing#resize-parameters)頁面上了解有關這些參數的更多信息。 ### [](https://octobercms.com/docs/database/attachments#attachments-usage-example)使用范例 本部分顯示了模型附件功能的完整用法示例-從定義模型中的關系到在頁面上顯示上載的圖像。 在模型內部,定義與`System\Models\File`類的關系,例如: ~~~ class Post extends Model { public $attachOne = [ 'featured_image' => 'System\Models\File' ]; } ~~~ 構建用于上傳文件的表單: ~~~ <?= Form::open(['files' => true]) ?> <input name="example_file" type="file"> <button type="submit">Upload File</button> <?= Form::close() ?> ~~~ 在服務器上處理上傳的文件并將其附加到模型: ~~~ // Find the Blog Post model $post = Post::find(1); // Save the featured image of the Blog Post model if (Input::hasFile('example_file')) { $post->featured_image = Input::file('example_file'); } ~~~ 另外,您可以使用[延遲綁定](https://octobercms.com/docs/database/relations#deferred-binding)來延遲關系: ~~~ // Find the Blog Post model $post = Post::find(1); // Look for the postback data 'example_file' in the HTML form above $fileFromPost = Input::file('example_file'); // If it exists, save it as the featured image with a deferred session key if ($fileFromPost) { $post->featured_image()->create(['data' => $fileFromPost], $sessionKey); } ~~~ 在頁面上顯示上傳的文件: ~~~ // Find the Blog Post model again $post = Post::find(1); // Look for the featured image address, otherwise use a default one if ($post->featured_image) { $featuredImage = $post->featured_image->getPath(); } else { $featuredImage = 'http://placehold.it/220x300'; } <img src="<?= $featuredImage ?>" alt="Featured Image"> ~~~ 如果需要訪問文件的所有者,則可以使用模型的`attachment`屬性`File`: ~~~ public $morphTo = [ 'attachment' => [] ]; ~~~ 例: ~~~ $user = $file->attachment; ~~~ 有關更多信息,請閱讀[多態關系](https://octobercms.com/docs/database/relations#polymorphic-relations) ### [](https://octobercms.com/docs/database/attachments#attachments-validation-example)驗證示例 下面的示例使用[數組驗證](https://octobercms.com/docs/services/validation#validating-arrays)來驗證`$attachMany`關系。 ~~~ use October\Rain\Database\Traits\Validation; use System\Models\File; use Model; class Gallery extends Model { use Validation; public $attachMany = [ 'photos' => File::class ]; public $rules = [ 'photos' => 'required', 'photos.*' => 'image|max:1000|dimensions:min_width=100,min_height=100' ]; /* some other code */ } ~~~ 有關`attribute.*`上面使用的語法的更多信息,請參見[驗證數組](https://octobercms.com/docs/services/validation#validating-arrays)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看