【问题标题】:Uploadify rename more than one fileUploadify 重命名多个文件
【发布时间】:2013-06-23 01:10:33
【问题描述】:

我在一个项目上使用uploadify,我有php脚本重命名一个文件,但是如果一次上传多个文件,我不确定如何重复这个过程?

我的 php 脚本在下面...

$targetFolder = '/img/uploads'; // Relative to the root

$verifyToken = md5('unique_salt' . $_POST['timestamp']);

if (!empty($_FILES) && $_POST['token'] == $verifyToken) {

  $tempFile = $_FILES['Filedata']['tmp_name'];
  $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;

  $fileParts = pathinfo($_FILES['Filedata']['name']);
  $unique_hash = hash_hmac("md5", file_get_contents($_FILES['Filedata']['name']), SALT);
  $targetFile = rtrim($targetPath,'/') . '/' . $unique_hash .'-'.$_POST['userId'].'.'. $fileParts['extension'];


  #$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];

  // Validate the file type
  $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
  $fileParts = pathinfo($_FILES['Filedata']['name']);

  if (in_array($fileParts['extension'],$fileTypes)) {
    move_uploaded_file($tempFile,$targetFile);
    echo '1';
  } else {
    echo 'Invalid file type.';
  }
}

【问题讨论】:

  • Foreach 通过什么?

标签: php uploadify


【解决方案1】:

当您使用 ajax 时,您可以一个一个地为每个文件发送请求,然后您不必使用 foreach 或者您一次发送表单并使用下面的代码。

当您将所有文件一起发送时,您将在您的表单中使用类似这样的内容:name=userfile[],因此您可以像这样 foreach 文件:

foreach ($_FILES as $file) 
{     
 $uploadfile = $uploaddir . basename($file['name']);
 if (!move_uploaded_file($file["tmp_name"], $uploadfile)) 
 {
  echo set_e('error','Image ['.$i.'] not uploaded','');
 } 
}

【讨论】:

    【解决方案2】:

    您是否尝试过不止一个? Uploadify 使用 jQuery ajax 处理文件?它可能看起来是同时的,但它通过 ajax 一次处理一个文件。它会根据队列中的文件多次调用您的upload.php。因此,如果您的函数适用于一个文件,那么它将适用于其余文件。您只需要确保每次调用函数时它都在目录中写入一个唯一的文件名,看起来确实如此。 Uploadify 处理循环遍历每个文件名,然后调用您的上传脚本。

    http://www.uploadify.com/demos/

    使用 Chrome 或 Firebug 查看网络控制台,并在演示中上传一堆图片。您会看到每个文件都调用了 upload.php 脚本。

    仅供参考, 我曾经使用uploadify,但我改为PLUPLOAD http://www.plupload.com/。也支持多次上传,但文档更好,更容易与优秀的 api 一起使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-13
      • 1970-01-01
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 2019-11-02
      相关资源
      最近更新 更多