简体   繁体   English

如何使用管理页面将照片添加到PHP博客文章

[英]how to add photos to PHP blog postings using admin page

I managed to create a simple blog and an admin page using PHP. 我设法使用PHP创建了一个简单的博客和一个管理页面。 I didn't use a CMS like wordpress because I wanted a deeper understanding of PHP. 我没有使用像wordpress这样的CMS,因为我想更深入地了解PHP。

I wanted to create an admin page that allowed the user to specify where in their blog posting they could put in a picture; 我想创建一个管理页面,允许用户指定他们可以在博客发布中放置图片的位置; for example, they can add a photo before the first paragraph or after. 例如,他们可以在第一段之前或之后添加照片。

My admin page as it is now is just a textarea input field and a submit button that submits text-only blog posts from the textarea field to the database. 我的管理页面现在只是一个textarea输入字段和一个Submit按钮,用于将纯文本博客文章从textarea字段提交到数据库。 My thought process was that I could add img tags into the textarea input field, but the user's photos might be stored on their hard drive so I thought of the idea to store PHP code in the textarea input field to retrieve image files from the database. 我的想法是可以将img标签添加到textarea输入字段中,但是用户的照片可能存储在他们的硬盘上,因此我想到了将PHP代码存储在textarea输入字段中以从数据库检索图像文件的想法。 For example, if I wanted to place a photo after the first paragraph of the blog post, I would type something like this into the textarea field: 例如,如果我想在博客文章的第一段之后放置照片,则可以在textarea字段中输入如下内容:

This is the first paragraph. Here is a photo of my vacation:
<? echo "<img src='getPhoto.php?page=blogPost'>"; ?>

Then I click the "submit button" and the paragraph with the PHP code gets submitted into my database. 然后,我单击“提交按钮”,然后将包含PHP代码的段落提交到我的数据库中。 The page with the blog posting retrieves the text, "This is the first paragraph. Here is a photo of my vacation:" and the PHP code. 带有博客文章的页面检索文本“这是第一段。这是我度假的照片:”和PHP代码。 The PHP code then retrieves and shows the photo. 然后,PHP代码检索并显示照片。

The problem is that submitting PHP code with the blog text might be confusing for a user who is not tech-savvy or does not have any programming knowledge. 问题在于,对于不懂技术或没有任何编程知识的用户,使用博客文本提交PHP代码可能会造成混淆。 The admin page should make it possible for the blog writer to place photos anywhere in their blog post without programming knowledge. 管理员页面应使博客作者无需编程知识即可将照片放置在其博客文章中的任何位置。 I haven't found a tutorial yet that covers image placement in a PHP blog's admin page. 我还没有找到涵盖PHP博客管理页面中图像放置的教程。 Any help getting started would be appreciated. 任何帮助入门将不胜感激。

One way to accomplish what you want is by implementing custom shortcodes. 实现您想要的一种方法是实现自定义的简码。 In you case it'd look something like this: 在这种情况下,它看起来像这样:

This is the first paragraph. Here is a photo of my vacation:

[image bahamas-00123]

How it works is that you do something like this: 它是如何工作的,您可以执行以下操作:

$text = shortcodes($text);

You'd need a separate area for uploading photos and some way of keeping track of them (ie: sql, flatfiles) so you know that bahamas-00123 actually refers to /some/path/DSC-00123.JPG . 您需要一个单独的区域来上载照片,并需要某种方式来跟踪它们(例如sql,flatfiles),因此您知道bahamas-00123实际上是指/some/path/DSC-00123.JPG

For some code please check the accepted answer of: How do I replace custom “tags” in a string? 对于某些代码,请检查以下可接受的答案: 如何替换字符串中的自定义“标签”?

You can use a text editor like ckeditor or tinymce to format the text and upload images. 您可以使用文本编辑器(如ckeditor或tinymce)来格式化文本并上传图像。 This way users need not be aware about the custom tags and they can place the image wherever they like. 这样,用户无需了解自定义标签,便可以将图像放置在所需的任何位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM