Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7985785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:39:28+00:00 2026-06-04T11:39:28+00:00

I searched online for a code that does PHP image upload using ajax. I

  • 0

I searched online for a code that does PHP image upload using ajax. I found the code attached below. The issue is that I changed few things (minor tweaks) to make it work on my server. Originally it was just a php page (not a class or function) that handles the data posted from form. I made it into class then function. I am following OOP now. I thought the best way to do things in the conversion from procedural to OOP was to pass $_FILES and $_POST to a method and inside deal with them. I think this didn’t work. Look at the example and please advise on how to go forward.

function uploadImageChosen($_FILES, $_POST){
            $path = "../uploads/images/";
            $valid_formats = array("jpg", "png", "gif", "bmp");
            $connectionInstance = new ConnectionClass();
            $connectionInstance->connectToDatabase();
            $imgName;
            $imgURL;
            $imgSize;
            $imgDir = $_POST['directory'];
            if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
            {
                $name = $_FILES['photoimg']['name'];
                $imgSize = $_FILES['photoimg']['size'];
                if(strlen($name))
                {
                    list($txt, $ext) = explode(".", $name);
                    if(in_array($ext,$valid_formats))
                    {
                        if($size<(1024*1024))
                        {
                            $imgName = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
                            $tmp = $_FILES['photoimg']['tmp_name'];
                            if(move_uploaded_file($tmp, $path.$imgName))
                            {
                                $imgURL = $path.$imgName;
                                $connectionInstance->query("INSERT INTO imagesupload(id, title, url, size, directory) VALUES (null, '$imgName','$imgURL', '$imgSize', '$imgDir')");
                                //echo "<img src='uploads/".$imgName."'  class='preview'>";
                            }
                            else{
                                echo "failed";
                            }
                        }else{
                            echo "Image file size max 1 MB";                    
                        }
                    }else{
                        echo "Invalid file format..";   
                    }
            }else{
                echo "Please select image..!";
            }           

          }//end of if      

        }//end of function

As to the page where the class function is being called, here it is:

<?php
    require_once("../classes/UploadImages.php");
    $uploadInstance = new UploadImages();
    $uploadInstance->uploadImageChosen($_FILES, $_POST);
    //header("LOCATION:portfolio.php");
?>

Thank you very much 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:39:29+00:00Added an answer on June 4, 2026 at 11:39 am

    $_POST and $_FILES are superglobal arrays, they are always available, and redefining them in a function or method is a bad idea.

    You can do something like this:

    $uploadInstance->uploadImageChosen();
    

    ..

    function uploadImageChosen(){
               $path = "../uploads/images/";
               $valid_formats = array("jpg", "png", "gif", "bmp");
    ...
    $name = $_FILES['photoimg']['name'];
    ...
    

    Or if you need copies in the local scope do it like this:

    $uploadInstance->uploadImageChosen($_FILES, $_POST);
    

    ..

    function uploadImageChosen($files, $post){
                   $path = "../uploads/images/";
                   $valid_formats = array("jpg", "png", "gif", "bmp");
    ...
    $name = $files['photoimg']['name'];
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have searched online and tried the below solution but it does not work;
I have searched online for a solution, but have yet to find one that
I need a CORE that will perform AES-128 Encryption/Decryption. I have searched online but
Searched a lot about this problem but never found a answer, that solved it.
I realize this is a basic question but I have searched online, been to
Before you guys ask, yes I've searched online for the answer, but everything I
Here we go again , searched and tried myself but none of the online
Okay, I have searched online and even looked in a couple of books for
I had to clean up code for an online college application. There's nothing particularly
I have an application I am writing for Android that basically does a screen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.