0

I am trying to upload images from client to server. My Client and server machines are separate so I need to send the image data from client to server.

My Client side php code goes like (Full client-side code):

$filename = $_FILES["fileToUpload"]["name"];
$filedata = $_FILES['fileToUpload']['tmp_name'];
$imagedata = file_get_contents($_FILES['fileToUpload']['tmp_name']);

$fields = array(
            'filename' => $filename,
            'filedata' => "@$filedata"
            'imagedata' => "@$imagedata"
        );
$field_string = http_build_query($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://10.2.16.102/temp.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $field_string);
$server_output = curl_exec ($ch);
curl_close ($ch);

and my Server side code is (Full server-side code):

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

echo " and the filename is ". $_POST['filename'];
$uploadpath = "/uploads/";
$target_file = $_SERVER['DOCUMENT_ROOT'].  $uploadpath . $_POST['filename'];

file_put_contents($target_file, $_POST['imagedata']);
//move_uploaded_file($_POST['filedata'],$target_file);
//copy($_POST['filedata'],$target_file);

?>

I am able to store the image in my uploads folder and the filesize is also the same but I am not able to see the image. The image viewer says "Couldn't load the png file". Do I have to convert the file into png format before storing. Also how to do that? Any help?

In a nutshell, I need a converter which can convert the file format into png or any other image format which can convert my file into an image at the server end.

diggy
  • 351
  • 1
  • 5
  • 23

1 Answers1

1

Use $_FILES instead of $_POST in move_uploaded_file function e.g: move_upload_file($_FILES[filedata][tmp_name], YOUR_PATH/$_FILES[filedata][name]);

Here's PHP doc: http://php.net/manual/fr/function.move-uploaded-file.php

Mathieu Smith
  • 378
  • 1
  • 13
  • Your answer is not clear. Are you saying I don't need to change the client side code at all and just use move_uploaded_file($_FILES['filedata']['tmp_name'],$target_file);? I am getting this error "Notice: Undefined index: filedata" – diggy Nov 06 '14 at 12:36
  • I have also put full php codes if you wanna have a look. – diggy Nov 06 '14 at 12:40
  • Your answer works if I POST directly to the server. What I have here is you can call a bypass php code. I am converting the image into file format but I am not able to convert it back again into png format on the server end. – diggy Nov 06 '14 at 13:14
  • I see what you mean, maybe a look at this could help:http://stackoverflow.com/questions/755781/convert-jpg-image-to-gif-png-bmp-format-using-php – Mathieu Smith Nov 06 '14 at 13:23