0

首先,用户在 id 的帮助下单击此按钮,我得到一个图像名称

表单.php

<form name="frm1" action="3.php" method="POST" enctype="multipart/form-data">
                                <div class="upload">
                                    <input type="hidden" name="r" value="father">
                                    <input type="hidden" name="id" value="<?=$id?>">
                                    <input type="hidden" name="page" value="<?=$a?>">
                                    <input type="file" name="uploadPic" onchange="frm1.submit();" />
                                </div>
                            </form>

上传.php

<?php
if(isset($_POST['pic'])) {
//echo $_POST['pic'] ;
$upload=false;
$baseName = basename($_POST['pic']);
$baseName=uniqid().'_'.$baseName; 
//echo $baseName; 
$toret = array("result" => "","img"=>"");
if (file_exists("./upload-pics/".$baseName)) 
                                        $uploadResponse = "exists";
                                else {
                                        move_uploaded_file('./upload-pics/' .$baseName);
                                        $upload=true;
 }
 }
?>

pop.js

  $(".upload").on("click", function () {
    console.log("page");
    $(".upload").on("change", function () {

     var fr =$("#r").val();
     var fr1 =$("#r1").val();
     var fr2 =$("#r2").val();
     var fr3 =$("#upload").val();
     //console.log("page"+fr+fr1+fr2+fr3);
     $.post("upload.php", {
            "pic": fr3}, function (data) {
                    console.log(eval(data));
                    if (data.result == "1") {
                        console.log("getresult");
                        //webpopup();

                    } else {
                        alert("Please try again.");
                    }
                }, "json");


      });     
    });

我从pop.js获取图片名称之后,我得到了图片名称,我想将它保存在我的文件夹中,即上传图片。但是图像没有保存在所需的文件夹中。

4

2 回答 2

0

我认为这是一个重复的问题,请参阅: this working answer

以下答案基于原始问题

回复具有不同状态和路径(修复状态)的 json:

<?php
if (isset($_POST['pic'])) {
//echo $_POST['pic'] ;
    $upload   = false;
    $baseName = basename($_POST['pic']);
    $baseName = uniqid() . '_' . $baseName;

    $uploadResponse = array(
        'status' => "KO",
        'path' => "",
    );

//echo $baseName; 
    $toret = array("result" => "", "img" => "");
    if (file_exists("./upload-pics/" . $baseName))
        $uploadResponse = array(
            'status' => "exists",
            'path' => "upload-pics/" . $baseName,
        );

    else {
        // hypotize a base64 content
        $binaryContent = base64_decode($_POST['pic'];

        file_put_contents('./upload-pics/' . $baseName, $binaryContent);
        $uploadResponse = array(
            'status' => "OK",
            'path' => "upload-pics/" . $baseName,
        );
    }
    header('Content-type: application/json');
    return json_encode($uploadResponse);
}

pop.js

$.post("upload.php", { "pic": fr3 }, function (data) {
                console.log(eval(data));
                // manage different status
                if (data.status == "OK") {
                    console.log("getresult: "+data.path);
                    //webpopup();

                } else {
                    alert("Please try again.");
                }
            }, "json");
于 2013-10-24T05:41:28.450 回答
0

move_uploaded_file() function takes two parameter.1. file name, 2 new location.

Make sure that the folder you using has write access on the server.

于 2013-10-24T05:45:37.980 回答