douhuo3696 2015-09-04 06:03
浏览 73

php move_uploaded_file函数不工作但记录保存在数据库中

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>";
                }
  • 写回答

1条回答 默认 最新

  • doulu5109 2015-09-04 06:53
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