无法使用PHP将文件上传到文件夹中

时间:2016-11-25 04:32:58

标签: php file

我正在尝试使用PHP将文件上传到文件夹但无法完成。我在下面解释我的代码。

user.php的:

window.addEventListener('load', () => {
  const width = screen.width;
  if (width > 1300) {
    render((
      <SomeComponent />
    ), document.getElementById('root'));
  }
  else {
    render((
      <div>404 not found </div>
    ), document.getElementById('root'));
  }
});

我收到消息$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));

在这种情况下,我总是无法将文件上传到文件夹中。

3 个答案:

答案 0 :(得分:2)

$targetdir更改为:

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

答案 1 :(得分:0)

您的上传目录与脚本的路径相关。你可以改变它:

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

答案 2 :(得分:0)

试试这个

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