Deleting attached files when its post deleted - php

Imagine there is writing article application, The article would have some pictures, files, etc. Of course, when the article removed, attached files should be removed too. But How can I do that?
There is write form, in
/news/write. When I uploads some picture, the picture will send to the server in background, (by ajax post). and this picture will have some name, like a0bb880c5a06b5b9dc238f84cfda6861, it will be appended in the editor <img src="/uploads/a0bb880c5a06b5b9dc238f84cfda6861">.
I don't finish the article, but the picture in the server. Finally I submit the post, and there is picture with in it, And later few hours, I want to delete it. No problem with delete article itselt, but the picutre remains in the server. I want to delete it too.
I think some ways, The conclusion is, I should bind article and its picture.
Mapping files and article - But how can I do that? the article not exist, so I don't know article's id.
When article saved, parsing content and pick up <img> tag's src attribute, and connect it. Should I do like this? Is there any easier way?
Or delete files periodly ...?

You need to make a relation with your article and image files to be able to delete the image file when article is deleted.
You can also parse through the content of the articles and find images and delete them but I think that will be bad way to do it.
Instead try this.
When you save articles you can save images files in a field of the articles document.
db.articles.insert( { title: "article title", body: 'article body', images: [{imageFileName: 'xyz.jpg'}, {imageFileName: 'xyz.png'}...] } );
Now when you delete article you will have images field that you can iterate through and delete the image files as well.

Related

WordPress: Can you add an Excerpt on a PDF

Is it possible to add an Excerpt on a PDF document. Whenever I use the search function on my website I can see the PDFs results, but the text below the link says "No excerpt available". I tried adding text into the Description and Caption fields, but it did not work.
I tried going through the media library > clicking the file > Edit more details then when the file document opens I clicked on the Share Options at the top of the page, but there is no Excerpt option available.
I can see the Excerpt option in Pages and Posts, but not on PDFs.
Is there a way to add Excerpts to PDF documents?
Thank you.
I do not know of any plugins nor native way to do it but I had a similar problem on one of my last projects and here are my solutions.
Either... Creating a child post dedicated to that pdf and copying the content, outputting the child post content onto the parent post.
Or... Simply adding a description to the uploaded media (here the pdf) and outputting the description onto the post.
I went for the second one for dev time restriction, but the first one was also pretty cool because we could add long format additional information linked to the pdf.
Hope that will help!

Add custom class to images in wordpress uploader

I have a site that has a script pulling images uploaded to a post and displaying it on their respective posts with the masonry script.
The issue is that I would like to have different widths on some "key images". The only thing that I thought of that could work is if there's a way to add a class to images being uploaded (before inserted).
For example under URL, Title, Caption etc.
Those classes could be radioboxes with small, medium or large.
Right now the only way to style images are after they're actually inserted in the post and I don't want to do that since they are never inserted and I don't want to mess up the text.
Does anyone have an idea?
Here's a link to a post for clarification.
http://www.lokalxxxarkitekter.com/web/projekt/broar-infrastruktur/kaptensbron/
Regards, Marcus

How to make an image selector to choose an image from the server to use in HTML form

I have built a blog system where each entry has an associated image file. I have created a php upload for this system and can get everything onto the server. When I write each blog post I have a row in my database which stores the filename of the image, so I could, if I knew the filename already, just type it in.
What I want to do is on my blog post form, instead of being able to type the file name, I want a button to click on which opens a window with all my images in it, then I can click on one and have that filename sent back to the form.
I suppose I could use AJAX to do this but it seems over complicated. Does anyone know of a good way to achieve this.

Blog posts and the common layer

I am using Joomla to design a website, very simple, there is a page on my site that is to store all news headlines (each is href) and each href leads to a full article, I would like to post an image in all of the articles, where can I put my image into in joomla folders so that right after each article title, the image will be shown ?
Thank you
To insert image in each article you have to follow this steps:
Administration->go to Article manager->CLick on article(view the article detail).
2.Now you can see the edition where you putted your article content.now on same editor there is an option of image.select it and choose image from the folder in which you putted the images.
now apply or save the article.
hence you will add image to each article in joomla.
You can put the images into any folder you want. All you have to do is go to each article in the backend and manually insert an image by typing in the path. Easier thing to do would be to create a folder in the "images" folder so that the insert path isn't too long.
As fr inserting, I'm not sure if you wanted all the images to be inserted automatically, but I am pretty sure this isn't possible.

Keep track of user uploads within articles

just can't get my head around this.
I just finished my own WYSIWYG editor. It allows my users to upload images to embed in their article they are writing.
But how can i keep track of those user uploads ? I want them to be attached to the article, so that when i remove an article all images will also be removed. (keep my disk clean)
My first bet was to add an hidden input field to the article form for every uploaded image containing the image name, then on submit move all attached images from the tmp dir to the article image dir.
But this way a user can attach 100 times a picture to an article, remove them 99 times from the WYSIWYG editor and use only 1. Now i would have to save all the unused images. This seems like waste to me.
Any best practices on this ?
Don't worry about the edge case. Nobody is going to upload then deassociate 99 images.
You have to move the received files out of the tmp dir as soon as the (ajax?) upload sends them over. You cannot wait till the article submission, because the /tmp files would be gone by then.
Try to save your images with enumerated filenames like ArticleName.1.jpeg. And let your WYSIWIG editor inject uniqure placeholder tokens like {{img1}} on which you can see which images are factuall still in the article. (You can also try to deduce the file ids out from <img src="upl/articlename.1.jpeg"... since your WYSIWIG editor generates HTML.)
Anyway, on upload compare the list of enumerated {{img123}} tokens with the existing list of files.123.jpeg. Remove the unused.
Alternative: Do not remove unused images. Sell it as feature, since you now have more article history. And I guess the filesizes are negligible for most web sites.

Categories