satya satya - 16 days ago 5
PHP Question

Can not upload file into folder using PHP

I have an issue.I am trying to upload file into folder using PHP but it can not done. I am explaining my code below.


user.php:


$target_dir = "admin/uploads/";
$target_file = $target_dir . basename($_FILES['file']['name']);
$imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
$uploadOk = 1;
$check = getimagesize($_FILES['file']['tmp_name']);
header('Content-Type: application/json');
if ($check !== false) {

$uploadOk = 1;
} else {

$uploadOk = 0;
}

if (file_exists($target_file)) {
$uploadOk = 0;
}
if ($uploadOk == 0) {
} else {
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) {
$result['msg'] = "Image has uploaded successfully.";
$result['num'] = 1;
$result['img'] =$_FILES['file']['name'];
} else {
$result['msg'] = "Sorry, Your Image could not uploaded to the directory.";
$result['num'] = 0;
}

}


Here i am getting the message
Sorry, Your Image could not uploaded to the directory.
.Here i am getting the input for
$_FILES
is like below.

$_FILES=array('file'=>array('name' => 'IMG-20161121-WA0000.jpg','type' => ' application/octet-stream','tmp_name' => '/tmp/phpSb6a53', 'error' => 0, 'size' => 119198));


I have also the folder write permission.My directory structure is like below.

root folder

->admin
=>uploads//(images need to saved)


-> API
=>V1
->user.php(//here is my file upload code)


In this case always i am unable to upload the files into folder. Please help me to resolve this issue.

Answer

Change $targetdir to:

$targetdir = '../../admin/uploads';