-2

Can anyone tell me, how I can print a photo name? The photo will be selected from input type=file.

Here is what I have tried so far:

$photo = $_FILES['photo']['tmp_name'];
//$photo_name = basename($photo);
echo $photo;

This code doesn't show actual file name. It print as like this (php3515.tmp);

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
Nishan Dhungana
  • 831
  • 3
  • 11
  • 30
  • 3
    `echo $_FILES['photo']['name']`. RTM: http://php.net/manual/en/features.file-upload.post-method.php – M. Eriksson Jul 01 '17 at 09:06
  • Possible duplicate of [Isn't Using the basename function with $\_FILES\['userFile'\]\['name'\] Redundant?](https://stackoverflow.com/questions/2347056/isnt-using-the-basename-function-with-filesuserfilename-redundant) The question itself answers this very basic question. – mickmackusa Jul 01 '17 at 11:46

3 Answers3

4

Print the file name instead of tmpname

Try this:

echo $_FILES['photo']['name'];
Ankit Singh
  • 1,477
  • 1
  • 13
  • 22
2

php3515.tmp in your case is a temporary file generated by PHP. You have to move this file to your desired location with move_uploaded_file() function, which takes your $_FILES['photo']['tmp_name'] as first argument, and path and new filename as second argument.

Here is an example taken from PHP manual:

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

Note $_FILES['userfile']['name'] in this example, which contains the original file name.

Lukas
  • 817
  • 8
  • 13
  • This doesn't even answer the OP's question, which simply was _"how can i print a photo name"_. – M. Eriksson Jul 01 '17 at 09:12
  • @Magnus Yes, my mistake. I already edited the answer to highlight the `name` index in `$_FILES` array. I leave the answer here as it could provide more useful information about file upload in PHP. Especially the `print_r($_FILES);` line would be priceless for education reasons. – Lukas Jul 01 '17 at 09:17
2

You need to know, that you have 5 parameters in $_FILES['userfile']:

  • name - original name of uploaded file
  • tmp_name - system name that is physically writen on disk by system
  • size - size of file
  • error - code of error - 0 mean no problem
  • type - type of saved file

If you want you original name, to display, use simply:

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

You also have function move_uploaded_file(), that allows you to safely move the file from a temporary place to your own. Another way, after the end of PHP process, you uploaded file will be deleted.

$path = __DIR__  . '/upload/' . basename($_FILES['userfile']['name']);
$result = move_uploaded_file($_FILES['userfile']['tmp_name'], $path);

$result show you, is system correctly save a file in given place.

timiTao
  • 1,417
  • 3
  • 20
  • 34