2016-11-25 152 views
0

我有一个问题。我试图使用PHP将文件上传到文件夹中,但无法完成。我在下面解释我的代码。无法使用PHP将文件上传到文件夹中

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; 
    } 

} 

这里我得到消息Sorry, Your Image could not uploaded to the directory.。在这里我得到的输入$_FILES是像下面。

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

我也有文件夹写权限。我的目录结构如下所示。

root folder 

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


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

在这种情况下,总是我无法将文件上传到文件夹。请帮我解决这个问题。

+0

可能是你的目录没有读写权限。将文件夹权限更改为077 –

+0

检查目录的权限。 – Samay

+0

php error_log说什么?该目录不仅需要可写,而且还由网络服务器运行的同一用户拥有(例如apache/nginx) - 假设这是一台Linux服务器? – flauntster

回答

2

更改$targetdir到:

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

0

您的上传目录与脚本的路径有关。您可以使用此更改:

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

试试这个

$target_dir = __DIR__ . "/admin/uploads/"; 
相关问题