0

first the user clicks on this button with the help of id , I am getting a image name

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


      });     
    });

I am getting the pic name from pop.js after that I am getting the pic name and i want to save it on my folder ie upload-pics .but image is not saving in the desired folder .

PHP_USER1
  • 628
  • 1
  • 14
  • 29

2 Answers2

0

I think this a dupplicate question, see: this working answer

THE FOLLOWING ANSWER IS BASED ON ORIGINAL QUESTION

Reply with a json with different status and path (fix status):

<?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");
Community
  • 1
  • 1
Ivan Buttinoni
  • 4,110
  • 1
  • 24
  • 44
  • but my problem is , the image is not saving in my folder directory – PHP_USER1 Oct 24 '13 at 05:49
  • Fatal error: Call to undefined function base64decode() ** $binaryContent = base64decode($_POST['pic'];** – PHP_USER1 Oct 24 '13 at 06:39
  • now my image name is saved in the respective folder but i am just sending the image name , its not showing me the proper image . how to send the whole image as input file – PHP_USER1 Oct 24 '13 at 07:42
  • In the **img** param of the result put the URL of the image, so the html can display it. – Ivan Buttinoni Oct 24 '13 at 16:11
  • I didnt get you @Ivan please can u explain me – PHP_USER1 Oct 25 '13 at 09:23
  • Sorry, I misunderstand you. My code DON'T SEND ANY IMAGE. Please refer http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery to see the right way to "ajax image upload" – Ivan Buttinoni Oct 25 '13 at 22:12
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.

Yogesh Gupta
  • 170
  • 1
  • 15