Search code examples
phpjqueryjquery-file-upload

Trim a string purely to text, without leaving any whitespace in PHP?


I am having an image upload script, which works fine so far. It runs with jQuery fileupload. The PHP script generates a new name for the uploaded image and gives it out through exit($imgname);. Strangely I always get a response with many whitespaces like you can see in the picture.

To the screenshot:
Many whitespaces, but why?***

My whole website uses jQuery and I thought about using $.trim() to just trim the result to plain text, but I don't know if this is a great idea since I don't think that this works for any common browser.

Additional: The most strange thing about this is, that it worked in the past just fine without any whitespaces. Today I uploaded something and suddenly it does something like this...

PHP:

$upload  = $image->upload(); 
$imgname = $image->getName();
$imgmime = $image->getMime();
$fullimgname = $imgname . "." . $imgmime;

if($upload){
    // POST TO DATABASE ETC.

    exit($fullimgname);
}

Solution

  • The white paces may come from somewhere else in the PHP file...

    Look for spaces that would be outside the <?php and ?> brackets.

    Like this, for example:

            // <-- 4 tabs on this line
    <?php
    $upload  = $image->upload(); 
    $imgname = $image->getName();
    $imgmime = $image->getMime();
    $fullimgname = $imgname . "." . $imgmime;
    
    if($upload){
      // POST TO DATABASE ETC.
    
      exit($fullimgname);
    }