WordPress upload Chinese name image automatically renamed two times development

Source: Internet
Author: User
Tags php file

These days with WordPress set up a picture station, fill in the contents of a problem when I upload pictures in the Media Library some pictures can be normal display, but the individual picture is not showing the thumbnail, this problem really let me tangled up in the afternoon. No way to find the cause of the research code, silently wear glasses hard work!

Why the picture doesn't appear

As the saying goes: "Kung fu is not hard to people," the problem is I have found that the original coding problem if we upload the picture name is Chinese so WP thumbnail mechanism can not handle so there is a problem with me.

In the wp-admin/includes/file.php file, there is a section of code in which line 2nd controls the naming of our uploaded images:

Move the file to the uploads dir
$new _file = $uploads [' Path '].  "/$filename"; The newly uploaded picture is displayed by the name of the upload
if (false = = @ Move_uploaded_file ($file [' Tmp_name '], $new _file)) {
if (0 = = Strpos ($uploads [' Basedir '], Abspath))
$error _path = Str_replace (Abspath, ", $uploads [' Basedir ']). $uploads [' SubDir '];
Else
$error _path = basename ($uploads [' Basedir ']). $uploads [' SubDir '];
Return $upload _error_handler ($file, sprintf (' The uploaded file could is moved to%s. '), $error _path));
}

Since the location to the problem is of course is to quickly resolve off so that the picture is normal display!

Processing Method Rollup
Since the problem surfaced, according to a mother's loyal fans of course, Baidu first to find the answer, the following is Baidu found 3 kinds of answers.

First: Modify WP's program file file.php

Find the following code in file.php

$new _file = $uploads [' Path ']. "/$filename";

Modify the above code into

$new _file = $uploads [' Path ']. "/". Date ("Ymdhis"). Floor (Microtime () *1000). $ext;

The results displayed are named in a format such as "a number of seconds + thousand milliseconds".

This method has a lot of drawbacks: the first uploaded pictures will all be renamed; the second WP updates to the new version and modifies the code.

Second: Add code to subject functions.php

function Wpyou_rename_upload_file_prefilter ($file) {
$time =date ("y-m-d");
$file [' name '] = $time. "". Mt_rand (1,100). ".". PathInfo ($file [' name '], pathinfo_extension);
return $file;
}
Add_filter (' Wp_handle_upload_prefilter ', ' wpyou_rename_upload_file_prefilter ');

The effect of implementation and the first result is the same, so that each update WP do not have to repeatedly modify the program file, but the drawbacks or there is the upload of all the pictures will be renamed.

The perfect solution

If the above method changes, I uploaded the picture will be renamed for me to use the cloud to store pictures of users is a problem, so I can only do it myself. Here's a perfect solution, when you upload a picture, the subject will detect whether the image name contains Chinese characters, if it contains the mechanism to perform renaming, if not included so directly with the name of the upload as a picture name, directly add the following code to the subject of the functions.php file.


function Tin_custom_upload_name ($file) {
if (preg_match) ('/[one-?] /u ', $file [' name ']):
$ext =ltrim (STRRCHR ($file [' name '], '. '), '. ');
$file [' Name ']=preg_replace (' #^www\.# ', ', ', Strtolower ($_server[' server_name ']). ' _ '. Date (' Y-m-d_h-i-s '). $ext;
endif
return $file;
}
Add_filter (' Wp_handle_upload_prefilter ', ' Tin_custom_upload_name ', 5, 1);

The problem of not showing thumbnails in Chinese pictures through the code fragment above is finally solved perfectly. It seems that later still want to study some of the functions of WP program AH! Write this method here today hope can help to meet with me the same problem friend!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.