OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Uploading image file to php server [duplicate]

  • Thread starter Thread starter Franjo Sunjic
  • Start date Start date
F

Franjo Sunjic

Guest
I am developing a simple registration system, with javascript, Ajax and php. I am having problem with uploading the picture of the registrant to the server. I have 2 url parameters that were passed to the server. All colected info are passed as single JSON object, second parameter is object containing the image file. I am stuck on server side where my PHP script should parse received parameters and store them in the database. JSON object is processed with no issues, however the image object is one that is causing me all the grief. Any help or tip of what i am doing wrong would be appreciated.

Here is how i get image file:

Code:
<div class="custom-file">  
 <input type="file" class="custom-file-input" id="user_picture" aria-describedby="user_picture" required>  
 <label class="custom-file-label" for="user_picture"> Choose file </label>  
</div>

Here is my javascript that sends file to the server:

Code:
var files = document.getElementById("user_picture").files;

// Create formData object
var formData = new FormData();

// Might or not be a multi file input, so loop through it
for (var i = 0; i < files.length; i++) {
  var file = files[i]

  formData.append('upfile', file);
}
// Url parameters
var t_params = "otsl_action=player_reg&otsl_data=" + JSON.stringify(regDataObj) + '&player_image=' + formData;
pbc_env = window.location.protocol + "//" + document.location.hostname;

var url = pbc_env + "/otsl_api.php?" + t_params;
var result_obj = '';
$.getJSON(url, function(result) {
  for (var i in result) {
    if (result.hasOwnProperty(i)) {
      console.log("Got result: " + result.success + " Message: " + result.message);
      result_obj = result;
      break;
    }
  }

I tried with JSON call and Ajax with same result.

The PHP script gets two parameters and assigns them to the PHP variables:

Code:
$otsl_data = $_GET['otsl_data'];
$player_image = $_GET['player_image'];
$otsl_data = isset($otsl_data)?$otsl_data:$_REQUEST['otsl_data'];
$player_image = isset($player_image)?$player_image:$_REQUEST['player_image'];

Then try to process them in the specific function:

Code:
  protected function setRegistration($parameters) {
    $p_registration_data = $parameters['otsl_data'];
    $p_registrationObj = json_decode($p_registration_data, false);
    $player_image  = $parameters['player_image'];

Does not what i tried - could not get the way to access object ($player_image) and have file uploaded.

How should i the image?

<p>I am developing a simple registration system, with javascript, Ajax and php. I am having problem with uploading the picture of the registrant to the server. I have 2 url parameters that were passed to the server. All colected info are passed as single JSON object, second parameter is object containing the image file.
I am stuck on server side where my PHP script should parse received parameters and store them in the database. JSON object is processed with no issues, however the image object is one that is causing me all the grief. Any help or tip of what i am doing wrong would be appreciated.</p>
<p>Here is how i get image file:</p>
<pre><code><div class="custom-file">
<input type="file" class="custom-file-input" id="user_picture" aria-describedby="user_picture" required>
<label class="custom-file-label" for="user_picture"> Choose file </label>
</div>
</code></pre>
<p>Here is my javascript that sends file to the server:</p>
<pre><code>var files = document.getElementById("user_picture").files;

// Create formData object
var formData = new FormData();

// Might or not be a multi file input, so loop through it
for (var i = 0; i < files.length; i++) {
var file = files

formData.append('upfile', file);
}
// Url parameters
var t_params = "otsl_action=player_reg&otsl_data=" + JSON.stringify(regDataObj) + '&player_image=' + formData;
pbc_env = window.location.protocol + "//" + document.location.hostname;

var url = pbc_env + "/otsl_api.php?" + t_params;
var result_obj = '';
$.getJSON(url, function(result) {
for (var i in result) {
if (result.hasOwnProperty(i)) {
console.log("Got result: " + result.success + " Message: " + result.message);
result_obj = result;
break;
}
}
</code></pre>
<p>I tried with JSON call and Ajax with same result.</p>
<p>The PHP script gets two parameters and assigns them to the PHP variables:</p>
<pre><code>$otsl_data = $_GET['otsl_data'];
$player_image = $_GET['player_image'];
$otsl_data = isset($otsl_data)?$otsl_data:$_REQUEST['otsl_data'];
$player_image = isset($player_image)?$player_image:$_REQUEST['player_image'];

</code></pre>
<p>Then try to process them in the specific function:</p>
<pre><code> protected function setRegistration($parameters) {
$p_registration_data = $parameters['otsl_data'];
$p_registrationObj = json_decode($p_registration_data, false);
$player_image = $parameters['player_image'];

</code></pre>
<p>Does not what i tried - could not get the way to access object ($player_image) and have file uploaded.</p>
<p>How should i the image?</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top