satya
satya

Reputation: 3560

Can not upload file into folder using PHP

I am trying to upload file into folder using PHP but it can not done. My code is 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;
   }

}

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.

Upvotes: 0

Views: 83

Answers (3)

Shanu k k
Shanu k k

Reputation: 1285

Try this

$target_dir = __DIR__ . "/admin/uploads/";

Upvotes: 0

Abhay Maurya
Abhay Maurya

Reputation: 12277

Change $targetdir to:

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

Upvotes: 2

rNix
rNix

Reputation: 2557

Your upload directory is relative to path of your script. You can change it with this:

$target_dir = $_SERVER['DOCUMENT_ROOT'] . "/admin/uploads/";

Upvotes: 0

Related Questions