简体   繁体   English

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

[英]html + php upload form is not uploading the file

I am currently trying to debug why my PHP script isn't currently working. 我目前正在尝试调试为什么我的PHP脚本当前无法正常工作。 Essentially, I have a web form that end user creates new group name, then has the option to upload 1 of 2 different files. 本质上,我有一个Web表单,最终用户可以创建新的组名,然后可以选择上传2个不同文件中的1个。

html: 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:

<?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>";
?>

debug from PHP: 从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
    )

)

I am sure its something simple that i am not doing. 我敢肯定,我没有在做些简单的事情。 Keep in mind this is not a public facing application its a small local host virtual appliance. 请记住,这不是面向公众的应用程序,而是一个小型本地主机虚拟设备。

So I found online a better more detailed example of the upload process for PHP. 所以我在网上找到了一个更详细的PHP上传过程示例。 This in turn helped solve the issue. 这反过来帮助解决了这个问题。

This is what I am using now - works perfectly. 这就是我现在使用的 - 完美的工作。

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

This is in conjunction with what i have already setup with the HTML. 这与我已经使用HTML设置的内容相结合。
Found this which went into detail and had a very much working copy documented. 发现了这一点,并进行了详尽的记录,并记录了很多工作副本。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM