-2

I'm developing a function in PHP, that takes 'Image Name' as parameter and upload the image further. I knew there are several ways to deal with multiple image uploading. But in my situation this multiple image-upload (for different images) is appearing like...

<input name="imageone" id="imageone" type="file"/>
<input name="imagetwo" id="imagetwo" type="file"/>
<input name="imagethree" id="imagethree" type="file"/>

and the PHP code is...

function uploadImage($imageName)
{
    if(!isset($imageName)){
       die('Image is Missing!');
    } else {
      //uploading code continuous...
    }
}

I expect that parameter '$imageName' takes whatever the image-name... as 'imageone'... 'imagetwo'... 'imagethree'

Could anyone suggest, that How I take the image-name as a parameter in to $_FILES[''] in php..?

Thanks in advance..!

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • you need to create input text field, there is no possibility to pass custom text as file variable – Daimos Sep 23 '15 at 11:14
  • do you mean isset($_FILES[$imageName]) ? Oh, my god, what an unintelligible question – Amarnasan Sep 23 '15 at 11:17
  • It seems to me you dont understand how to upload files with PHP, [Start by reading the manual](http://php.net/manual/en/features.file-upload.php) and then see [Uploading mutiple files](http://php.net/manual/en/features.file-upload.multiple.php) – RiggsFolly Sep 23 '15 at 11:18
  • possible duplicate of [Why would $\_FILES be empty when uploading files to PHP?](http://stackoverflow.com/questions/3586919/why-would-files-be-empty-when-uploading-files-to-php) – kasper chexonz Sep 23 '15 at 11:19
  • Sorry friends, I mistakenly copied the code. See my edited code above... – Gajen Dissanayake Sep 23 '15 at 11:24

2 Answers2

0

Name in the form is different from the $_FILES value for name. One is the name of the form variable being posted and the other is the filename of the uploaded file.

So, to get the image name;

$_FILES['imageone']['name']

Would give you the filename of the first uploaded file.

Geoff Atkins
  • 1,693
  • 1
  • 17
  • 23
  • Yeah... You are correct. But I need that image name (in html form) would be taken as a parameter for php function uploadImage(). How to deal this..? – Gajen Dissanayake Sep 23 '15 at 11:19
  • Sorry, it's not clear what you're asking. Is it that you are passing a new filename based on some text input? Or are you asking how to take the existing filename and pass it to your function? – Geoff Atkins Sep 23 '15 at 11:25
  • Yeah Yeah... A 'Text Input'..! Actually once I select an image via the 'upload button', the image-name is appearing no..? That image name has to be the parameter to "php function"... Will it possible..? – Gajen Dissanayake Sep 23 '15 at 11:29
  • Then when you are handling the file that has been uploaded, you can rename it to whatever you want, be it a pre-defined name or something from a text input. – Geoff Atkins Sep 23 '15 at 11:31
0

If you want access to the $_FILES array in your function then pass it to the function as a parameter.

function uploadImage($files)
{
    if(!isset($imageName)){
       die('Image is Missing!');
    } else {
      //uploading code continuous...
    }
}


// call function
uploadImage($FILES);

Also you dont need to invent filenames the $_FILES array contains everything you need, including error codes should something go wrong in the upload process.

The global $_FILES will contain all the uploaded file information. Its contents from the example form is as follows. Note that this assumes the use of the file upload name userfile, as used in the example script above. This can be any name.

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

The original name of the file on the client machine. $_FILES['userfile']['type']

The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted. $_FILES['userfile']['size']

The size, in bytes, of the uploaded file. $_FILES['userfile']['tmp_name']

The temporary filename of the file in which the uploaded file was stored on the server. $_FILES['userfile']['error']

The error code associated with this file upload.

Files will, by default be stored in the server's default temporary directory, unless another location has been given with the upload_tmp_dir directive in php.ini. The server's default directory can be changed by setting the environment variable TMPDIR in the environment in which PHP runs. Setting it using putenv() from within a PHP script will not work. This environment variable can also be used to make sure that other operations are working on uploaded files, as well.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149