Question

I have a very simple upload image which works on one page but returns empty file name in another page

   if (isset($_POST['upload_img'])) {
   $file_name = $HTTP_POST_FILES['img']['name'];
   $random_digit=rand(0000000,9999999999);
   $new_file_name=$random_digit.$file_name.'.jpg';
   $path= "images/".$new_file_name;

  echo"$file_name"; 

  }

and this is the html form

<form role="form" action="" method="post" enctype="multipart/form-data">   
<div class="form-group">
<label for="exampleInputFile">Upload Image</label>
<input type="file" id="exampleInputFile" name="img">
</div>

<p><a href="">Terms and conditions</a></p>

<button type="submit" class="btn btn-default btn-block" name="upload_img">Upload    </button>

</form>

Any idea whats wrong?

Thank you all in advance

Was it helpful?

Solution

Try

$file_name = $_FILES['img']['name'];

instead of

$file_name = $HTTP_POST_FILES['img']['name'];

OTHER TIPS

uploadpage.php

<?php
 if (isset($_FILES['img'])) {
   $file_name = $_FILES['img']['tmp_name'];
   $random_digit=rand(0000000,9999999999);
   $new_file_name=$random_digit.$file_name.'.jpg';
   $path= "images/".$new_file_name;

  echo"$file_name"; 

  }

 ?> 

index.php:

<form action="uploadpage.php" method="post" enctype="multipart/form-data" >

<div class="form-group">
<label for="exampleInputFile">Upload Image</label>
<input type="file" id="exampleInputFile" name="img">
</div>

<p><a href="">Terms and conditions</a></p>

<button type="submit" class="btn btn-default btn-block" name="upload_img">Upload    </button>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top