簡體   English   中英

html + php上傳表單沒有上傳文件

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

我目前正在嘗試調試為什么我的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
    )

)

我敢肯定,我沒有在做些簡單的事情。 請記住,這不是面向公眾的應用程序,而是一個小型本地主機虛擬設備。

所以我在網上找到了一個更詳細的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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM