LPology / Simple-Ajax-Uploader

Javascript file upload plugin with progress bar support. Works in all major browsers, including IE7+, Chrome, Firefox, Safari, and Opera. No dependencies - use it with or without jQuery.
995 stars 268 forks source link

Re: setting newFileName #11

Closed jgrounds closed 11 years ago

jgrounds commented 11 years ago

I was wondering if anyone has got the newFileName / customFileName working? All I have managed to do is get the file renamed to - customFileName.ext etc. I need to rename the file to a randomly generate variable. i.e. how does one replace example - customFileName.doc with random variable.doc ? Thank you!

LPology commented 11 years ago

Can you post the code you're using? I'm currently using newFileName without any problems. The demo also uses it.

jgrounds commented 11 years ago

Hey, thanks for replying. Here is the page I'm working on, you can get an idea what I'm attempting. basically want to rename files so I don't get 3 resume.doc files uploaded and overwritten. http://www.firstchoice.peoplefirstcorp.com/pf_application_job_test.asp?code=pxdmm513

When I edit this line in the uploader.php file, I only get customFileName.doc etc, when I try substituting a variable, it breaks down. $Upload->newFileName = 'customFileName.'.$ext; eg: $Upload->newFileName = $myvariable.'.'.$ext;

Here is my complete uploader.php file:

Thanks for your help.

<?php /**

/**

/**

/**

}

$Upload = new FileUpload('uploadfile'); $ext = $Upload->getExtension(); // Get the extension of the uploaded file $Upload->newFileName = 'customFileName.'.$ext; $result = $Upload->handleUpload($upload_dir, $valid_extensions);

if (!$result) { echo json_encode(array('success' => false, 'msg' => $Upload->getErrorMsg()));
} else { echo json_encode(array('success' => true, 'file' => $Upload->getFileName())); }

?>

LPology commented 11 years ago

A few things...

Instead of adding code to Uploader.php, include it into a separate script. You shouldn't need to make any changes to the Uploader.php file.

With your current code, the new file name will always be "customFileName". If you want to use a random file name, you can't set the file name as a string, it must be a variable. One way is to use PHP's uniqid() function (more info here).

<?php
require('Uploader.php'); // include Uploader.php

$upload_dir = '/uploads/'; // some directory where we're saving uploaded files
$valid_extensions = array('gif', 'png', 'jpeg', 'jpg'); // allowed extensions (optional)

$Upload = new FileUpload('uploadfile');
$ext = $Upload->getExtension(); // Get the extension of the uploaded file
$newName = uniqid().'.'.$ext;
$Upload->newFileName = $newName;
$result = $Upload->handleUpload($upload_dir, $valid_extensions);

if (!$result) {
    echo json_encode(array('success' => false, 'msg' => $uploader->getErrorMsg())); 
} else {
    echo json_encode(array('success' => true, 'file' => $newName));
}

Hope this helps.

jgrounds commented 11 years ago

Thanks a stack!!

It works great now.

I just need to work out how to pass the $newName back to the page so I can submit it with the form and it's done.

From: LPology, LLC [mailto:notifications@github.com] Sent: Monday, July 08, 2013 8:15 PM To: LPology/Simple-Ajax-Uploader Cc: jgrounds Subject: Re: [Simple-Ajax-Uploader] Re: setting newFileName (#11)

A few things...

Instead of adding code to Uploader.php, include it into a separate script. You shouldn't need to make any changes to the Uploader.php file.

With your current code, the new file name will always be "customFileName". If you want to use a random file name, you can't set the file name as a string, it must be a variable. One way is to use PHP's uniqid() function (more info here http://php.net/manual/en/function.uniqid.php ).

<?php require('Uploader.php'); // include Uploader.php

$newName = uniqid(); // use unique string for new file name $upload_dir = '/uploads/'; // some directory where we're saving uploaded files $valid_extensions = array('gif', 'png', 'jpeg', 'jpg'); // allowed extensions (optional)

$Upload = new FileUpload('uploadfile'); $ext = $Upload->getExtension(); // Get the extension of the uploaded file $Upload->newFileName = $newName.'.'.$ext; $result = $Upload->handleUpload($upload_dir, $valid_extensions);

if (!$result) { echo json_encode(array('success' => false, 'msg' => $uploader->getErrorMsg())); } else { echo json_encode(array('success' => true, 'file' => $filename)); }

Hope this helps.

— Reply to this email directly or view it on GitHub https://github.com/LPology/Simple-Ajax-Uploader/issues/11#issuecomment-20645579 .Image removed by sender.

LPology commented 11 years ago

Glad I could help.

I just updated the code in my comment. Now after a successful upload, the new file name is returned from the server. You can get the returned file name from the server response in the onComplete() function with something like this:

onComplete: function(file, response) {
  if (response && response.success === true) {
    var newName = response.file;
    alert('Uploaded file name: ' + newName); // or whatever you want to do with it
  }
}
jgrounds commented 11 years ago

Perfect, thank you.

I'd like to send you a little thank you in the mail, how can I contact you?

From: LPology, LLC [mailto:notifications@github.com] Sent: Monday, July 08, 2013 9:11 PM To: LPology/Simple-Ajax-Uploader Cc: jgrounds Subject: Re: [Simple-Ajax-Uploader] Re: setting newFileName (#11)

Glad I could help.

I just updated the code in my comment. Now after a successful upload, the new file name is returned from the server. You can get the returned file name from the server response in the onComplete() function with something like this:

onComplete: function(file, response) { if (response && response.success === true) { var newName = response.file; alert('Uploaded file name: ' + newName); // or whatever you want to do with it } }

— Reply to this email directly or view it on GitHub https://github.com/LPology/Simple-Ajax-Uploader/issues/11#issuecomment-20647522 .Image removed by sender.

LPology commented 11 years ago

Oh gosh, you don't have to do that. Saying "thank you" is plenty enough for me.

Tell you what, if you really want to do something, just "Star" the repo at the top of the page to tell others that you thought the plugin was helpful (currently, 16 ppl have starred it). That would definitely beat a thank you note :)

Oh, and if you hear of anyone needing an upload plugin, send them this way!

OR, if you REALLY want to go big, you could add a link to the project in the credits section or wherever on your web site That's not necessary of course, but if you just want to. :)