移动使用Dropzone.js和PHP上传的照片 [英] Moving a photo uploaded with Dropzone.js and PHP

查看:99
本文介绍了移动使用Dropzone.js和PHP上传的照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 dropzone.js 为以下对象添加简单的拖放界面一个用户最多可以将10张照片上传到我的服务器(当前为WAMP)。我在后端使用PHP 5.4。

I'm trying to use dropzone.js to add an easy drag-and-drop interface for a user to upload up to 10 photos to my server (currently WAMP). I'm using PHP 5.4 on the back end.

本教程我使系统工作正常,可以将可接受的文件类型上传到根目录中的 Uploads文件夹中,但是我一直在坚持如何使照片上传到每个用户的唯一目录。

Following this tutorial I got the system working enough to upload acceptable file types to an 'Uploads' folder in my root directory, but I'm stuck on how to make the photos instead upload to a unique directory for each user.

当用户进入我的照片上传页面时,他们已经创建了一个文件夹在我的服务器上具有唯一名称,其路径已存储在$ _SESSION ['requestedDirectory']中。

By the time the user gets to my photo upload page, they've already created a folder on my server with a unique name, the path to which has been stored in $_SESSION['requestedDirectory'].

以下是用于显示Dropzone表单的代码:

Here is the code used to display the Dropzone form:

<form action="<?php echo BASE_URL; ?>photo_uploads.php" class="dropzone" id="photoUploadDropzone"></form>

<script type="text/javascript">
    Dropzone.options.photoUploadDropzone = {
      paramName: "file",
      maxFilesize: 5, // MB
      maxFiles: 10,
      addRemoveLinks: true,
      acceptedFiles: "image/jpeg, image/jpg, image/png, image/gif",
      accept: function(file, done) {
        done();
      }
    };
</script>

它将数据发送到名为photo_uploads.php的文件,该文件具有以下代码:

As you can see, it sends the data to a file called photo_uploads.php, which has the following code:

if (!empty($_FILES)) {
  $tempFile = $_FILES['file']['tmp_name'];
  $targetPath = $_SESSION['requestedDirectory']; <--HERE'S THE PROBLEM LINE
  $targetFile =  $targetPath. $_FILES['file']['name'];
  move_uploaded_file($tempFile,$targetFile);
}

如上面的评论所述,该文件似乎无法取出数据会话变量,但我不确定为什么。如果我更改该行以提供完整目录(例如$ targetPath ='C:// wamp / www / shops / foldername';),它可以正常工作,但是我当然不能根据哪个用户动态更改该文件夹名

As mentioned in the comment above, this file doesn't seem to be able to take data out of a session variable, but I'm not sure why. If I change that line to give the full directory (eg $targetPath = 'C://wamp/www/shops/foldername';) it works fine, but then of course I can't change that foldername dynamically based on which user is using the form.

所以要澄清一下,我想知道如何将文件上传到Session中存储的路径?

So to clarify, I'd like to know how to upload the file to the path stored in Session?

对于以后遇到相同问题的其他人,这就是我将表格更改为:

For anyone else with the same problem in future, this is what I changed the form to:

<form action="<?php echo BASE_URL; ?>photo_uploads.php?folder=<?php echo $_SESSION['photosDir']; ?>" class="dropzone" id="photoUploadDropzone"></form>

就像Rizwan建议的那样,通过在GET变量中传递$ _SESSION ['photosDir']值,可以在表单后访问

Like Rizwan suggested, by passing the $_SESSION['photosDir'] value in a GET variable it was accessible after the form posted.

我所做的另一个更改是对photo_uploads.php的更改,仅更改为以下行:

The other change I made was to photo_uploads.php, just to the following line:

$targetPath = $_GET['folder'];

完全不知道为什么最初无法直接从Session获得该值-我从没听说过Session值超出范围-但很高兴能解决此问题。

Absolutely no idea why the value wasn't available directly from Session in the first place - I've never heard of Session values going out of scope - but happy to have this problem solved.

推荐答案

您可以将请求目录作为查询字符串传递给表单的动作属性。这样就可以访问它。

You can pass the requestedDirectory as a query string with the form's action attribute. In that way it can be accessible.

这篇关于移动使用Dropzone.js和PHP上传的照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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