Raymond Geerts Posted March 8, 2013 Share Posted March 8, 2013 Is it possible to rename a file with WireUpload. I can image when the file is moved from the temp folder to the destination path there is a chance to rename it? For example i like to rename the file to some md5 hash. I have tried the following but that didnt rename the image: // execute() returns an array, so we'll foreach() it even though only 1 file foreach($u->execute() as $key => $filename) { $parts = pathinfo($filename); $u->setTargetFilename(md5($parts['filename']).$parts['extension']); $entry->img->add($filename); } Link to comment Share on other sites More sharing options...
Raymond Geerts Posted March 8, 2013 Author Share Posted March 8, 2013 I have got it working now. But this will only work with 1 file upload. If anybody has a better way that also will work with multiple files please post it here $entry = new Page(); $entry->template = 'pollanswer'; $entry->parent = $qUpload; // setting the normal fields $entry->name = date("YmdHis").'_'.$sanitizer->pageName($fields['naam'], true); $entry->title = $fields['naam']; $entry->email = $fields['emailadres']; $entry->datetime = date("U"); $entry->save(); // instantiate the class and give it the name of the HTML field $u = new WireUpload('uploads'); $u->setMaxFiles(1); $u->setOverwrite(false); // have it put the files in their final destination. this should be okay since // the WireUpload class will only create PW compatible filenames $u->setDestinationPath($entry->img->path); // tell it what extensions to expect $u->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png')); // set target filename as md5 hash to prevent showing original filename $parts = pathinfo($_FILES['uploads']['tmp_name']); $u->setTargetFilename(md5($parts['filename']).$parts['extension']); // execute() returns an array, so we'll foreach() it even though only 1 file foreach($u->execute() as $key => $filename) { $entry->img->add($filename); //$entry->img->eq($key)->description = $description[$key]; } // save the page $entry->status = Page::statusUnpublished; $entry->save(); Edit: in this example i used some variables that work specificly in my own website. For a working version you need to replace some values! Link to comment Share on other sites More sharing options...
Soma Posted March 8, 2013 Share Posted March 8, 2013 You can't use setTargetFilename on multiple. You should be able to just rename them after uploading: foreach($u->execute() as $key => $filename) { $file = pathinfo($filename); $newfile = md5($file['filename']) . "." .$file['extension']; @rename($entry->images->path . $filename, $entry->images->path . $newfile); $entry->images->add($newfile); } However this works, you create a new page everytime which stays there if there's an error. Best practice would be to first upload to a protected temp folder and create page and add files if everything went well... See also my script example here: https://gist.github.com/somatonic/4150974 1 Link to comment Share on other sites More sharing options...
Raymond Geerts Posted March 8, 2013 Author Share Posted March 8, 2013 Yes indeed i noticed that already when something went wrong while uploading there still would be made a new page without an image. I have taken a look at your example script and the aproach you used is much better so that no images in the temp folder or pages without an image will stay behind. Thanks for the example Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now