将图像保存在文件夹中并使用ajax获取图像名称

时间:2013-10-24 05:30:12

标签: php ajax image

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

form.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>

upload.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 获取图片名称后,我得到图片名称,我想将其保存在我的文件夹中,即 upload-pics 。图像未保存在所需的文件夹中。

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分:0)

move_uploaded_file()函数有两个参数。文件名,2个新位置。

确保您使用的文件夹在服务器上具有写入权限。