【问题标题】:php move_uploaded_file function not working but record save in databasephp move_uploaded_file函数不起作用但记录保存在数据库中
【发布时间】:2015-11-30 04:50:43
【问题描述】:

我设计了代码来上传托管服务器数据库中的图像和数据信息。记录信息保存在表格中。但图像不会移动到服务器文件夹。我也尝试rename()Copy(),但没有工作。 奇怪的是,我设计了四种上传信息表格(a、b、c 和 d),但 d 不起作用。所有 3 都工作正常。所有文件中的代码都相同。

我的代码在文件中

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>";
                }

【问题讨论】:

  • PHP 结束标签在您的 form 标签中被编码:?&amp;gt;
  • 是的,我对其进行编码,因为在 stackoverflow 上发布问题
  • 好的,请问您的move_uploaded_file 电话是否已接通?顺便说一句:您正在将文件移动到uploadimg/other/full/,但在您的echo 中您使用的是uploadimg/other/thumb/
  • 这只是检查 $file_name 变量携带文件名和路径的回声。这条线没有功能只是回显。
  • 您的move_uploaded_file 电话接通了吗?您是否具有写入目录uploadimg/other/full/ 所需的权限?在move_uploaded_file 调用之后,$moveResult 的值是多少?也请查看您的php_error_log 以获取警告、通知等。

标签: javascript php jquery css dom


【解决方案1】:

在 if 条件中添加一项检查并将代码放入 try catch 块中,以便您获得正确的错误消息。试试这个并根据您的需要进行编辑:)。

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();
}

希望这会对你有所帮助。

【讨论】:

  • 我认为您拥有上述代码的所有文件写入权限。祝你编码愉快:)。
猜你喜欢
  • 2016-12-14
  • 2020-11-04
  • 2023-03-10
  • 2013-09-26
  • 2018-07-22
  • 2018-07-06
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多