我想知道如何使用 jquery 验证和发布图片上传论坛。只有两个输入:标题和图像。如果这些字段中的任何一个为空或上传的文件格式错误,我需要给出一个错误。这是我的代码(目前不使用 jquery。)
图片.php
<?php
$uploadDir = 'images/'; //Image Upload Folder
if(isset($_POST['Submit']))
{
$title = mysql_real_escape_string($_POST['title']);
$fileName = $_FILES['Photo']['name'];
$tmpName = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO images(title,image) VALUES ('".$title."','".$filePath."')";
mysql_query($query) or die (mysql_error());
}
?>
<form name="Image" enctype="multipart/form-data" action="image.php" method="POST">
<input type="text" name="title" id="title" value=""><br/><br/>
<input type="file" name="Photo" size="20" accept="image/gif, image/jpeg, image/x-ms-bmp, image/x-png"><br/>
<INPUT type="submit" class="button" name="Submit" value=" Submit ">
</form>