Closed jgrounds closed 11 years ago
Can you post the code you're using? I'm currently using newFileName without any problems. The demo also uses it.
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:
<?php /**
/**
/**
/**
Main class for handling file uploads
*/
class FileUpload {
public $uploadDir = 'resumes/'; // File upload directory (include trailing slash)
public $allowedExtensions = array('doc', 'docx', 'wps', 'pdf', 'txt', 'rtf', 'wpd'); // Array of permitted file extensions
public $sizeLimit = 10485760; // Max file upload size in bytes (default 10MB)
public $newFileName; // Optionally save uploaded files with a new name by setting this
private $fileName; // Filename of the uploaded file
private $fileSize; // Size of uploaded file in bytes
private $fileExtension; // File extension of uploaded file
private $savedFile; // Path to newly uploaded file (after upload completed)
private $errorMsg; // Error message if handleUpload() returns false (use getErrorMsg() to retrieve)
private $handler;
function __construct($uploadName) {
if (isset($_FILES[$uploadName])) {
$this->handler = new FileUploadPOSTForm(); // Form-based upload
} elseif (isset($_GET[$uploadName])) {
$this->handler = new FileUploadXHR(); // XHR upload
} else {
$this->handler = false;
}
if ($this->handler) {
$this->handler->uploadName = $uploadName;
$this->fileName = $this->handler->getFileName();
$this->fileSize = $this->handler->getFileSize();
$fileInfo = pathinfo($this->fileName);
if (array_key_exists('extension', $fileInfo)) {
$this->fileExtension = strtolower($fileInfo['extension']);
}
}
}
public function getFileName() { return $this->fileName; }
public function getFileSize() { return $this->fileSize; }
public function getExtension() { return $this->fileExtension; }
public function getErrorMsg() { return $this->errorMsg; }
public function getSavedFile() { return $this->savedFile; }
private function checkExtension($ext, $allowedExtensions) {
if (!is_array($allowedExtensions)) {
return false;
}
if (!in_array(strtolower($ext), array_map('strtolower', $allowedExtensions))) {
return false;
}
return true;
}
private function setErrorMsg($msg) { $this->errorMsg = $msg; }
private function fixDir($dir) { $last = substr($dir, -1); if ($last == '/' || $last == '\') { $dir = substr($dir, 0, -1); } return $dir . DIRECTORY_SEPARATOR; }
public function handleUpload($uploadDir = null, $allowedExtensions = null) {
if ($this->handler === false) {
$this->setErrorMsg('Incorrect upload name or no file uploaded');
return false;
}
if ($uploadDir !== null) {
$this->uploadDir = $uploadDir;
}
if ($allowedExtensions !== null && is_array($allowedExtensions)) {
$this->allowedExtensions = $allowedExtensions;
}
$this->uploadDir = $this->fixDir($this->uploadDir);
if ($this->newFileName !== null) { $this->fileName = $this->newFileName; $this->savedFile = $this->uploadDir.$this->newFileName; } else { $this->savedFile = $this->uploadDir.$this->fileName; }
if ($this->fileSize == 0) {
$this->setErrorMsg('File is empty');
return false;
}
if (!is_writable($this->uploadDir)) {
$this->setErrorMsg('Upload directory is not writable');
return false;
}
if ($this->fileSize > $this->sizeLimit) {
$this->setErrorMsg('File size exceeds limit');
return false;
}
if ($this->allowedExtensions !== null) {
if (!$this->checkExtension($this->fileExtension, $this->allowedExtensions)) {
$this->setErrorMsg('Invalid file type');
return false;
}
}
if (!$this->handler->Save($this->savedFile)) {
$this->setErrorMsg('File could not be saved');
return false;
}
return true; }
}
$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()));
}
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.
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.
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
}
}
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.
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. :)
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!