0

i am uploading a file to the server. everything works fine unless the uploaded file has a space in it.

I tried to use: str_replace(" ", "_", $_FILES['image']['name']);

My code is

 $image_name= str_replace(" ", "_", $_FILES['image']['name']);
               $image_tmp_name = $_FILES['image']['tmp_name'];
               $image=$_FILES['image'];

              $url = "http://jkshahclasses.com/push_images/$image_name";
                if(move_uploaded_file($image_tmp_name,"../../push_images/$image_name"))
                {
                    echo "file uploaded";
                }
                else
                {
                    echo "error: file not uploaded";
                    }

Thanks in advance

1 Answers1

0

You can use this function to slugify your file name before uploded :

  public function slugify($text)
  {
    // replace all non letters or digits by _
    $text = preg_replace('/\W+/', '_', $text);

    // trim and lowercase
    $text = strtolower(trim($text, '_'));

    return $text;
  }

But first you have to get only file name you can do it with this function :

$file_name = pathinfo($path, PATHINFO_FILENAME);

If you want to get file extension you have to use :

$file_ext = pathinfo($path, PATHINFO_EXTENSION);
MEDALI
  • 55
  • 5
  • Thanks, but it still doesnt work. also somehow it renames the .jpg file to _jpg – Rishad Gandhy Dec 02 '15 at 15:14
  • Just to be clear, i should use this on the image file name not the temp name right? i used `$image_name = $this->slugify(pathinfo($_FILES['image']['name'],PATHINFO_FILENAME)) . '.' . pathinfo($_FILES['image']['name'],PATHINFO_EXTENSION);` but it still doesnt work – Rishad Gandhy Dec 02 '15 at 16:01