John Beasley
John Beasley

Reputation: 3075

PHP - renaming a file before uploading

I am trying to build off a question I asked yesterday.

I am able to pass the file over to PHP using the ajax method. But I need to be able to change the file name to the booking number. For some reason, the booking was not being passed over to the PHP script. So I attempted the following:

$('#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);}     
  });
});

As you will notice above, I had to use the $.post method to send the booking and partner over to the php script.

I then used $.ajax to send the form_data over to the same script.

(I could not achieve this in one motion from yesterday's question I asked. So this is my second attempt to complete this. If there is a way to send all of the info in one motion, please refer to the question I linked above.)

So over in the PHP script, I am able to get all of the items I needed with a couple of functions:

<?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;
  }
?>

I know it's not pretty, but I am able to get the booking (I don't need the partner right now), and I am also able to get the file information.

What I need to do is rename the file to the booking, and then finally upload it to the necessary directory.

I wasn't sure if I had to combine the functions, but I did try to no avail.

With that said, I am able to get the booking and file info within the PHP script. Now how would I go about renaming the file to the booking?

Upvotes: 0

Views: 97

Answers (2)

Philipp
Philipp

Reputation: 15629

To fix your ajax request (especially the illegal invocation), use the following javascript code

$('#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); }
    });
});

Notice the use of processData: false and contentType: false

Upvotes: 1

RiggsFolly
RiggsFolly

Reputation: 94662

As you used form_data.append() to add the file data to the formdata. did it not occur to you to also use that to add the booking and partner values to it as well?

$('#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());

    $.post('process/fileUpload.php', form_data, function(data)
        {
            console.log(data);
        });
});

Upvotes: 2

Related Questions