php形成多个输入上传

时间:2016-09-03 12:53:40

标签: php

我写了一个多表单,但它不起作用。代码检查员告诉我move_uploaded_file函数有问题。谁能告诉我这是什么问题?

我的HTML代码:

<div class="setting post">
    <form action="add-banner.php" method="post" enctype="multipart/form-data">
    <input type="text" name="banner-title" placeholder="enter new banner title"><br>
    <select name="banner-cat">
           <?php
           $get_cats = mysqli_query($db,'select * from cats');
           while($row = mysqli_fetch_assoc($get_cats)){
           ?>
           <option value="<?php echo $row['id']?>"><?php echo $row['cat_name'] ?></option>
           <?php
           }
           ?>
           </select>
           <br>
           <input type="file" name="banner">
           <br>
     <input type="submit" name="upload" value="add new banner">
     </form>
    </div>

这是我的PHP代码:

<?php
require_once 'db.php';
global $db;
$banner_title = $_POST['banner-title'];
$banner_cat = $_POST['banner-cat'];
$banner = $_FILES['banner']['name'];
$banner_tmp = $_FILES['banner']['tmp_name'];
$upload_file = move_uploaded_file($banner_tmp,'../../images/$banner');
$insert_banner = mysqli_query($db,"insert into banner(banner_title,banner_cat,banner_link) values ('$banner_title','$banner_cat','$banner')");
if($insert_banner && $upload_file){
    $message = 'New banner Succesfully added';
    echo "<script>
    alert('".$message."');
    window.location.href='post.php';
    exit;
    </script>";

    }else{$message = 'Something goes Wrong';
    echo "<script>
    alert('".$message."');
    window.location.href='post.php';
    exit;
    </script>"; 
    }
?>

1 个答案:

答案 0 :(得分:1)

<?php
 require_once 'db.php';
 global $db;
 $banner_title = $_POST['banner-title'];
 $banner_cat = $_POST['banner-cat'];
 $banner = basename($_FILES['banner']['name']);
 $banner_tmp = $_FILES['banner']['tmp_name'];
 $upload_file = move_uploaded_file($banner_tmp,'/images/$banner');
 $insert_banner = mysqli_query($db,"insert into banner(banner_title,banner_cat,banner_link) values ('$banner_title','$banner_cat','$banner')");
 if($insert_banner && $upload_file){
$message = 'New banner Succesfully added';
echo "<script>
alert('".$message."');
window.location.href='post.php';
exit;
</script>";

}else{$message = 'Something goes Wrong';
echo "<script>
alert('".$message."');
window.location.href='post.php';
exit;
</script>"; 
}
?>

//不要使用后台文件夹上传文件,如果使用后台文件夹,请使用文件夹的完整网址。