我正在制作一个快速的PHP脚本,以便在办公室的计算机上本地运行。我们的想法是,我们可以在不允许在线访问的情况下管理我们办公室的竞争。 FTP上传是我们可以将文件上传到我们的FTP帐户。
我有以下HTML表单代码:
<form method="post" id="new_competition" enctype="multipart/form-data">
<?php if(isset($error)){ echo "<p class='error'>$error</p>"; } ?>
<?php if(empty($done)){ ?>
<h1>Love Hearts Competition Entry</h1>
<label>Opening Date: </label><input type="date" name="opening_date" value="<?= $opening_date; ?>" placeholder="Opening Date" required>
<label>Closing Date: </label><input type="date" name="closing_date" value="<?= $closing_date; ?>" placeholder="Closing Date" required>
<label>Title: </label><input type="text" name="title" value="<?= $title; ?>" placeholder="Competition Title" required>
<label>Question: </label><textarea name="question" placeholder="Competition Question" required><?= $question; ?></textarea>
<div class="answers">
<?php if(!empty($answers)){
foreach ($answers as $answer) {?>
<div class="answer"><label class="answer_label">Answer: </label><input type="text" name="answer[]" value="<?= $answer; ?>" placeholder="Answer text" required/><div class="remove_answer">X</div></div>
<?php }
} else { ?>
<div class="answer"><label class="answer_label">Answer: </label><input type="text" name="answer[]" value="" placeholder="Answer text" required/><div class="remove_answer">X</div></div>
<?php } ?>
</div>
<div class="add_answer">Add another answer</div>
<label for="terms">Terms & Conditions File</label><input type="file" name="terms" placeholder="Terms & Conditions File" required />
<input type="submit" value="Add Competition" name="submit" id="submit" />
<?php } else { ?>
<p class='done'>Successfully added competition</p>
<?php } ?>
</form>
而且,正如您所看到的,我使用了正确的enctype。
我的PHP代码如下:
if($_POST['submit']){
$opening_date = $_POST['opening_date'];
$closing_date = $_POST['closing_date'];
$title = $_POST['title'];
$question = $_POST['question'];
$answers = $_POST['answer'];
$terms = $_FILES['terms']['name'];
$tmp = $_FILES['terms']['tmp_name'];
$date_diff = (date_diff(date_create($opening_date), date_create($closing_date)));
if(!$date_diff->invert){
if(count($answers) > 1){
$ftpCon = ftp_connect($host_ip);
$ftpLogin = ftp_login($ftpCon, $host_login, $host_pswd);
$remoteURL = '/public_html/competition_terms/'.$terms;
// Upload the file!
$ftpUpload = ftp_put($ftpCon, $remoteURL, $tmp, FTP_BINARY) or die("Unable to load upload file");
if($ftpUpload){
$answers_json = json_encode($answers);
$db = mysql_connect($db_host, $db_user, $db_pwd) or die('cannot connect to db: '.mysql_error());
$db = mysql_select_db('lovehear_competitions') or die(mysql_error());
$query = mysql_query("INSERT INTO competitions (opening_date, closing_date, title, question, answers, terms) VALUES ('$opening_date', '$closing_date', '$title', '$question', '$answers_json', '".str_replace("public_html/", "", $remoteURL)."')") or die(mysql_error());
$done = true;
} else {
$error = "Unable to upload file.";
}
ftp_close($ftpCon);
} else {
// Only one answer
$error = "You must give more than one answer";
}
} else {
// Closing date before opening date
$error = "You must have a closing date that ends after the opening date.";
}
}
该文件在我的服务器上创建,位于“public_html / competition_terms /”下,但该文件包含零字节。
有谁知道为什么?我似乎无法在网上看到任何有用的帮助,PHP文档似乎没有提到本地到远程上传的任何限制。
答案 0 :(得分:0)
我想我是通过重新启动MAMP服务器来解决它的。