使用dropzone将文件发布到服务器

时间:2014-05-24 21:20:36

标签: php jquery file-upload dropzone.js

我在我的php应用程序中使用dropzone,但我无法将文件发布到服务器,我是新手dropzone已阅读文档但仍然有这样的问题。

这是html代码:

<div id='dropzone'>

</div>

这是javascript代码:

<script type="text/javascript" src="js/dropzone.js"></script>

<script type="text/javascript">
    $(document).ready(function() {
        $("#dropzone").dropzone({ 
            url: "profile.php", 
            maxFilesize: 8, 
            paramName: "Displaypic", 
            addRemoveLinks: true 
        });
    });
</script>

我想了解如何将文件放入文件阵列并从服务器端访问此类文件。

1 个答案:

答案 0 :(得分:0)

Dropzone只是用于创建堆栈的图像处理程序。实际上传不是由dropzone处理,而是通过php或javascript处理。我选择了php。我从不喜欢他们在他们的教程上做的方式并且有几个问题,所以我想出了自己的方式。这是我的uploads.php文件。

<?php 
session_start();
//The $_SESSION['x'] variable is serialized using 
//     date("Y_m_d") . "-" . date("h_i_sa")
//This allows the file names to appear like this at the end...
//                              2015_07_07-12_35_22pm1.jpg
//                              2015_07_07-12_35_22pm2.jpg
$newname = $_SESSION['x']; 
$ds = "/";  //1
//My upload folder for the images.
$storeFolder = "/photos";  
$ext = "jpg"; // My Format is .jpg so I chose to restrict to jpg only!
$_SESSION['count'] +=1;
//count is added to the end of the file name so that each name is unique.
$name = $newname.$_SESSION['count'] .'.'.$ext;

if (!empty($_FILES)) {
  $tempFile = $_FILES['file']['tmp_name'];                       

  $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;  

  $targetFile =  $targetPath. $name;  

  //Change IP address for host
  $host = "10.10.10.10";
  $port = "21";
  $timeout = "300";
  $ftp = ftp_connect($host,$port,$timeout);
  // Change user and password
  $user = "user";
  $pass = "password";
  ftp_login($ftp,$user,$pass);

 $ret = ftp_nb_put($ftp, $targetFile, $tempFile, FTP_BINARY, FTP_AUTORESUME);

 while (FTP_MOREDATA == $ret)
   {
    // display progress bar, or someting
    $ret = ftp_nb_continue($ftp);
   }
}  ?>