繁体   English   中英

为什么第二个 move_uploaded_file function 在此代码中不起作用?

[英]Why the second move_uploaded_file function doesn't work in this code?

如何在此代码中上传缩略图?
当我点击上传时,它只移动第一个 move_uploaded 文件而不会移动第二个。 任何人都可以帮助我吗?

<?php
if(isset($_POST['submit'])){
    $cap = mysqli_real_escape_string($con, $_POST['cap']);
    $image = $_FILES['image'] ['name'];
    $tmp_name= $_FILES['image'] ['tmp_name'];
    $thumb = $_FILES['thumb'] ['name'];
    $tmp_name= $_FILES['thumb'] ['tmp_name'];
    $type = $_POST['type'];
    

    if(empty($image) or empty($thumb) or empty($cap)){
        $error = "<p style='font-seize: 20px; font-weight: bolder; color: red;'>
        Please Fill out all required stared inputs!</p>";
    }
    else{
        $pic = "INSERT INTO photos(image, thumb, caps, type)VALUES('$image', '$thumb', '$cap', '$type')";
        if(mysqli_query($con, $pic)){
            $success = "<p style='font-seize: 25px; font-weight: bolder; color: green;'>
            <i class='fa fa-smile-beam'></i> Your Data 
            Published Successfully</p>";
            move_uploaded_file($tmp_name, "img/photos/$image");
            move_uploaded_file($tmp_name, "img/thumbs/$thumb");
            
        }
        else{
            $fail = "<p style='font-seize: 20px; font-weight: bolder; color: red;'><i class='fa fa-sad-tear'>
            </i> Unsuccessful!</p>";
        }
    }
}
?>

一个变量只能容纳一件事。 您正在为图像和缩略图使用$tmp_name ,在第二次赋值后它只有缩略图的临时名称。

您需要使用不同的变量。

<?php
if(isset($_POST['submit'])){
    $cap = mysqli_real_escape_string($con, $_POST['cap']);
    $image = $_FILES['image'] ['name'];
    $tmp_image= $_FILES['image'] ['tmp_name'];
    $thumb = $_FILES['thumb'] ['name'];
    $tmp_thumb= $_FILES['thumb'] ['tmp_name'];
    $type = $_POST['type'];
    

    if(empty($image) or empty($thumb) or empty($cap)){
        $error = "<p style='font-seize: 20px; font-weight: bolder; color: red;'>
        Please Fill out all required stared inputs!</p>";
    }
    else{
        $pic = "INSERT INTO photos(image, thumb, caps, type)VALUES('$image', '$thumb', '$cap', '$type')";
        if(mysqli_query($con, $pic)){
            $success = "<p style='font-seize: 25px; font-weight: bolder; color: green;'>
            <i class='fa fa-smile-beam'></i> Your Data 
            Published Successfully</p>";
            move_uploaded_file($tmp_image, "img/photos/$image");
            move_uploaded_file($tmp_thumb, "img/thumbs/$thumb");
            
        }
        else{
            $fail = "<p style='font-seize: 20px; font-weight: bolder; color: red;'><i class='fa fa-sad-tear'>
            </i> Unsuccessful!</p>";
        }
    }
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM