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

  • SEARCH
  • Home
  • 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 7619805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:46:52+00:00 2026-05-31T03:46:52+00:00

For the longest time, I’ve been using a very standard HTML post to my

  • 0

For the longest time, I’ve been using a very standard HTML post to my PHP upload script for my image upload. I wanted to try something new by sending the image to the PHP uplaod script via JQuery/JSON. All text data on my site is sent through this and I wanted to try and get image uploads to work with JQUery. I googled, but only found pre-made scripts, no one ever has a tutorial on making it yourself or where to begin, there’s nothing wrong with using premade scripts but alot of times I can’t get it to work with my website or style it so that it fits in, so I began to write a quick something, can you guys please tell me if this would work being that an image isn’t text data?

My HTML:

<div class="upload_box_logged">
<input type="file" id="file_up"><br />
<button id="up_btn">Upload</button>
<em style="display:none; color:red;" id="no_file"  >Please Select a File!</em>
<em style="display:none; color:red;" id="up_fail" >Failed!</em>
<em style="display:none; color:red;" id="up_success" >Success!</em>
</div>

My JQuery:

$('#up_btn').click(function(){

var fileCheck = $('#file_up').val();
    if(fileCheck == '')
    {
        $('#no_file').fadeIn();
    }

    else
    {
        var file_upVar = encodeURIComponent($('#file_up').val());

             $.ajax({ 
            type: 'POST', url: 'upload.php',  dataType: "json",  data: { file_up: file_upVar  }, 
            success: function(result) {                 
                if (!result.success) { $('#up_fail').fadeIn().fadeOut(5000); }  
                else { $('#up_success').fadeIn().fadeOut(5000);  } 
            }  
        }); 


    }


});

Now here’s my PHP file that I’ve been using with my standard HTML post to PHP method:

$file_name = $HTTP_POST_FILES['ufile1']['name'];
$file_name_for_db = ($HTTP_POST_FILES['ufile1']['name']);
$new_file_name = $id."_pic1_".$file_name;


    $allowedTypes = array("image/jpg", "image/jpeg", "image/png"); 
    $maxSize = 5 * 1024 * 1024; // 3Mb 

    $fileType = $HTTP_POST_FILES['ufile1']["type"]; 
    $fileSize = $HTTP_POST_FILES['ufile1']["size"]; 

    // check if there was an error 
    if ($HTTP_POST_FILES['ufile1']["error"] > 0) 
    { 
        die($HTTP_POST_FILES['ufile1']["error"]); 
    } 

    // check if the filetype is valid 
    if (!in_array($fileType, $allowedTypes)) 
    { 
        die("Invalid file type: $fileType"); 
    } 

    // check if the size doesn't exceed the limitations 
    if ($fileSize > $maxSize) 
    { 
        die("The file was too big: $fileSize"); 
    } 

    $name = $HTTP_POST_FILES['ufile1']["name"]; 
    $tmpfile = $HTTP_POST_FILES['ufile1']["tmp_name"]; 

    // check if the filename is valid 
    if (preg_match("/[\w-]+\.(jpg|jpeg|png)$/", $name) != 1) 
    { 
        die("Invalid file name: $name"); 
    } 

    $path = "../pic/";

    move_uploaded_file($tmpfile, $path); 

I will obviously have to de-code the post with something like this.. well I would if this is a correct way of going about it.

$uploaded_file = htmlspecialchars(trim(urldecode($_POST['file_up'])));

Well this is how I’d do it if it were text. Any way I can do this with image data?

Thanks a bunch
-Mike

  • 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-05-31T03:46:54+00:00Added an answer on May 31, 2026 at 3:46 am

    Unfortunately I don’t believe there is a way to post Images & Files via Ajax.

    The way I’ve normally done it in the past is creating a hidden iframe and making the form target the iframe name.

    So an example would look like:

    <form name="imageform" method='POST' action="upload_image.php" target="image_upload_frame" enctype="multipart/form-data">
        ...
    </form>
    <iframe name="image_upload_frame" height="1" width="1" style="visibility: hidden"></iframe>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the longest time now I've been trying to convert HTML pages containing large
For the longest time, I've been using the following basic formatting for SQL queries
For the longest time, I've been using MySQL servers to handle data (in JAVA,
I was using the serializing approach between Flash and PHP for the longest time
For the longest time I've been using AutoMapper to both map my domain models
For the longest time I've been interested in building a cluster of heterogeneous nodes
I've been beating my head against this reg ex for the longest time now
I've been wondering for the longest time WHY I actually need a live resource
I've been trying for the longest time to figure this out but nothing seems
I've been trying to format the output to the console for the longest time

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.