使用PHP的文件上载实用程序

时间:2015-02-11 22:07:31

标签: php html

我正在构建一个网页,允许用户将多个文件上传到我的网络服务器。它是一个简单的html页面和PHP脚本。我有PHP错误,我不知道为什么。任何帮助是极大的赞赏。

这是我的HTML。

<html>
<head>
<title>File Upload</title>
</head>
<body>

<form enctype="multipart/form-data" action="upload.php" method="POST">
    Your Name:
    <select name="uname">
    <option value="Test">Test</option>
    </select>
<br>
    Your Company:
    <select name="company">
    <option value="TestCom">testCompany</option>
    </select>
<br>
    Choose file(s) to upload (Max 500MB): 
    <input name="files[]" type="file" id="files" multiple="multiple" />
    <input type="submit" name="submit" value="Upload" />
</form>
</body>
</html>

这是我的php

<?php
$uname = $_POST['uname'];
$company = $_POST['company'];
$dir = "D:/File Upload/uploads/$uname/$company/";
$count = 0;

if ($_SERVER['REQUEST_METHOD'] == 'POST' and isset($_FILES['files']))
{
  // loop all files
  foreach ( $_FILES['files']['name'] as $i => $name )
  {
    // if file not uploaded then skip it
    if ( !is_uploaded_file($_FILES['files']['tmp_name'][$i]) )
      continue;

    // now we can move uploaded files
      if( move_uploaded_file($_FILES["files"]["tmp_name"][$i], $dir . $name) )
        $count++;
  }

  echo json_encode(array('count' => $count));

}
?>

这是php日志中的错误。

[11-Feb-2015 16:01:04 America/Chicago] PHP Warning:  move_uploaded_file(D:/File Upload/uploads/Test/TestCom/image.png): failed to open stream: No such file or directory in D:\File Upload\upload.php on line 17
[11-Feb-2015 16:01:04 America/Chicago] PHP Warning:  move_uploaded_file(): Unable to move 'D:\phptempdata\php48F.tmp' to 'D:/File Upload/uploads/Test/TestCom/image.png' in D:\File Upload\upload.php on line 17

1 个答案:

答案 0 :(得分:0)

原来我没有在文件系统上实际创建的目录。

我一直在努力上传文件,我不会停下来问这个文件夹是否真的存在。

我添加了这一行来创建文件夹(如果它不存在)。

if (!file_exists($dir)) { mkdir($dir, 0777, true); }