【问题标题】:Laravel/php upload not workingLaravel / php上传不起作用
【发布时间】:2015-06-23 08:29:57
【问题描述】:

有很多类似的问题,但似乎没有一个完全相同。

这是我在 Facade 中使用的一些代码

public function create($input, $index = false, $params = false)
{
    $file = Input::file($input);

    if ($file === null)
    {
        return false;
    }

    if ($index !== false)
    {
        $file = $file[$index];
    }
    else
    {
        $file = reset($file);
    }

    if ($file === null)
    {
        return false;
    }

    $image              = new ImageModel;
    $image->mime        = $file->getClientMimeType();
    $image->extension   = $file->guessClientExtension();
    $image->filename    = $file->getClientOriginalName();

    if ($params !== false)
    {
        //there are parameters to add
    }
    $image->save();
    $image->hash = md5($image->id);

    $path = str_split($image->hash);
    $path = array_slice($path, 0, 5);

    $system_path = implode(DIRECTORY_SEPARATOR, $path);

    $fileName = substr($image->hash, 5);
    $path[] = $fileName;

    $image->path = implode('/', $path);
    $destination_path = implode(DIRECTORY_SEPARATOR, [public_path(), 'images', $system_path, null]);

    if (self::_makePath($destination_path))
    {
        $file_path = implode('.', [$destination_path.$fileName, $image->extension]);
        $image->save();
        dd([
            $file->getRealPath(),
            file_exists($file->getRealPath()),
            file_exists($destination_path),
            $file_path,
            is_writable($destination_path),
            move_uploaded_file($file->getRealPath(), $file_path)
        ]);
        move_uploaded_file($file->getRealPath(), $file_path);
        //$file->move($destination_path, $fileName);

        $this->image = $image;

        return $this->image;
    }

    return false;
}

此函数的目标是创建图像 id 的哈希,然后使用它来创建图像的目录路径,确保上传始终是唯一的。

这个函数最近在一个宅基地应用程序中使用$file->move 工作正常(laravel 4.2 而不是最新版本的宅基地,也进行了一定程度的调整,但没有更改任何 php/nginx 值)但是没有任何调整它有一段时间,它停止工作。我已尝试将功能更改为其他上传方式,但没有。它只是说由于未知原因无法上传文件。

使用上面的代码,我在那里进行了一些测试,以确定文件是否可以上传/移动,并且全部检查出来,即使 move_uploaded_file 返回 true,但是没有文件被移动。

【问题讨论】:

  • 不仔细看代码,有没有检查过目标目录的权限?
  • 是的,我使用is_writable 来检查使用转储。

标签: php laravel file-upload laravel-4


【解决方案1】:

解决了这个问题,虽然这是我的错误,但很难追查。

基本上,上传运行了两次,它可以正常工作,但因为最新的图像是第二张,所以看起来好像不是。使用 laravel 很难看到这一点,除非您看到添加到数据库中的 2 行,但如果您没有将它们存储在数据库中,那么该选项就消失了。

运行两次的代码是对Facade的调用,所以上面的代码没有列出来。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 2017-04-16
    • 2021-02-03
    • 2017-07-28
    • 1970-01-01
    • 2015-02-11
    相关资源
    最近更新 更多