如何将文件从一个位置移动到另一位置? [英] How can I move a file from one location to another?

查看:450
本文介绍了如何将文件从一个位置移动到另一位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件从一个位置移动到另一位置.在这种情况下,这是我的用户个人资料图片.由于我根据用户名存储我的用户个人资料图片,因此当他们更改用户名时.我将需要移动他们的个人资料照片,否则,图像链接将被破坏.

I'm trying to move a file from one place to another place. In this case it is my user profile picture. Since I store my user profile picture base on their username, so when they change their username. I will need to move their profile photo, otherwise, the image link will be broken.

我在这里尝试过

if ( $user->username != Input::get('username')) {
    $new_path = public_path().'/img/logo/'. Input::get('username').'/'.$user->logo_path;
    $old_path = public_path().'/img/logo/'. $user->username.'/'.$user->logo_path;
    $move = File::move($new_path, $old_path);
    $delete = File::delete($old_path);
}


我不断得到:


I keep getting:

有什么建议吗?

推荐答案

您正在错误的方向移动文件.

You're moving the file in the wrong direction.

应为$move = File::move($old_path, $new_path);

...换句话说,第一个参数应该是OLD文件位置,第二个参数应该是NEW文件位置...您可以将其倒退. :)

... in other words, the first argument should be the OLD file location, the second argument should be the NEW file location... you have it backwards. :)

Laravel文档

将文件移动到新位置

Move A File To A New Location

Storage :: move('old/file1.jpg','new/file1.jpg');

Storage::move('old/file1.jpg', 'new/file1.jpg');

此外,如上所述,由于该文件已移动并因此被删除,因此您不应同时拥有File :: delete.

Additionally, as said above, you shouldn't have the File::delete either since that file was moved and therefore deleted.

所以,有两件事:

1)切换"old_path"和"new_path"以及

1) Switch the "old_path" and "new_path" and

2)删除File :: delete行

2) Remove the File::delete line

这篇关于如何将文件从一个位置移动到另一位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