Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
Download image from a php function called with ajax

I have this ajax:

 $('.getid').on('click', function(){
        var imagename =  $(this).data("id");
        // you can make ajax call here to get data
        $.ajax({
        type: "POST",
        url: "Monitor/downloadDesiredImage",
        data: {'val' : imagename},
        dataType: "text"
        }).done(function(data) {
       console.log(data);        
       }).fail(function() {
    alert( "error" );
  });
});

This will work with a button that will send on.click a id value saved in a var imagename, that is a string that correspond to a image name saved on my server.

This is the php function that will get that id :

public function downloadDesiredImage() {
        $imagename = $_POST['val'];
        echo "this is my ".  $imagename;
        $file = file_get_contents('./images/'.$imagename.'.jpg', FILE_USE_INCLUDE_PATH);
        $imageData = base64_encode(($file));
        $src = 'data: '.mime_content_type($file).';base64,'.$imageData;
        echo '<img src="' . $src . '">';

    }

That will output the image in the network response, but I will also get the error :

Message: mime_content_type(): Invalid path

But what I want to do is after I click the button is to download that image from my server. How I can do that ? Thanks!

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

mime_content_type() expects the file name, not the data of the file itself. Something like this:

mime_content_type('./images/'.$imagename.'.jpg')
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!