返回

PHP-小注释部分中的可选上载图像

发布时间:2022-04-12 05:45:23 326
# edge# 后端

你好,我是一名编程初学者。我正在尝试使用和学习PHP。

我从学校里吸取经验和教训,并尝试去适应它。

 500000) {
       echo "Sorry, your file is too large.";
       $uploadOk = 0;
   }

   // Allow certain file formats
   if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
   && $imageFileType != "gif" ) {
       echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
       $uploadOk = 0;
   }

   // Check if $uploadOk is set to 0 by an error
   if ($uploadOk == 0) {
       echo "Sorry, your file was not uploaded.";
   // if everything is ok, try to upload file
   } else {
       if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
               // **** THIS IS CODE IVE ADDED ****  
               $mail = $_POST['mail'];
               $comment = $_POST['comment'];
                $file = basename( $_FILES["fileToUpload"]["name"]);
               $now = date('Y-m-d H:i:s.');
 
               $sql_add = 'INSERT INTO tablecomments (time,mail,comment,file) VALUES (?,?,?,?)';
               $stmt_add = $pdo->prepare($sql_add);
               $stmt_add->execute(array($now,$mail,$comment,$file));
               // **** THIS IS CODE IVE ADDED **** 
               echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
           } else {
               echo "Sorry, there was an error uploading your file.";
       }
   }
// ---------- w3schools example ends here --------------

} else {
   
   if(isset($_POST["submit"])) {
       $mail = $_POST['mail'];
       $comment = $_POST['comment'];
       $file = 0;
       $now = date('Y-m-d H:i:s.');

       $sql_add = 'INSERT INTO tablecomments (time,mail,comment,file) VALUES (?,?,?,?)';
       $stmt_add = $pdo->prepare($sql_add);
       $stmt_add->execute(array($now,$mail,$comment,$file));   
   }

}

?>

我已经标记了我添加的部分和W3学校的部分,所以基本上这是我想做的一小部分。你可以发邮件、发表评论,如果你喜欢,还可以上传图片。

图像进入文件夹。而数据库则是;邮件、评论、文件名;。如果你不上传文件;邮件、评论和0“;已发布。

它能工作,但看起来很不对劲,你如何让它更干净?我发布了两次相同的部分,正确的方法是什么???

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像