0

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.

8
  • may be your directory don't have read-write permission. change the folder permission to 077 Commented Nov 25, 2016 at 4:35
  • Check you permissions for the directory. Commented Nov 25, 2016 at 4:36
  • what does the php error_log say? The directory will not only need to be writeable, but also owned by the same user that the webserver runs as (eg apache/nginx) - assuming this is a linux server? Commented Nov 25, 2016 at 4:37
  • I have the folder permission. Commented Nov 25, 2016 at 4:38
  • @flauntster : Yes,this is ubuntu server. Commented Nov 25, 2016 at 4:39

3 Answers 3

2

Change $targetdir to:

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

0

Try this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.