【问题标题】:How can I check if a specific file exists on a given path?如何检查给定路径上是否存在特定文件?
【发布时间】:2021-02-17 11:53:49
【问题描述】:

我想实现一个功能来显示用户的头像。当用户上传他的图片时,它会自动与他的用户名一起保存在服务器上。

当我显示图像源时,将基于路径 /images/users/avatars/username

% if ($self->session('role') eq 'Admin') {
  <div class="picture-container">
  % my $path = Mojo::File->new("/images/users/avatars/$username");
  <%= l($path) %>
    % if (defined $path) {
         <img src="/images/users/avatars/<%= $username %>"
    % } else {
         <img src="/images/users/avatars/default.png"
    % }
              class="img-thumbnail"
              alt="Responsive image" id="picture">
  </div>

% }

但是如果文件不存在我想显示一个默认头像。

【问题讨论】:

    标签: html perl mojolicious


    【解决方案1】:
    my $path_string = $path->path;   # get path from Mojo::File object
    if (-e $path_string)
    

    ...将检查文件是否存在。 perldoc -f -X

    【讨论】:

    • 不起作用,路径可能没有正确定义?
    • 我现在看到 $path 是一个 Mojo::File 对象,我只是假设它是一个路径。您应该能够从$path-&gt;path 获取路径。 Perl 需要一个 *nix 风格的路径。例如images/foo/home/user/www/images/foo.
    猜你喜欢
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多