简体   繁体   English

Laravel使用按钮将图像从一个文件夹移动到另一个文件夹

[英]Laravel move image from one folder to another with button

I'm new to Laravel and I'm trying to figure out the appropriate way of moving an image with the click of a button. 我是Laravel的新手,我试图找出通过单击按钮移动图像的适当方法。 I currently have a folder containing unverified images, and these are displayed on a control panel where admins can approve them before they're displayed on another page. 我目前有一个包含未验证图像的文件夹,这些图像显示在控制面板上,管理员可以在未显示其他页面之前批准它们。

My current solution is to add a link below the image called 'Accept Image', which passes the image name as a GET parameter to another file. 我当前的解决方案是在图像下方添加一个名为“接受图像”的链接,该链接将图像名称作为GET参数传递给另一个文件。 This is sent to another page that moves the image from the unverified folder to the verified folder, and then the admin gets redirected back to the control panel. 这被发送到另一个页面,该页面将图像从未验证的文件夹移动到已验证的文件夹,然后管理员被重定向回控制面板。 The solution works just fine, but I'm not sure if this is a safe/good way to do it. 该解决方案可以正常工作,但是我不确定这是否是安全/良好的方法。

Here's my code: 这是我的代码:

<a href="/accept/upload?fileName='.$fileName.'">Accept Image</a>

And the /accept/upload looks like this: / accept / upload看起来像这样:

$fileName = htmlentities($_GET['fileName']);
$destinationPath = 'verified-images/';
$sourcePath = 'unverified-images/';

if (File::move($sourcePath.$fileName, $destinationPath.$fileName))
{
  Session::flash('success', 'Success'); 
  return Redirect::to('controlpanel');
} 
else
{
  Session::flash('error', 'Error');
  return Redirect::to('controlpanel');
}

I'm not sure if that's all the code but make sure you have some sort of access control so not everyone can do this, otherwise it looks like it would be possible for someone to pass in some variables where they can start moving just about any file around. 我不确定这是否就是全部代码,但请确保您具有某种访问控制,因此并非每个人都可以执行此操作,否则似乎有人可以传入一些变量,使他们可以开始移动几乎任何变量。文件周围。 I'd also check mimetypes on the files to be sure they are images. 我还要检查文件上的MIME类型,以确保它们是图像。

Another solution altogether and one I think more people would prefer is to keep all the files in the same directory where each file would have one corresponding record in a database table which would have columns like path, verified, created, and updated. 另一种解决方案是,我认为更多的人希望将所有文件保留在同一目录中,其中每个文件在数据库表中都有一个对应的记录,该表中将包含路径,已验证,创建和更新的列。 Then you'd just need to grab the correct records from the database to display the images. 然后,您只需要从数据库中获取正确的记录即可显示图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM