ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Safecracker File: "The file name you submitted already exists on the server"

October 08, 2012 8:45am

Subscribe [3]
  • #1 / Oct 08, 2012 8:45am

    Digitaal Dier

    75 posts

    Hey guys,

    One of my clients is getting the error mentioned in the title when uploading files through Safecracker. I’ve already checked write-permissions on the cache-folder, and allowed filetypes in the file-upload settings and it’s all been set properly. Why are we getting this error now? Is this a setting we can disable somewhere, or for example make safecracker append it with _x?

    Thanks,

    - Steven

  • #2 / Oct 08, 2012 11:55am

    James Smith

    259 posts

    I just had a quick peek at this file, which is where the error is being generated: /system/expressionengine/libraries/EE_Upload.php (line 86). Just before that (lines 75-82) it seems to loop through 100 times trying to generate a new filename with the loop iteration number appended to the end. Do you already have 100 instances of that filename uploaded?

  • #3 / Oct 08, 2012 12:01pm

    Digitaal Dier

    75 posts

    That is very much possible. They’re using EE as an e-Learning environment for little kids. The kids just have to name the tests they upload “Test #.[doc/ppt/docx/pptx]”. Is there any other way to handle this?

  • #4 / Oct 08, 2012 12:23pm

    James Smith

    259 posts

    I’ve never used Safecracker File before myself, but it looks like there’s an extension hook that you could use post-save (pre-save would obviously be much more helpful, but that doesn’t seem to exist!!):

    http://ellislab.com/expressionengine/user-guide/development/extension_hooks/global/filemanager/index.html

    Assuming you remove enough files from the directory to get back under 100 (so that the file uploads ok) you could write a pretty simple extension that appends, say, the logged-in member_id or a random hash to the filename after it has uploaded so that the next time someone attempts to upload it would succeed.

    Otherwise, you could hack the core with a function that properly figures out the highest numbered file and increments the digit accordingly: I took this code from the old nGen File Upload fieldtype from back in the EE1 days (http://www.ngenworks.com)... hopefully they don’t mind me pasting it here:

    // Check if file exists already, if it does add an increment to the file name
       if( file_exists($upload_path . $file_name) ) {
        $matching_files = glob($upload_path . $file['name'] . "*" . $file['ext']);
     
        // Find highest number, add 1 and set new $file_name
        sort($matching_files);
        preg_match("/" . $file['name'] . "_(\d+)\." . substr($file['ext'], 1) . "/", basename(end($matching_files)), $matches);
        
        if( isset($matches[1]) && $matches[1] ) {
         $increment = "_" . ($matches[1] + 1);
        } else {
         $increment = "_1";
        }    
        $file_name = $file['name'] . $increment . $file['ext'];
       }

    Just don’t be tempted to do it client-side with JavaScript…

  • #5 / Oct 08, 2012 12:57pm

    Dom Stubbs

    156 posts

    I’m not particularly clued up on Safecracker, but it looks as though there is a safecracker_submit_entry_start hook. I’d have thought there’s a good chance you could use that to manipulate the $_FILES data before it reaches the set_filename function.

    The odd thing is the native upload function does seem to support random/encrypted filenames, but there doesn’t seem to be anywhere to enable that, even via hidden config vars (feature request here).

  • #6 / Oct 09, 2012 5:54pm

    Kevin Smith

    4784 posts

    Thanks for the help on this one, James and Dom.

    Steven, based on the digging they did, it sounds like the solution to this one will require some custom development. Would you like me to move this thread over to Development and Programming to see if some developers could toss in with ideas here?

  • #7 / Oct 10, 2012 3:19am

    James Smith

    259 posts

    If there is indeed a hardcoded 100 limit it’s probably also worth a bug report

  • #8 / Oct 11, 2012 4:38pm

    Kevin Smith

    4784 posts

    I hear ya, but there would have to be some kind of limit there so that EE doesn’t endlessly loop. Hitting that limit is usually a signal to find a better way to accomplish this since that would mean there are already 100 files of the same name that have been uploaded.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases