简体   繁体   English

PHP Move_uploaded_file function 不工作

[英]PHP Move_uploaded_file function not working

here is the HTML code for image upload这是用于图像上传的 HTML 代码

<form id="imageUpload" action="" class="form-horizontal form-material" method="POST" enctype="multipart/form-data">
    <div class="form-group">
        <label class="col-md-6">Image</label>
        <div class="col-md-6">
        <input type="file" class="form-control form-control-line" name="portfolioimage">
    </div>
    <div class="form-group">
        <label class="col-md-6">Select Package</label>
        <div class="col-md-6">
            <select class="form-control form-control-line" name="packageName">
                <option selected disabled>-- SELECT PACKAGE -- </option>
                <?php
                    while ($row = mysqli_fetch_row($result)) {
                        echo "<option>$row[1]</option>";
                    }
                ?>
            </select>
        </div>
    </div>
    <div class="form-group ">
        <div class="col-sm-6">
            <input type="submit" class="btn btn-success" value="ADD" name="addbtn">
        </div>
    </div>
</form> 

here is my php code for uploading image as the package name and datetime stamps.这是我的 php 代码,用于将图像上传为 package 名称和日期时间戳。

<?php
if(isset($_POST['addbtn']))
{
    $errors= array();
    $package_name = $_POST['packageName'];
    $query = "select id from packages where title='$package_name'";
    $result = mysqli_query($con,$query);
    $row = mysqli_fetch_row($result);
    $package_id = $row[0];
    $image=$_FILES['portfolioimage']['name'];
    // $file_size =$_FILES['portfolioimage']['size'];
    $file_tmp =$_FILES['portfolioimage']['tmp_name'];
    $file_type=$_FILES['portfolioimage']['type'];
    $file_ext=strtolower(end(explode('.',$_FILES['portfolioimage']['name'])));
    $file_tmp=$_FILES['portfolioimage']['tmp_name'];
    $extensions= array("jpeg","jpg","png");

    if(in_array($file_ext,$extensions)=== false){
       $errors[]="extension not allowed, please choose a JPEG or PNG file.";
    }
    
    // if($file_size > 2097152){
    //    $errors[]='File size must be excately 2 MB';
    // }
    $timestamp = date("mdYHis");
    $imageName = $package_name . $timestamp ."." . $file_ext;

    if(empty($errors)==true)
    {   
        $uploadDir = '../Admin/images/portfolio_images';
        $dest = $uploadDir . "/" . $imageName ;
        $moveIMG = move_uploaded_file($file_tmp,$dest);
        if($moveIMG){
            echo"<script language='javascript'> alert('Image  Moved') </script>";
        } else {
            echo"<script language='javascript'> alert('Move failed') </script>";
        }
        $str = "INSERT INTO portfolio_images (image,package_id) VALUES ('$imageName',$package_id)";
            $query = mysqli_query($con,$str);
            if($query == 1)
            {
                echo"<script language='javascript'> alert('Image  Uploaded') </script>";
            }
            else
            {
                echo"<script language='javascript'> alert('".$str."') </script>";
            }
    }
    else
    {
       print_r($errors);
    }
}
?>

the condition in if section $moveIMG gives false value. if 部分 $moveIMG 中的条件给出错误值。 tried different ways to store at the dest path but the after so many tries it didn't works.尝试了不同的方法来存储在 dest 路径,但经过这么多尝试它没有工作。 it worked with this same code on first submission but now its not working.它在第一次提交时使用相同的代码,但现在它不起作用。

May be one of you can find the error!!可能是你们中的一个人可以找到错误!

ok.好的。 after so many digging and some suggestions by @ADyson in the comments.经过@ADyson 在评论中的大量挖掘和一些建议。 Come to know that if you put conditions for the file size and checking for the file size you come up with the specific file size but to upload files greater then 2MB have to change php.init max_file_size also.要知道,如果您为文件大小设置条件并检查文件大小,您会得出具体的文件大小,但要上传大于 2MB 的文件,还必须更改 php.init max_file_size。 But still you cant't able to move files to the another folder that is move_uploaded_files() do.但是您仍然无法将文件移动到 move_uploaded_files() 所做的另一个文件夹。 I dont know the way to change that but anyone knows that really will appreciated.我不知道如何改变它,但任何人都知道这真的会很感激。

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

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