PHP文件上传返回空文件类型

时间:2014-04-09 12:09:10

标签: php html image upload

我有一个非常简单的上传图片,它在一个页面上工作,但在另一个页面中返回空文件名

   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"; 

  }

这是html表单

<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>

任何想法都错了吗?

提前谢谢大家

2 个答案:

答案 0 :(得分:2)

尝试

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

而不是

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

答案 1 :(得分:0)

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>