Reputation: 3560
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
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