PHP-在上传之前重命名文件

时间:2018-06-22 16:51:35

标签: php jquery function file-upload file-rename

我正在尝试建立a question I asked yesterday

我可以使用ajax方法将文件传递给PHP。但是我需要能够将文件名更改为预订号。由于某种原因,预订未传递到PHP脚本。因此,我尝试了以下操作:

$('#uploadBtn').on('click', function()
{
  var form_data = new FormData();
  form_data.append("file", document.getElementById('pdfFile').files[0]);
  var booking = $('#bookingNum').val();
  var partner = $('#partnerCode').val();

  $.post('process/fileUpload.php', {booking:booking, partner:partner}, function(data)
  {
    // Wasn't sure if I needed anything here
    console.log(data);
  });

  $.ajax({
    url: 'process/fileUpload.php',
    method:"POST",
    data: form_data,
    contentType: false,
    cache: false,
    processData: false,
    success: function(data){console.log(data);},
    error: function(jqHHR, textStatus, errorThrown){console.log('fail: ' + errorThrown);}     
  });
});

正如您将在上面注意到的那样,我不得不使用$ .post方法将预订和合作伙伴发送到php脚本。

然后我使用$ .ajax将form_data发送到同一脚本。

(我昨天提出的问题无法在一个动议中实现。因此,这是我第二次尝试完成此任务。如果可以通过一个动议发送所有信息,请参阅我链接的问题以上)。

因此,在PHP脚本中,我可以通过几个函数来获取所需的所有项目:

<?php
  // from the $.post method
  if(isset($_POST['booking']))
  {
    $booking = $_POST['booking'];
    $partner = $_POST['partner'];
    getInfo($booking);
  }
  // from the $.ajax method
  if($_FILES['file'])
  {
    $file = var_dump($_FILES['file']);
    getFile($file);
  }

  function getInfo($booking)
  {
    return $booking;
  }

  function getFile($file)
  {
    return $file;
  }
?>

我知道它并不漂亮,但是我可以预订(我现在不需要合作伙伴),而且我也可以获取文件信息。

我需要做的是将文件重命名为预订,然后最后将其上传到必要的目录。

我不确定是否必须组合功能,但是我确实没有用。

话虽如此,我能够在PHP脚本中获取预订和文件信息。现在我该如何将文件重命名为预订?

2 个答案:

答案 0 :(得分:2)

使用#include <cuda_runtime_api.h> #include <cuda.h> #include "QCUDA.cuh" template<typename T> __host__ void QCUDA::CUDAGPU<T>::convertSTLToThrust(const std::valarray<std::complex<T>>& m1, const std::valarray<std::complex<T>>& m2, const int m, const int n) { std::cout << "Before resizing hostVecA_: " << this->hostVecA_.size() << std::endl; this->hostVecA_.resize(m * n, 0); std::cout << "After resizing hostVecA_: " << this->hostVecA_.size() << std::endl; std::cout << "Before resizing hostVecB_: " << this->hostVecB_.size() << std::endl; this->hostVecB_.resize(m * n, 0); std::cout << "After resizing hostVecB_: " << this->hostVecB_.size() << std::endl; } template class QCUDA::CUDAGPU<double>; 将文件数据添加到formdata时。难道您还没有使用它来向其中添加预订和合作伙伴值吗?

form_data.append()

答案 1 :(得分:1)

要修复您的ajax请求(尤其是非法调用),请使用以下javascript代码

.route

注意使用$('#uploadBtn').on('click', function() { var form_data = new FormData(); form_data.append("file", document.getElementById('pdfFile').files[0]); form_data.append('booking', $('#bookingNum').val()); form_data.append('partner', $('#partnerCode').val()); $.ajax({ type: "POST", url: 'process/fileUpload.php', data: form_data, processData: false, contentType: false, success: function(data) { console.log(data); } }); }); processData: false