我有一个非常简单的上传图片,它在一个页面上工作,但在另一个页面中返回空文件名
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>
任何想法都错了吗?
提前谢谢大家
答案 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>