Jump to content
hzesen1221

Unable to upload an image with non-ascii (Chinese) characters

Recommended Posts

I tried uploading an image with an English filename which went through perfectly fine. Then I copied and pasted the same exact file and gave it a Chinese name instead. Next I attemped to upload this image however I got this error message:

"Unable to move uploaded file ..."

I am not sure how to solve this problem. Any help would be greatly appreciated!

Share this post


Link to post
Share on other sites

Hi there !

The forum of processwire has more than 4 years of posts and has become a great source of information.

Your answer is already in there. Did you have a look ?

Welcome to processwire and why not tell us something more and introduce your self.

Share this post


Link to post
Share on other sites

This is made by design to avoid problems with handling files on wide range of server OCs. At least, this is how I inderstand it. You can solve the problem by renaming files on upload with this Adrian's module. Have not tried it myself, so please report back if you choose to go this way ))

Actually I would like to read more about this myself, if someone could share some links.

  • Like 2

Share this post


Link to post
Share on other sites

Hello Ivan thanks for your help. I actually tried that module before posting this thread. Unfortunately it did not work for me. For files with English names it worked like a charm. However that is not my problem here. For files with Chinese names I still got the same error. 

And hello Pwired (and anyone else). I am the owner of a small web development firm here in China. However I studied in the U.S. for the last five years so I am exposed a lot to all these 'foreign technologies' and we used these tech exclusively to differentiate ourselves. My company is currently using CouchCMS to develop sites for clients. However I am looking for another more extensible alternative and I found Processwire. I just have to make sure that it will work properly for my clients. 

Share this post


Link to post
Share on other sites

Update: If the image's filename contains ONLY Chinese characters I got the same error as before. However, if it has any ascii character in it (eg: numbers, letters, punctuation), for example: “未命名-1.jpg”, the module will kick in and rename the uploaded file name for me. Could anyone explain this phenomenon to me? 

Thank you very much.

Zesen 

  • Like 1

Share this post


Link to post
Share on other sites

Just guessing: the module renames only those characters it is aware of. So "未命名-1.jpg" gets renames to something like "____-1.jpg" and if there is no other files whick got renamed to same kind of nonsense name there would be no filename conflict. So you can upload "未命名-2.jpg" to the same field, but not "名未命-1.jpg".

Probably there is a way to include some transliteration table/array for simlified chinese to the module, but Adrian has to step in here (as I may be wrong about all this).

Share this post


Link to post
Share on other sites

Hi guys,

A quick guess here - I think PW itself is likely stripping all the non latin characters from the filename as the first step. So if there are no latin characters at all, there is nothing left and so the file is rejected. If there is at least one latin character then it can continue to the point where my module hooks in and sets the new filename.

I think these are the two relevant spots in the PW core:

https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/core/WireUpload.php#L213

https://github.com/ryancramerdesign/ProcessWire/blob/a8024bb49785370aa2e3867bd683094663cfeabf/wire/core/Pagefiles.php#L264

Not really sure what can be done about this at the moment. Here is one other relevant post:

https://processwire.com/talk/topic/4387-is-it-possible-to-retain-non-english-filenames/

  • Like 1

Share this post


Link to post
Share on other sites

Please help me on this!

I followed your links, Adrian, and it seems like in validateFilename function there is already a possibility to get the transliterated name for a file by passing "TRUE" instead of "Sanitizer::translate" to filename method.

$value = $this->wire('sanitizer')->filename($value, Sanitizer::translate); 

I am so unexperienced in this, that I may be completely wrong. And if I am not, I still do not know how to hook and change that constant.

But if there is a way to transliterate a name for a page there must be a way to transliterate a filename. We just need to find out how )))

Share this post


Link to post
Share on other sites

Actually I was just working on filename cleaning for Migrator and learned a little more :)

I played around with sanitizer->filename and didn't achieve what I wanted, so I ended up making use of:

https://github.com/ryancramerdesign/ProcessWire/blob/7b9730d43e6eda8bd27841a4811a1b9737bfe903/wire/core/Pagefiles.php#L302

public function cleanBasename($basename, $originalize = false, $allowDots = true, $translate = false)

Does that translate option help for you guys?

This is where it is used during Pagefile::install (which is when a file is added to the system)

https://github.com/ryancramerdesign/ProcessWire/blob/7b9730d43e6eda8bd27841a4811a1b9737bfe903/wire/core/Pagefile.php#L91

Notice that it is set to true here, even though the default for the function is false.

  • Like 1

Share this post


Link to post
Share on other sites

