Search code examples
imagemodxmodx-revolution

how to upload images without reloading page in modx revolution


How to upload images in web context (frontend) without reloading the page in modx revolution? I am trying to use fileupload extra for uploading images but it is reloading the page. Can anyone help me, please?


Solution

  • finally i got the answer. we can do it without using FILEUPLOAD addon by using the simple php code and a small javascript.. php code:-

    <?php
    
    $path = "uploads/";
    
    $valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
    {
    $name = $_FILES['photoimg']['name'];
    $size = $_FILES['photoimg']['size'];
    if(strlen($name))
    {
    list($txt, $ext) = explode(".", $name);
    if(in_array($ext,$valid_formats))
    {
    if($size<(1024*1024)) // Image size max 1 MB
    {
    $actual_image_name = time().".".$ext;
    $tmp = $_FILES['photoimg']['tmp_name'];
    if(move_uploaded_file($tmp, $path.$actual_image_name))
    {
    
    echo "image uploaded";
    }
    else
    echo "failed";
    }
    else
    echo "Image file size max 1 MB";
    }
    else
    echo "Invalid file format..";
    }
    else
    echo "Please select image..!";
    exit;
    }
    

    for other javascript and all u can refer the below link:- http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html