How to generate test files for uploading during development or troubleshooting
We recently had a client request for improvement to a form where the specific size of a file upload was causing a problem. This meant that to test our use cases properly, I needed some files to upload that were specific sizes:
ie, a file that was greater than 2MB but less than 32MB.
ie, a file that was greater than 32MB but less than 256MB.
ie, a file that was greater than 256MB but less than 512MB.
I read a really useful post on how to generate test files of particular file size on the common OS platforms on Stottmeister's Blog that proved quite useful for me.
Mac OS:
mkfile <file size> <filename>
Example:
mkfile 40M testfile.txt
Linux:
dd if=/dev/zero of=<filename> bs=<initial blocksize in bytes> count=<iterations of the blocksize>
Example:
dd if=/dev/zero of=testfile.txt bs=28M count=1
Windows:
C:\>fsutil file createnew <filename> <filesize in bytes>
Example:
C:\>fsutil file createnew C:\testfile.txt 1024
The process to create these files is pretty quick too - so it's much easier to generate a test file than to search around your hard drive for a file of appropriate size. Not to mention - if you work with many clients; you don't want to be uploading files that belong to other clients just so you can test a use case.
Written by Tim Fernihough
Related protips
5 Responses
This is great, thanks for all the different examples.
Awesome, thanks! I never knew there's even a built-in utility on Windows!
Glad that it was helpful to you guys!
That's great, thanks for sharing. Another useful thing to test for with file uploads is to upload a document containing a string that looks like MIME part separator! One of our vendors had a problem with a specific document being uploaded that contained the string: ==00OO00== and their mime decoder truncated the file there!
@alchemycs I could definitely see that being a pain point for sure! If you are writing your own upload library, I could see that being something the developer could easily control but if they are using something pre-built like the Webforms in Drupal; wonder how we'd be able to get around that? Creating a patch and submitting it against the Webform Module I guess. :)