Rename File on upload
-
Can an uploaded image be renamed based on the entry information?
Something like DATE-NAME-CATEGORY-TITLE
-
Yes, some fields can be used that way, although not the date — see the help item by “Optional Prefix” in the webforms admin page. The “prefix” can become the entire filename.
But if the prefix is applied on upload there are some gotchas:
1. The user is not able to edit the field after they have uploaded the image (otherwise the filename would have to change)
2. Only option-list fields can be used. (Because most servers have limitations on what can be in a filename)And if the prefix is applied only when you download, you shouldn’t download until after the webform is closed, or the user could change the filename and you end up downloading it twice.
OK, thanks.
So I did this but something seems off.The field Title is not going in there.
What I have: [~cat]-[~sequence]-[~Title]-[~WFM]
What I get: MO-2-__Title_-mcc20202101.jpgThe title was a simple one word title, caroline
any ideas?
also how can a pull in something else that helps identify the user?Did you check “Apply prefix on upload”?
If you do that, text input fields will not become part of the filename. It’s too much of a security risk to store files with names that might not be valid in the unix/linux systems that most webhosts use.
I tired both ways.
If I don’t do it the file is not renamed, even during download.I stand corrected…it did it on the download.
Now…any way for something to identify the user in the name?
I know there is Display Nam and User Login on the spreadsheet. I would love to have an identifying name or id of some kind in the file.I have a work-around for this which does well for us. I’m not sure if you’ll want to dive in to this level though.
In wp-content/plugins/entrywizard/classes/ewz-webform.php
Change the line
$fp = popen("zip -0 -j -q - $fnames", 'r'); // see testing notes
To
$fp = popen("/usr/local/bin/createZipForCompetition.sh $fnames", 'r'); // see testing notesThen create the file /usr/local/bin/createZipForCompetition.sh as follows
#!/bin/bash # Given a list of files, create a zip suitable for download # One of the files will be a CSV TMPDIR=/tmp/CompetitionZIP_$$ mkdir $TMPDIR mkdir $TMPDIR/orig mkdir $TMPDIR/out mkdir $TMPDIR/out/fullname mkdir $TMPDIR/out/titleonly # store arguments in a special array args=("$@") # get number of elements ELEMENTS=${#args[@]} # echo each element in array # for loop for (( i=0;i<$ELEMENTS;i++)); do # echo "Processing ${args[${i}]}" # echo cp "${args[${i}]}" $TMPDIR/orig/ cp "${args[${i}]}" $TMPDIR/orig/ done # We've now copied all the files into the target location # Next we need to process the CSV file LINECOUNT=<code>cat $TMPDIR/orig/*.csv | wc -l</code> # echo "Lines to handle = $LINECOUNT" #Create the data files cat $TMPDIR/orig/*.csv | csvcut -c "Display Name" > $TMPDIR/orig/_DISPLAYNAME.TXT cat $TMPDIR/orig/*.csv | csvcut -c "Filename" > $TMPDIR/orig/_FILENAME.TXT cat $TMPDIR/orig/*.csv | csvcut -c "Title" > $TMPDIR/orig/_TITLE.TXT cat $TMPDIR/orig/*.csv | csvcut -c "Tp" > $TMPDIR/orig/_TYPE.TXT for (( i=2;i<=$LINECOUNT;i++)); do IMAGEAUTHOR=<code>cat $TMPDIR/orig/_DISPLAYNAME.TXT | sed -n -e 1p -e ${i}p | tail -n 1 | tr "<>:\"/\\|?*_" @</code> IMAGEFILE=<code>cat $TMPDIR/orig/_FILENAME.TXT | sed -n -e 1p -e ${i}p | tail -n 1</code> IMAGETITLE=<code>cat $TMPDIR/orig/_TITLE.TXT | sed -n -e 1p -e ${i}p | tail -n 1 | tr "<>:\"/\\|?*_" @</code> IMAGETYPE=<code>cat $TMPDIR/orig/_TYPE | sed -n -e 1p -e ${i}p | tail -n 1 | tr "<>:\"/\\|?*_" @</code> RINDEX=<code>od -A n -t d -N 4 /dev/urandom | tail -c 6</code> # echo "===== ENTRY $i" # echo "Author : >$IMAGEAUTHOR<" # echo "Filename : >$IMAGEFILE<" # echo "Title : >$IMAGETITLE<" # echo "Type : >$IMAGETYPE<" if [ -z "$IMAGEAUTHOR" ] then # echo "***** NO AUTHOR - giving up" continue fi if [ -z "$IMAGEFILE" ] then # echo "***** NO FILE - giving up" continue fi if [ -z "$IMAGETITLE" ] then # echo "***** NO TITLE - giving up" continue fi if [ -z "$IMAGETYPE" ] then # echo "***** NO TYPE - creating a standard name" TARGETFULL="${IMAGETITLE}_${IMAGEAUTHOR}.jpg" TARGETSHORT="${RINDEX}_${IMAGETITLE}.jpg" else # echo "***** HAS TYPE - creating an extended name" TARGETFULL="${IMAGETITLE}_${IMAGETYPE}_${IMAGEAUTHOR}.jpg" TARGETSHORT="${RINDEX}_{IMAGETYPE}_${IMAGETITLE}.jpg" fi SOURCEFILE=$(printf '%q' "$IMAGEFILE") TARGETFILEFULL=$(printf '%q' "$TARGETFULL") MYCMDFULL="cp $TMPDIR/orig/${SOURCEFILE} $TMPDIR/out/fullname/${TARGETFILEFULL}" eval $MYCMDFULL TARGETFILESHORT=$(printf '%q' "$TARGETSHORT") MYCMDSHORT="cp $TMPDIR/orig/${SOURCEFILE} $TMPDIR/out/titleonly/${TARGETFILESHORT}" eval $MYCMDSHORT done cd ${TMPDIR}/out zip -r -q -0 ${TMPDIR}/generatedzip.zip * cat ${TMPDIR}/generatedzip.zip cd / rm -rf ${TMPDIR}You may need to update to grab the specific columns you want from the CSV file generated for inclusion in the filename.
We run this on a Linux hosted environment and not hit any problems yet. We strip out the problematic characters from the titles before renaming the file (tell people that they can’t have things like * < > | etc).
Works for us, but we need to remember to re-modify the plugin code every time we update.
Yeah…that is a little deep.
I get it, I think…HA. It is pretty much what I am doing outside of the site. I create a name based on the content of the Excel file and then copy the source files into a new folder with the new name. Simple Concatenate in Excel creates the filename I desire and then the copy does the rest. Ugly, but works.I was just hoping at the very least I could use Display Name or User Login since they were in the output excel as part of the file name. It is really the only thing missing from my ugly means of doing it.
To Wildcardinal, if you make changes to the EW code, what happens with a new update from Josie?
To Mcollen – yes, it’s the same as you are doing but getting it done on the server first. As long as there is a field in the CSV you can use it as part of the filename creation.
To Ronc – the down side is that when ever Josie puts out an update I have to go back in and make the update. Not great, but this approach does make it easier for us. I did raise thread asking if the command used to create the zip could be configurable, but Josie wasn’t keen (understandably). Thread here.
I was able to pull UID out which is the Worpress User ID.
What am I putting in the prefix to pull User Name or User Login?Sorry, but I’m afraid there is currently no way to do that, because the user login could contain characters that are not safe as part of filenames on a unix/linux system.
At this point, entrywizard has become so complex that I don’t want to add more features until I have had time to write a lot more automated tests than I have at the moment. Every new feature adds a risk of introducing a new bug.
Thanks so much for all the help, Josie.
I understand what you’re saying, I just had to ask. Most of the time I overlook the simple solution.Is there any documentation on Custom Fields?
See my reply to your separate query.
Don’t expect EntryWizard to do everything for you. If you need files named in a specific way, create those filenames by renaming after download using data from fields you can include in the downloaded spreadsheet.
I also translate the WP user account to be a preferred version of the entrant’s name.
The topic ‘Rename File on upload’ is closed to new replies.