I am working on a php script for multiple file uploading and getting json data from server for the file uploaded. However, the json response is not coming how it is supposed to be
php script
$con= new mysqli('localhost','root','','app');
$name='Dp User id ';
if(!empty($_FILES['files']['name'])&&!empty($_POST['id']))
{
$id=$_POST['id'];
$files=$_FILES['files'];
$response=array();
$uploaded=array();
$failed=array();
$allowed=array('txt','jpeg','sig','pak','dll','jpg');
foreach($files['name'] as $position=>$file_name)
{
$file_tmp=$files['tmp_name'][$position];
$file_size=$files['size'][$position];
$file_error=$files['error'][$position];
$file_ext1=explode('.', $file_name);
$file_ext=strtolower(end($file_ext1));
if(in_array($file_ext,$allowed))
{
if($file_error===0)
{
if($file_size<=2097152)
{
$file_name_new=$name.$id.'.'.$file_ext;
$upload_url = $_SERVER['DOCUMENT_ROOT'].'/app2/User Files/User Id-'.$id.'/';
$file_destination=$upload_url.$file_name_new;
$Sql_Query = $con -> prepare("UPDATE profile SET pic= ? WHERE id = ?");
$Sql_Query -> bind_param("si", $file_destination,$id);
$sql = $Sql_Query -> execute();
if(move_uploaded_file($file_tmp,$file_destination))
{
//$uploaded[$position]=$file_destination.'<br>';
array_push($uploaded,array($position=>$file_destination.'<br>'));
}else{
$failed[$position]="[{$file_name}] failed to upload".mysqli_error($con).'<br>';
}
}else{
$failed[$position]="[$file_name] is too large.".mysqli_error($con).'<br>';
}
}else{
$failed[$position]="[$file_name] has error {$file_error}".mysqli_error($con).'<br>';
}
}else{
$failed[$position]="[$file_name] has {$file_ext} which is not allowed".mysqli_error($con).'<br>';
}
}
if(!empty($uploaded))
{
echo (json_encode(array(
"success"=>1,
"message" => $uploaded)));
}
if(!empty($failed))
{
echo json_encode(array(
"success" => 0,
"message" => $failed));
}
}
mysqli_close($con);
?>
I want json to be returned for this script like this
{"success":1,
"message":[{"C:\/Apache24\/htdocs\/app2\/User Files\/User Id-94\/Dp User id 94.jpg
"},
{"1":"C:\/Apache24\/htdocs\/app2\/User Files\/User Id-94\/Dp User id 94.jpg
"},
{"2":"C:\/Apache24\/htdocs\/app2\/User Files\/User Id-94\/Dp User id 94.jpg
"}]}
but the json is coming like this
{"success":1,
"message":[["C:\/Apache24\/htdocs\/app2\/User Files\/User Id-94\/Dp User id 94.jpg
"],
{"1":"C:\/Apache24\/htdocs\/app2\/User Files\/User Id-94\/Dp User id 94.jpg
"},
{"2":"C:\/Apache24\/htdocs\/app2\/User Files\/User Id-94\/Dp User id 94.jpg
"}]}
The 0th element is between the array, and i want it to be between object like the rest. Whenever i choose files and upload it, the 0th one always end up showing like this in response.Any help will be appreciated
Thanks in advance
php array with integer keys (including implicitly cast strings) starting from 0
gets encoded as JSON array but not JSON object. Meanwhile php array with string keys or integer keys not starting from 0 (I think that's your case here) will get encoded into JSON object by json_encode
.
In your case you have two options:
If you want your output to contain all objects instead of array for 1st (0)
element, just increase your $position
variable by 1
when pushing elements
into your $uploaded
and $failed
arrays:
array_push($uploaded,array($position+1=>$file_destination.'<br>'));
^^
$failed[$position+1]=...
^^
That of course will result in 1-based instead of zero-based list in your JSON output.
Alternatively you can also pass JSON_FORCE_OBJECT
as a second parameter to json_encode
but that will get rid of your outer array for message
...