Hello Adrian. Thanks for your answer! However I'm still not quite sure what you were suggesting.. Could you please elaborate a little bit more? I looked into that cleanBasename function and sort of understand what it does / what its purpose is. I'm pretty sure you are not suggesting that I should modify this core function right?

Thank you very much.

Share this post


Link to post
Share on other sites

Sorry - no I am not suggesting you modify the core - it was just meant to be some info to show where the sanitizing of the name is occurring.

I just tested an image upload that I named: 名未命.png and it worked perfectly with Custom Upload Names installed - it was renamed to exactly what the rule specified. Without it installed it still uploaded but got renamed to png.png which is a bit weird, but it still worked.

What version of PW are you running. I am running latest dev. Does it make a difference for you if you upgrade?

  • Like 1

Share this post


Link to post
Share on other sites

I am actually running the latest stable version that I downloaded a few days ago, which I believe is 2.5.2. After a fresh installation I am still getting this error: 

名未命.png - Unable to move uploaded file to: /var/www/..........

Maybe I should try the latest dev version as well however I don't think that's gonna make a difference though...

Do you guys have any idea regarding to why this is happening to share with me? I would really appreciate any inputs!

Share this post


Link to post
Share on other sites

It seems like it might be server specific. I just tried again on my local dev setup and it works as I reported in my last post. But then I just tried via lightning.pw and it failed with the same message you are getting.

I honestly don't know why though. Do you have another environment you might be able to test with?

Share this post


Link to post
Share on other sites

