【发布时间】: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