html + php上传表单没有上传文件

时间:2016-03-26 00:25:26

标签: php html html5

我目前正在尝试调试为什么我的PHP脚本目前还没有工作。基本上,我有一个最终用户创建新组名的Web表单,然后可以选择上传2个不同文件中的1个。

HTML:

<form method="post" action="/php/create.php" enctype="multipart/form-data">
<div class="form-group">
<label for="customerName">Customer Name:</label>
<input type="text" class="form-control" id="foldername" name="foldername" placeholder="Customer Name">
</div>
<div class="form-group">
<label for="healthCheckOutput">HealthCheck Results</label>
<input type="file" id="healthCheckFile">
</div>
<div class="form-group">
<label for="SANUpload">Appliance Data</label>
<input type="file" id="SANUpload" name="SANUpload">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>

php:

<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
//Create Customer Folder
$folder = $_POST['foldername'];

$dirPath = '../customers/'.$folder;
if (!mkdir($dirPath, 0750, true)) {
    die('Failed to create customer folders...current customer name already exists');
}

//Upload the file and move it to the correct folders for processing
//$target_dir = "/uploads/";
//$target_file = $target_dir . basename($_FILES["SANUpload"]["name"])

$uploaddir = "uploads/";
$uploadfile = $uploaddir . basename($_FILES['SANUpload']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['SANUpload']['tmp_name'], $uploadfile)){
echo "Success.\n";
} else {
echo "Failure.\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>

从PHP调试:

Warning:  move_uploaded_file(uploads/test2.csv): failed to open stream: No such file or directory in /var/www/html/php/create.php on line 26

Warning:  move_uploaded_file(): Unable to move '/tmp/php8E2y8j' to 'uploads/test2.csv' in /var/www/html/php/create.php on line 26

Failure.
Here is some more debugging info:Array
(
[SANUpload] => Array
    (
        [name] => test2.csv
        [type] => text/csv
        [tmp_name] => /tmp/php8E2y8j
        [error] => 0
        [size] => 58242
    )

)

我确信它不是一件简单的事我不会这样做。请记住,这不是面向公众的应用程序,而是一个小型本地主机虚拟设备。

1 个答案:

答案 0 :(得分:0)

所以我在网上找到了一个更详细的PHP上传过程示例。这反过来帮助解决了这个问题。

这就是我现在使用的 - 完美无缺。

PHP:

define("UPLOAD_DIR", "/var/www/html/uploads/");

if (!empty($_FILES["SANUpload"])) {
$myFile = $_FILES["SANUpload"];

if ($myFile["error"] !== UPLOAD_ERR_OK) {
    echo "<p>An error occurred.</p>";
    exit;
}

// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "_", $myFile["name"]);

// don't overwrite an existing file
$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
    $i++;
    $name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}

// preserve file from temporary directory
$success = move_uploaded_file($myFile["tmp_name"],
    UPLOAD_DIR . $name);
if (!$success) { 
    echo "<p>Unable to save file.</p>";
    exit;
}
// set proper permissions on the new file
chmod(UPLOAD_DIR . $name, 0644);
}

这与我已经使用HTML设置的内容相结合 发现了这个细节,并记录了非常多的工作副本。

http://www.sitepoint.com/file-uploads-with-php/