0

I was wondering whether it is possible to rename an image base on the form input file ID.

<form action="upload_file.php" enctype="multipart/form-data" method="post">
    <input id="picture_01"  type="file">
    <input id="picture_02"  type="file">
    <input id="picture_03"  type="file">
    <input id="picture_04"  type="file">
<input name="submit" type="submit" value="Submit">
</form>

I want that if the image is uploaded from input 4 it will be renamed to 'picture_04', if it is from input form 2 it will be renamed to 'picture_02'. Not sequencially but according to the input form box.

I haven't managed to do this despite the various trial and errors.

Panny Monium
  • 301
  • 2
  • 4
  • 20
  • What have you tried? When you save the file server-side, you can set the name to any value you want. Setting it to the form element where you read it should be easy enough. – David Oct 04 '13 at 23:33
  • Can we get the PHP code you're using and the final result you're expecting to see? – Raz Harush Oct 04 '13 at 23:33
  • This should be useful to you: http://stackoverflow.com/questions/3509333/php-how-to-upload-save-files-with-desired-name – David Oct 04 '13 at 23:34

2 Answers2

0

You need to name your inputs:

<input id="picture_01" name="picture_01" type="file">

etc.

Then in PHP you retrieve the image with the $_FILES array, like $_FILES['picture_01'] or by simply looping through $_FILES.

foreach( $_FILES as $input_name=>$file)
{
  // $input_name is the name used as the form input name
  // $file is an array with the following keys: name, type, tmp_name, error, size.
}

Of course the manual is always a good read http://www.php.net/manual/en/features.file-upload.post-method.php

Sébastien
  • 11,860
  • 11
  • 58
  • 78
0

I would use separate forms for each input. This way you could use a hidden input like:

<form action="upload_file.php" enctype="multipart/form-data" method="post">
   <input type='hidden' name='picture_03_file' value="picture_03" />
   <input type='file'   name='picture_03_name' />
</form>
<form action="upload_file.php" enctype="multipart/form-data" method="post">
   <input type='hidden' name='picture_04_file' />
   <input type='file'   name='picture_04_name' value="picture_04" />
</form>

This way your PHP code would look like:

$imgName = $_POST['picture_04_name'];
// Do file upload here
phpete
  • 318
  • 3
  • 12