简体   繁体   中英

PHP Move_uploaded_file function not working

here is the HTML code for image upload

<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
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. tried different ways to store at the dest path but the after so many tries it didn't works. 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. 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. But still you cant't able to move files to the another folder that is move_uploaded_files() do. I dont know the way to change that but anyone knows that really will appreciated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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