2014-04-09 90 views
0

我有一個非常簡單的上傳圖像,其工作在一個頁面上,但在另一頁PHP文件上傳返回空文件類型

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> 

任何想法什麼是錯的?

謝謝大家提前

+1

我沒有看到開頭'

'? – Sharky

+0

我有它,它沒有出現在這裏 – user3482036

+0

並使用' ridvanzoro

回答

2

嘗試

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

,而不是

$file_name = $HTTP_POST_FILES['img']['name']; 
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>