2

There is an error message in CodeIgniter

The file name you submitted already exists on the server

Does that make any sense? Let me explain.

There is a preference in File Uploading Class preferences called "overwrite". It can be TRUE or FALSE. The default value is FALSE. If it's TRUE, then the file will be overwritten. If it's FALSE, then a number will be appended to the filename if another with the same name exists. So, either it'll overwrite the file or it will rename the file. How is it possible to get the above error?

So, it looks like CodeIgniter is unable to rename the files properly.

I looked at my database and found how CodeIgniter has renamed the files. Though there are no other file exists with the same name, some of the filenames are appended an underscore ( _ ), but no number after that. I also found that when I tried to upload a file with file name as "myfilename.zip" several times, "myfilename_.zip", "myfilename_1.zip", "myfilename_2.zip" ... "myfilename_11.zip" found at the upload path. But "myfilename.zip" is not found there and instead of creating "myfilename_12.zip", it showed me the above error.

So, where is the issue? How to solve this?

Update: Now I understood why I am getting the error message The file name you submitted already exists on the server. The mechanism to check whether any file with the same name exists work for 100 files. It checks whether any file with the same name exists, if found, then it appends a number to filename and check again. This happens 100 times. If still a file with the same name found, it stops, so that the execution of the script will not take much time. So, we can say there is a limit and 100 is a good limit.

But I am still looking for the answer, why some files are appended an underscore, some with an underscore and then a number, and some with just the number. There is no consistency with the pattern to append a number to the files, if another file with the same name exists.

4

3 回答 3

1

我通过设置解决了这个问题$config['encrypt_name'] = TRUE

于 2013-11-18T10:04:26.330 回答
0

我查看了我的数据库,发现 CodeIgniter 是如何重命名文件的。虽然不存在同名的其他文件,但有些文件名附加了下划线 (_),但后面没有数字

发生这种情况是因为默认情况下,下面的选项将空格更改为下划线...如果您想保留空格,请将其更改为 FALSE。

remove_spaces   TRUE

关于您的"The file name you submitted already exists on the server"信息-您将不得不做类似...

if($_FILES['image']['error'] == 0){
   $file_name = $_FILES['image']['name'];
   if(file_exists($upload_dir.$file_name)) {
      show_error('The file name you submitted already exists on the server!');
   } else {
      //do_upload() etc
   }
}

我还没有测试过这段代码,但它应该会给你一些想法。

于 2013-04-25T07:49:23.747 回答
0

您还没有将代码放在这里。但是,我认为您已使用 [file_name] 索引将文件名设置为固定文件名,并对其进行了约束。

于 2013-04-25T07:39:44.660 回答