简体   繁体   中英

php move_uploaded_file function not working but record save in database

i've design codes to upload image and data information in hosted server database. The record information save in table. But image not move to server folder. I also try rename() and Copy() but not working. The strange thing is that i've designed four upload information forms(a,b,c and d) but d is not working. all 3 are working fine. same codes are in all files.

my codes are in files

d.php

<div class="container-fluid">

<!--Form 1-->
<div class="row">
<div class="col-md-6 center" id="shahster">
<div class="panel panel-default shadow">
<div class="panel-heading"><h3 class="panel-title"&gt;Shashter Form</h3></div>
<div class="panel-body">    

        <form class="form-horizontal" method="post" enctype="multipart/form-data"  action="<?php $_SERVER["DOCUMENT_ROOT"] ;?&gt;
/dashboard/other/other_sbt.php"&gt;
<div class="form-group">
<label class="control-label col-xs-3" for="name"&gt;Article Title Name</label>
        <div class="col-xs-9"><input type="text" class="form-control" id="name" name="nametxt" placeholder="Enter Title Name" /></div></div>            

        <div class="form-group">
        <label class="control-label col-xs-3" for="desc">Description</label>
        <div class="col-xs-9"><textarea rows="3" class="form-control" id="desc" name="desctxt" placeholder="Description"></textarea></div></div>

        <div class="form-group">
        <label class="control-label col-xs-3" for="upload">Upload:</label>
        <div class="col-xs-9"><input id="upload" class="file"  type="file" name="upimage" data-min-file-count="1" /></div></div>

        <div class="form-group">
        <label class="control-label col-xs-3" for="type">Choose options</label>
        <div class="col-xs-9"> 
        <label class="checkbox-inline"><input type="checkbox" name="taksalitxt" value="taksali" /> Taksali</label></div></div>

        <div class="clearfix"></div>
        <div class="form-group">
        <label class="control-label col-xs-3">Feature Product</label>
        <div class="col-xs-9"><input type="checkbox" name="fproducttxt" value="fproduct"></div></div>

        <div class="form-group">
        <div class="col-xs-offset-3 col-xs-9">
        <input type="submit" class="btn btn-primary" name="submit" value="Submit" />
        <input type="reset" class="btn btn-default" value="Reset" /></div></div>
        </form>

</div><!--Panel Body Close-->
</div><!--Panel default Close-->
</div><!--End of Col6 Form-->
</div><!--End of form Row-->
</div><!--End of Container-->

d_submit.php

    ob_start();
//global $target_path;
include_once($_SERVER["DOCUMENT_ROOT"].'/dashboard/other/crudfrm.php');  
$shaobj = new shashter();
if(isset($_POST['submit']) ){

        $name1 = mysql_real_escape_string($_POST['nametxt']);
        if(isset($_POST['taksalitxt'])){
            $taksali = true;
        }
        else {
            $taksali = false;
        }
        if(isset($_POST['fproducttxt'])){
        $fproduct = true;
        }
        else {
            $fproduct = false;
        }
        $desc = mysql_real_escape_string($_POST['desctxt']);

            //*************IMAGE MANIPULATION


    if(isset($_FILES['upimage'])){
    $errors= array();
    $file_name = $_FILES['upimage']['name'];
    $file_size =$_FILES['upimage']['size'];
    $file_tmp =$_FILES['upimage']['tmp_name'];
    $file_type=$_FILES['upimage']['type'];   
    $file_ext=strtolower(end(explode('.',$_FILES['upimage']['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';
    }               
    if(empty($errors)==true){
         // Place it into your "uploads" folder mow using the move_uploaded_file() function
        $moveResult = move_uploaded_file($file_tmp,  $_SERVER['DOCUMENT_ROOT'].'uploadimg/other/full/'.$file_name);
        //echo $moveResult;
        echo $_SERVER['DOCUMENT_ROOT']."/uploadimg/other/thumb/".$file_name."<br>";
        //exit();      

        echo "<br>Success";
    }else{
        print_r($errors);
    }
    }
}/*isset condition close*/

$thumb_path = "thumb/".$file_name;  
$full_path = "full/".$file_name;    
        //*************IMAGE MANIPULATION END
    if($moveResult) {
        echo "File Uploaded Successfull!";
    }else{
   echo "ERROR: File not uploaded. Try again.";
   // unlink($file_tmp); // Remove the uploaded file from the PHP temp folder
  //  exit();
}
//unlink($fileTmpLoc); // Remove the uploaded file from the PHP temp folder 

        // ---------- Include Universal Image Resizing Function --------
include_once($_SERVER["DOCUMENT_ROOT"]."/dashboard/image_resize_lib.php");
$target_file = $_SERVER['DOCUMENT_ROOT']."/uploadimg/other/full/$file_name";
$resized_file = $_SERVER['DOCUMENT_ROOT']."/uploadimg/other/thumb/$file_name";
$wmax = 200;
$hmax = 150;    

//echo "<br><br>../../uploadimg/shashter/full/".$file_name;
ak_img_resize($target_file, $resized_file, $wmax, $hmax, $fileExt);
// ----------- End Universal Image Resizing Function -----------
// Display things to the page so you can see what is happening for testing purposes
echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
echo "It is an <strong>$fileType</strong> type of file.<br /><br />";
echo "The file extension is <strong>$fileExt</strong><br /><br />";
echo "The Error Message output for this upload is: $fileErrorMsg";

//#################################### SAVE  RECORD USING CLASS LIBRARY ####################################
    $register = $shaobj->createpro($name1, $desc, $taksali, $fproduct, $thumb_path, $full_path );

    if($register){
        // if everything is ok, try to upload file

             echo "<script>alert('Product Added Successful')</script>";
                     header('Location: http://example.com/dashboard/admindashboard.php');
                     exit();
                }else{
                    echo "<script>alert('Product Not Added Successful')</script>";
                }

Add one check inside your if condition and put your code inside try catch block so that you will get the proper error messages. Try with this and edit as per your need :).

try {
    if (isset($_POST['submit'])) {
        if (isset($_FILES['upimage'])) {

            $errors = array();
            $file_name = $_FILES['upimage']['name'];
            $file_size = $_FILES['upimage']['size'];
            $file_tmp = $_FILES['upimage']['tmp_name'];
            $file_type = $_FILES['upimage']['type'];
            $file_ext = strtolower(end(explode('.', $_FILES['upimage']['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';
            }
            if (empty($errors) == true) {
                // Place it into your "uploads" folder mow using the move_uploaded_file() function
                $dirPath = $_SERVER['DOCUMENT_ROOT'] . '/uploadimg/other/full/';
                if (!file_exists($dirPath)) {
                    mkdir($dirPath, 0777, true);
                }
                $moveResult = move_uploaded_file($file_tmp, $dirPath . $file_name);
                //echo $moveResult;
                echo $dirPath . $file_name . "<br>";
                //exit();    
                echo "<br>Success";
            } else {
                print_r($errors);
            }
        }
    }
} catch (Exception $ex) {
    echo $ex->getMessage();
}

Hope this will help you.

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