将已上传的文件移至laravel4中的新位置

时间:2015-02-04 12:54:44

标签: php file laravel-4 file-move

如何将已上传的文件移动到laravel4中的另一个位置。我将上传的文件保存在项目文件夹中的临时文件夹中,路径为'public/images/temp/'文件正在成功上传到该临时文件夹。但是我在这之间做了一个付款选项。因此,只有付款成功,我才需要通过重命名将苍蝇从临时文件夹移动到另一个文件夹,路径为'public/images/consult/'

我尝试了FILE :: Move()和FILE :: delete()但是没有用。我使用的代码是:

$filename1 = Session::get('email_filename1'); //filename of file stored in temporary folder

$ext = substr(strrchr($filename1,'.'),1);

$newfilename1 = 'Email_'.Str::random(20).'_'.Session::get('patient_id').'.'.$ext;

$oldfile = public_path().'images/consultation_files/temp/'.$filename1;

$newfile = public_path().'images/consultation_files/'.$newfilename1;

 File::move($oldfile, $newfile);

如果有人知道请分享您的解决方案。它会很有帮助..

日志显示:

[2015-02-05 11:27:54] log.ERROR:exception' Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException'在/var/www/html/myrpoject/vendor/laravel/framework/src/Illuminate/Routing/Router.php:1429 堆栈跟踪:#0 /var/www/html/myrpoject/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1050):Illuminate \ Routing \ Router-> handleRoutingException(Object(Symfony \ Component \ Routing) \异常\ ResourceNotFoundException))

:#1 / var / www / html / myrpoject / vendor / laravel / framework / src / Illuminate / Routing / Router.php(1014):Illuminate \ Routing \ Router-> findRoute(Object(Illuminate \ Http \)请求))

:#2 / var / www / html / myrpoject / vendor / laravel / framework / src / Illuminate / Foundation / Application.php(530):Illuminate \ Routing \ Router-> dispatch(Object(Illuminate \ Http \)请求))

:#3 / var / www / html / myrpoject / vendor / laravel / framework / src / Illuminate / Foundation / Application.php(506):Illuminate \ Foundation \ Application-> dispatch(Object(Illuminate \ Http \)请求))

:#4 /var/www/html/myrpoject/public/index.php(50):Illuminate \ Foundation \ Application-> run()

:#5 {main} [] []

1 个答案:

答案 0 :(得分:0)

使用File::copy()方法

$source将成为您的来源,$destination将成为您的目标

if ( ! File::copy($source, $desitination))
{
    die("Couldn't copy file");
}

要删除文件,请使用File::delete($filename);并使用多个文件File::delete($file1, $file2, $file3);

如果失败,请参阅app/storage/logs/laravel.log作为lukasgeiter建议

我建议检查File::exists(),然后执行File::delete($filename);

More Information here