Img

Get image name on upload

When I upload an image:

 <h3 id="txt_i2">Img</h3><input type="file" accept="image/jpeg, image/jpg" name="picture" size="chars">

How to get the name of the downloaded img using php? I do not mean "picture". For example, if img is called cow.jpg, get that "cow"

+3
source share
5 answers

Anton, you can use the code below to get an image or any file name without extension.

$filename = pathinfo($_FILES['picture']['name'], PATHINFO_FILENAME);
+5
source

Loading the POST method :

$_FILES['userfile']['name']

The original file name on the client machine.

Thus, using the above, if you want the actual file name cow.jpg, it is stored in

$_FILES['picture']['name'];

, pathinfo(), PATHINFO_FILENAME, cow. cow.moo.jpg, cow.moo:

$picture_filename = pathinfo($_FILES['picture']['name'], PATHINFO_FILENAME);
+3

check pathinfo has some examples, so you just need to use pathinfo in the variable$_FILES

0
source

Use this

<html>
 <body>
  <form action="upload_file.php" method="post"
    enctype="multipart/form-data">
   <label for="file">Filename:</label>
     <input type="file" name="file" id="file"><br>
     <input type="submit" name="submit" value="Submit">
  </form>
 </body>
</html> 

Upstairs script

<?php
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
 ?> 

This will give you the file name, file type and file size.

0
source
$filename = explode(".", $_FILES['picture']['name']); //split by '.'
array_pop($filename); //remove the last segment
$filename = implode(".", $filename); //concat it by '.'
-1
source

Source: https://habr.com/ru/post/1664812/


All Articles