这是我用于上传图像(来自W3schools)的php:
<?php
$target_dir = "images/";
$target_file = $target_dir . basename("firstfood.jpg");
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST "submit")) {
$check = getimagesize($_FILES "firstfood.jpg");
if($check !== false) {
echo "File is an image - " . $check "mime" . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES "firstfood.jpg", $target_file)) {
echo "The file ". basename( $_FILES "firstfood.jpg"). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
当我将其添加到mainphp文件中时(参见下面代码的中间部分),使用路径替换,
<?php // sqltest.php
require_once 'login.php';
$conn = new mysqli($hn, $un, $pw, $db);
if ($conn->connect_error) die($conn->connect_error);
if (isset($_POST['delete']) && isset($_POST['isbn']))
{
$isbn = get_post($conn, 'isbn');
$query = "DELETE FROM classics WHERE isbn='$isbn'";
$result = $conn->query($query);
if (!$result) echo "DELETE failed: $query<br>" .
$conn->error . "<br><br>";
}
if (isset($_POST['author']) &&
isset($_POST['title']) &&
isset($_POST['category']) &&
isset($_POST['year']) &&
isset($_POST['isbn']) &&
isset($_POST['sleeve']))
{
$author = get_post($conn, 'author');
$title = get_post($conn, 'title');
$category = get_post($conn, 'category');
$year = get_post($conn, 'year');
$isbn = get_post($conn, 'isbn');
$sleeve = get_post($conn, 'sleeve');
$query = "INSERT INTO classics VALUES" .
"('$author', '$title', '$category', '$year', '$isbn', '$sleeve')";
$result = $conn->query($query);
if (!$result) echo "INSERT failed: $query<br>" .
$conn->error . "<br><br>";
}
echo <<<_END
<form enctype="multipart/form-data" action="sqltest.php" method="post"><pre>
Author <input type="text" name="author">
Title <input type="text" name="title">
Category <input type="text" name="category">
Year <input type="text" name="year">
ISBN <input type="text" name="isbn">
Sleeve <input type="file" name="sleeve">
<input type="submit" value="Upload Image">
<?php
$target_dir = "images/";
$target_file = $target_dir . basename("firstfood.jpg");
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST "submit")) {
$check = getimagesize($_FILES "firstfood.jpg");
if($check !== false) {
echo "File is an image - " . $check "mime" . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES "firstfood.jpg", $target_file)) {
echo "The file ". basename( $_FILES "firstfood.jpg"). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
<input type="submit" value="ADD RECORD">
</pre></form>
_END;
$query = "SELECT * FROM classics";
$result = $conn->query($query);
if (!$result) die ("Database access failed: " . $conn->error);
$rows = $result->num_rows;
for ($j = 0 ; $j < $rows ; ++$j)
{
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);
echo <<<_END
<pre>
<form enctype="multipart/form-data" action="sqltest.php" method="post">
Author <input type="text" name="author" value=$row[0]>
Title <input type="text" name="title" value=$row[1]>
Category <input type="text" name="category" value=$row[2]>
Year <input type="text" name="year" value=$row[3]>
ISBN <input type="text" name="isbn" value=$row[4]>
</pre>
<input type="hidden" name="delete" value="yes">
<input type="hidden" name="isbn" value="$row[4]">
<input type="submit" value="EDIT RECORD"></form>
<input type="submit" value="DELETE RECORD"></form>
_END;
}
$result->close();
$conn->close();
function get_post($conn, $var)
{
return $conn->real_escape_string($_POST[$var]);
}
?>
它抛出了一大堆错误(所有这些错误都与我首先粘贴的用于上传图像的代码部分有关)。以下是一些:
- 注意:未定义的变量:target_dir
- 注意:未定义的变量:imageFileType
- 注意:未定义的变量:target_file
我真的很感激这方面的指导,因为我才刚刚开始,可以绕过这一点。谢谢,
您不会被解释为<?php?>插入在<lt<_END _END;
echo <<<_END
<form>
<?php $string = ''; echo $string ?>
</form>
_END;
这将被视为
echo "<form><?php $string = ''; echo $string ?></form>";
所以你会看到错误"未定义变量",因为它与相同
echo "<form><?php ". $string ."= ''; echo ". $string ." ?></form>";
检查生成的页面的源代码,你就会确定。
试试这个吧,它并不理想,但在任何情况下都有效;)
<?php ob_start(); ?>
<form>
<?php $string = ''; echo $string ?>
</form>
<?= ob_get_clean() ?>
顺便说一句,你有2x<表单>有
<input type="submit" value="EDIT RECORD"></form>
<input type="submit" value="DELETE RECORD"></form>