I tried another hosting but unfortunately I am getting the same error. I might need to fall back to Perch CMS for this upcoming project then, which I think does less for more (although it's indeed easier than Processwire to set up in my opinion). I will come back and dig a little bit more into this issue in the future. Thanks for all your help! And please keep me posted if any solution is found :)

Share this post


Link to post
Share on other sites

It seems like it might be server specific. I just tried again on my local dev setup and it works as I reported in my last post. But then I just tried via lightning.pw and it failed with the same message you are getting.

Thakns for the Info. We might also have to look into this issue. Maybe our server admin can figure it out and then we can report back.

  • Like 2

Share this post


Link to post
Share on other sites

Thakns for the Info. We might also have to look into this issue. Maybe our server admin can figure it out and then we can report back.

Thanks! Looking forward to it.

Share this post


Link to post
Share on other sites

Report:

So I ended up modifying the core to cope with this... (yeah I know this is not a good idea but I need this to be functional for my Chinese clients)

In WireUpload.php line 240 I added:

$filename = 'file-' . $filename;

after:

$filename = $this->getTargetFilename($filename);

$filename = $this->validateFilename($filename);

This will force every file uploaded to start with a 'file-' prefix so that the system will not have files with a filename consisting Chinese characters only.

However I ran into another problem:

If I upload 'test.jpg', I get 'file-test.jpg' which is good.

But if I upload '未命名.jpg', I get 'file-' instead, missing the extension.

Then, if I upload '未命名.jpg' again, I get 'file--1.' again missing the extension but with a trailing dot...

This inconsistency is really just weird to me. For English filenames I got the extension, for the first Chinese filename I got the filename without the extension. And if I upload another file of Chinese filename, I got the filename and the dot (e.g.:  file--1. )

Despite all this, I can still view these files as images in my browser, so ...../file-test.jpg , ...../file- ,  ........../file--1. all display the image without any error.

If I insist I need to modify the core in order to make this work (before any official fix is available). Where is the best place to do so? The way I did it works in some sense but produces all these weird filenames which are not really desirable.

Anyone familiar with the core willing to help out here? Thank you so much in advance!

  • Like 1

Share this post


Link to post
Share on other sites

I just played with uploading your files and it worked fine (maybe not so fine, but worked) for me without Adrians module on PW 2.4.14. Got the same results as Adrian. Maybe png.png is not the best option and should be changed, but it does function. I tried moving around those hieroglyphs so I get two 3-symbol files with untranslatable symbols in the filename and it still worked. I got  "png.png" and "png-1.png" or something like that.

Working on Windows 7 with XAMPP.

Share this post


Link to post
Share on other sites

This issue still exists on (at least my) linux servers. If a file to upload does not contain any latin characters, you get the "Unable to move uploaded file to: ..." error. If a file name has at least any latin character, the upload works. On windows everything works fine. Can we do something about it?

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Xonox
      Hi,
       
      I'm trying to upload images from a folder into a page. I need to replace the images instead of adding. The image field already has the replace existing images turned on, but it doesn't seem to be enough.
       
      My code:
      foreach($files as $file) { if(substr($file, 0, 1) != '.' && $file != '.' && $file != '..') { // Get SKU $file_sku = substr($file, 0, 9); // Check for book $book = $pages->get('sku=' . $file_sku); if(!$book->id) { // Book doesn't exist } else { // Upload image into book !!! THIS CODE ADDS FILE INSTEAD OF REPLACING. HOW CAN I REPLACE? $book->book_images->add($upload_directory . '/' . $file); // Delete file unlink($upload_directory . '/' . $file); } // Save book if($book->id) $book->save(); } }  
      What am I missing?
    • By Guy Incognito
      I'm trying to implement a front-end image upload form for user profile pictures, to a field called 'profile_picture'. The code is based on the various examples found around these forums but isn't yet 100% right.
      The form successfully submits the image to the server and updates the field in the dashboard. When you submit the firm, the page reloads and uploads the image but the template still loads the old image path (now broken path as the old image has been removed).
      I can only get the new image to show if I hit enter in the address bar forcing the page to reload.
      Any ideas? Is it a caching issue, or something to do with the order of the script? It makes no difference if I call the image after the upload form.
      <?php //Display current user image $userImg = $user->profile_picture->first(); echo '<img src="'.$userImg->url.'">'; $upload_path = $config->paths->assets . "files/avatar_uploads/"; $f = new WireUpload('userimage'); $f->setMaxFiles(1); $f->setMaxFileSize(1*1024*1024); $f->setOverwrite(true); $f->setDestinationPath($upload_path); $f->setValidExtensions(array('jpg', 'jpeg', 'png', 'gif')); if($input->post->form_submit) { if(!is_dir($upload_path)) { if(!wireMkdir($upload_path)) throw new WireException("No upload path!"); } $files = $f->execute(); if ($f->getErrors()) { foreach($files as $filename) @unlink($upload_path . $filename); foreach($f->getErrors() as $e) echo $e; } else { $user->of(false); $user->profile_picture->removeAll(); // wirearray (line added by @horst: explanation is three posts beneath) $user->profile_picture = $upload_path . $files[0]; $user->save(); $user->of(true); @unlink($upload_path . $files[0]); } } ?> <form class="forum-form" accept-charset="utf-8" action="./" method="post" enctype="multipart/form-data" > <input type="file" id="attach" name="userimage" accept="image/jpg,image/jpeg,image/gif,image/png" /> <input type="submit" name="form_submit" value="Submit"/> </form>
       
    • By bot19
      Hey guys,
      Today I was uploading images and ran into an image I couldn't upload. This has never happened before since I got images uploading working.
      I had a dig around the forum but couldn't find anything relevant.
      Initially I thought it was due to the size ~1.3mb, but I had uploaded something bigger after and it was fine (~1.7mb). There are no size or dimension restrictions on this image field.
      After looking at the image some more, I realised its dimensions was HUGE: ~5300px
      There must be some sort of limit somewhere throwing an error due to this dimension size? Because after I resized the image down to 2560px, it uploaded fine.
      What does everyone think? I kind of just want to know. See below for more info. Thanks!
      Uploading the smaller file, everything is fine. If you look at the response, you can see:
      [{"error":false,"message":"Added file: mock-email-marketing-and-newsletters-smaller.png","file":"\/procwire\/site\/assets\/files\/1189\/mock-email-marketing-and-newsletters-smaller.png","size":340797,"markup":"<li id='file_551ac1043f1cbbe0b909dab963a1f28c' class='ImageOuter gridImage ui-widget'><div class='gridImage__tooltip'><table><tr><th>Dimensions<\/th><td>2560x2560<\/td><\/tr><tr><th>Filesize<\/th><td>333&nbsp;kB<\/td><\/tr><tr><th>Variations<\/th><td>0<\/td><\/tr><\/table><\/div>\n\t\t\t<div class='gridImage__overflow'>\n\t\t\t\t<img src=\"\/procwire\/site\/assets\/files\/1189\/mock-email-marketing-and-newsletters-smaller.0x260.png?nc=1512803364\" alt=\"\" data-w=\"2560\" data-h=\"2560\" data-original=\"\/procwire\/site\/assets\/files\/1189\/mock-email-marketing-and-newsletters-smaller.png?nc=10\" \/>\n\t\t\t<\/div>\n\t\t\t\n\t\t\t\t<div class='gridImage__hover'>\n\t\t\t\t\t<div class='gridImage__inner'>\n\t\t\t\t\t\t<label for='' class='gridImage__trash'>\n\t\t\t\t\t\t\t<input class='gridImage__deletebox' type='checkbox' name='delete_crm_feature_img_repeater1189_551ac1043f1cbbe0b909dab963a1f28c' value='1' title='Delete' \/>\n\t\t\t\t\t\t\t<span class='fa fa-trash-o'><\/span>\n\t\t\t\t\t\t<\/label>\n\t\t\t\t\t\t<a class='gridImage__edit'>\n\t\t\t\t\t\t\t<span>Edit<\/span>\n\t\t\t\t\t\t<\/a>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\n\t\t\t\t<div class='ImageData'>\n\t\t\t\t\t<h2 class='InputfieldImageEdit__name'><span contenteditable='true'>mock-email-marketing-and-newsletters-smaller<\/span>.png<\/h2>\n\t\t\t\t\t<span class='InputfieldImageEdit__info'>333&nbsp;kB, 2560&times;2560 <\/span>\n\t\t\t\t\t<div class='InputfieldImageEdit__errors'><\/div>\n\t\t\t\t\t<div class='InputfieldImageEdit__buttons'><small><button type='button' data-href='\/procwire\/admin123\/page\/image\/edit\/?id=1189&file=1189,mock-email-marketing-and-newsletters-smaller.png&rte=0&field=crm_feature_img' class='InputfieldImageButtonCrop ui-button ui-corner-all ui-state-default pw-modal-large pw-modal' data-buttons='#non_rte_dialog_buttons button' data-autoclose='1' data-close='#non_rte_cancel'><span class='ui-button-text'><span class='fa fa-crop'><\/span> Crop<\/span><\/button><button type='button' data-href='\/procwire\/admin123\/page\/image\/variations\/?id=1189&file=mock-email-marketing-and-newsletters-smaller.png&modal=1&varcnt=varcnt_crm_feature_img_repeater1189_551ac1043f1cbbe0b909dab963a1f28c' class='ui-button ui-corner-all ui-state-default pw-modal-large pw-modal' data-buttons='button'><span class='ui-button-text'><span class='fa fa-files-o'><\/span> Variations <span class='ui-priority-secondary'>(0)<\/span><\/span><\/button><\/small><\/div>\n\t\t\t\t\t<div class='InputfieldImageEdit__core'><div class='InputfieldFileDescription'><label for='description_crm_feature_img_repeater1189_551ac1043f1cbbe0b909dab963a1f28c' class='detail'>Description<\/label><input type='text' name='description_crm_feature_img_repeater1189_551ac1043f1cbbe0b909dab963a1f28c' id='description_crm_feature_img_repeater1189_551ac1043f1cbbe0b909dab963a1f28c' value='' \/><\/div><\/div>\n\t\t\t\t\t<div class='InputfieldImageEdit__additional'><\/div>\n\t\t\t\t\t<input class='InputfieldFileSort' type='text' name='sort_crm_feature_img_repeater1189_551ac1043f1cbbe0b909dab963a1f28c' value='2' \/>\n\t\t\t\t\t<input class='InputfieldFileReplace' type='hidden' name='replace_crm_feature_img_repeater1189_551ac1043f1cbbe0b909dab963a1f28c' \/>\n\t\t\t\t\t<input class='InputfieldFileRename' type='hidden' name='rename_crm_feature_img_repeater1189_551ac1043f1cbbe0b909dab963a1f28c' \/>\n\t\t\t\t<\/div>\n\t\t\t<\/li>","replace":true,"overwrite":0}]
      Uploading the original file, something happens and there is no response (see Content-Length: 0)

      Which in turn must trigger this issue in the JS.

      No response.

    • By gonzz
      Hi i'm having a recurrent problem I found on this forum: when using the image uploader I get the following error on console "Unexpected token < in JSON". But I couldn't solve it with any of the other proposed solutions:
      setting debug to true setting $config->uploadTmpDir = dirname(__FILE__) . '/assets/uploads/'; setting uploadTmpDir in php.ini  
      BUT This problem only appears for non superuser users (ie editors).
      Running on a Digitial Ocean Server with NGINX and PHP 7.1. Version is ProcessWire 3.0.62
      Note: Im using Fredi plugin, but i doubt that's the problem because I'm using it in other sites with no problem
       
      Any idea how i could solve this (even with non ajax image upload) I need to sort this as fast and simple as possible
      Thanks in advance
    • By ridgedale
      I've been trying to work out how I can provide a frontend file upload (whatever file format the user chooses to upload - albeit not executables!) for registered users to be able to create pages/posts and upload any file they wish so they can create a link to the file so it can either be downloaded or displayed.
      Everything I've read so far appears to relate to just images and uploading and adding images is already covered using the images field.
      Any thoughts/guidance would be a appreciated.
       
×
×
  • Create New...