Image Uploading without page Refresh

Discussion in 'PHP' started by forumtest, Nov 15, 2007.

  1. #1
    Hi everybody
    I have just joined this forum.
    This is my first thread in this forum.

    I made new one page for uploading image as it is selected by Browse button.

    I m still new. if something goes wrong in this code then please post your comment.

    It is just for beginner in php

    cheers

    First you need to create folder "copiedImage" and give permission to it (0777)

    the file code============
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script language="javascript">
    function confirmFun(me)
    {
    if(confirm("Are You Want To Delete"))
    {

    document.frm.action ="processUpload.php?act=DEL";
    document.frm.target = "imgFrame";
    document.frm.submit();
    return true;
    }
    else
    {
    me.checked = false;
    return false;
    }

    }
    function uploadImg()
    {
    document.frm.action ="processUpload.php?act=UP";
    document.frm.target = "imgFrame";
    document.frm.submit();

    }
    </script>
    </head>

    <body>
    <form name="frm" action="<?=$_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
    <p>File1:
    <input type="file" name="img" id="img" value="" onchange="return uploadImg()" />
    Please Upload JPG,GIFor BMP.</p>

    <p><br />
    <img id="imgId" border="0" style="display:none" width="100" height="100"/><br />
    <div id="del" style="display:none">
    <input type="radio" id="radioBtn" value="D" onclick="return confirmFun(this);">Delete Image
    </div>
    <input type="hidden" name="hdnImgNm" id="hdnImgNm" value = "" />
    </p>
    <iframe name="imgFrame" style="display:none"></iframe>
    </form>
    </body>
    </html>



    another file=============
    <?php

    if(isset($_FILES['img']) && $_FILES['img'] != "" && $_REQUEST['act'] == "UP")
    {
    //print_r($_FILES);
    $imgTmp = $_FILES['img']['tmp_name'];
    list($nm,$ext) = explode(".",$_FILES['img']['name']);

    list($h,$w,$type) = getimagesize($imgTmp);
    //echo $h.":".$w.":".$type;
    $imgNewName = date("MDY_His").".".$ext;

    if($type == 1 || $type == 2 || $type == 6)
    {
    if(copy($imgTmp,"copiedImage/".$imgNewName))
    {
    $imgPath = "copiedImage/".$imgNewName;
    errFunction(100,$imgPath);
    }
    else
    {
    errFunction(101);
    }
    }
    else
    {
    errFunction(1);
    }

    }
    if($_REQUEST['act'] == "DEL")
    {

    if(unlink($_REQUEST['hdnImgNm']))
    {
    errFunction(11);
    }
    }
    function errFunction($arg,$imgPath="")
    {
    switch($arg)
    {
    case 1: $msg = "Please Select \"JPG\", \"GIF\" or \"BMP\" files only.";break;
    case 11: $msg = "Image Deleted Successfully.";break;
    case 100: $msg = "Image Copied Successfully.";break;
    case 101: $msg = "Error- has Occured.";break;
    }
    if($imgPath != "")
    echo "<script>alert('".$msg."');parent.document.getElementById('imgId').src='".$imgPath."';parent.document.getElementById('imgId').style.display='';parent.document.getElementById('del').style.display='';parent.document.getElementById('hdnImgNm').value='".$imgPath."';parent.document.getElementById('img').value = '';parent.document.getElementById('radioBtn').checked=false;</script>";
    else
    echo "<script>parent.document.getElementById('imgId').style.display='none';parent.document.getElementById('del').style.display='none';alert('".$msg."');</script>";


    }
    ?>
     
    forumtest, Nov 15, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    In case someone would use a script made by someone you can't even write proper English, "Are You Want To Delete", note that this allows server script files to be uploaded because there isn't even something as simple as file extension checking. Get some more practice or only ask for criticism before posting scripts for people to use.

    Wrapping the code in
     tags also helps.
    Code (markup):
     
    krt, Nov 15, 2007 IP