我如何删除PHP中的图像?

时间:2016-05-11 22:50:15

标签: php ajax

我试图删除ajax php文件中的图像。我有以下代码:

    // Author: Giovanni Costagliola <giovanni.costagliola@gmail.com>

    using System;
    using System.Collections.Generic;
    using System.Linq;

    namespace Utils
    {
    /// <summary>
    /// Represent a Weighted Item.
    /// </summary>
    public interface IWeighted
    {
        /// <summary>
        /// A positive weight. It's up to the implementer ensure this requirement
        /// </summary>
        int Weight { get; }
    }

    /// <summary>
    /// Pick up an element reflecting its weight.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class RandomWeightedPicker<T> where T:IWeighted
    {
        private readonly IEnumerable<T> items;
        private readonly int totalWeight;
        private Random random = new Random();

        /// <summary>
        /// Initiliaze the structure. O(1) or O(n) depending by the options, default O(n).
        /// </summary>
        /// <param name="items">The items</param>
        /// <param name="checkWeights">If <c>true</c> will check that the weights are positive. O(N)</param>
        /// <param name="shallowCopy">If <c>true</c> will copy the original collection structure (not the items). Keep in mind that items lifecycle is impacted.</param>
        public RandomWeightedPicker(IEnumerable<T> items, bool checkWeights = true, bool shallowCopy = true)
        {
            if (items == null) throw new ArgumentNullException("items");
            if (!items.Any()) throw new ArgumentException("items cannot be empty");
            if (shallowCopy)
                this.items = new List<T>(items);
            else
                this.items = items;
            if (checkWeights && this.items.Any(i => i.Weight <= 0))
            {
                throw new ArgumentException("There exists some items with a non positive weight");
            }
            totalWeight = this.items.Sum(i => i.Weight);
        }
        /// <summary>
        /// Pick a random item based on its chance. O(n)
        /// </summary>
        /// <param name="defaultValue">The value returned in case the element has not been found</param>
        /// <returns></returns>
        public T PickAnItem()
        {
            int rnd = random.Next(totalWeight);
            return items.First(i => (rnd -= i.Weight) < 0);
        }

        /// <summary>
        /// Resets the internal random generator. O(1)
        /// </summary>
        /// <param name="seed"></param>
        public void ResetRandomGenerator(int? seed)
        {
            random = seed.HasValue ? new Random(seed.Value) : new Random();
        }
    }
}
在这种情况下,

路径是例如图像。 1.JPG。它确实删除了表格行,但它并没有删除uploads文件夹中的图像。希望有人可以帮助我。先谢谢你的efford。

1 个答案:

答案 0 :(得分:2)

您可能需要重新检查代码。你有一个未公开的单一报价。请参阅下面的代码......

        define('IS_AJAX', true);

        $id     = $db->real_escape_string($_GET['photo_id']);
        $files  = $db->query("SELECT * FROM uploaded_photos WHERE id=".$id);
        $files  = $files->fetch_object();

        $file   = $files->path;

        // NOTE THAT if($file) WOULD ALMOST ALWAYS RETURN TRUE SO LONG AS IT CONTAINS ANY STRING
        // HOWEVER file_exists WILL CHECK IF THE FILE PHYSICALLY EXIST ON THE PATH/LOCATION OR NOT.
        // PREFER file_exists IN THIS SCENARIO INSTEAD.
        if(file_exists("../uploads/" . $file)){
            // CHANGE THE FILE PERMISSION ON THE IMAGE SO THAT YOU CAN WORK WITH IT...
            chmod("../uploads/" . $file, 0755);   // <== GIVES YOU PERMISSION TO DEAL WITH THE FILE...
            unlink("../uploads/".$file);
            // BY THE WAY;  HERE IS A PROBLEM... YOU HAVE AN UNCLOSED SINGLE QUOTE AFTER THE EQUAL SIGN.
            // I REALLY WONDER HOW THE TABLE ROW DELETES WITH THE TYPO...
            // TRY REMOVING OR COMPLETING IT THOUGH:        
            //$db->query("DELETE FROM uploaded_photos WHERE id='".$id);   <=== SEE THAT SINGLE QUOTE AFTER "=" THERE? THAT'S YOUR ENEMY.
            $db->query("DELETE FROM uploaded_photos WHERE id=".$id);
        }

更新:没有评论

    <?php

        define('IS_AJAX', true);

        $id     = $db->real_escape_string($_GET['photo_id']);
        $files  = $db->query("SELECT * FROM uploaded_photos WHERE id='{$id}'" );
        $files  = $files->fetch_object();       
        $file   = $files->path;

        if(file_exists("../uploads/" . $file)){
            chmod("../uploads/" . $file, 0777); 
            unlink("../uploads/".$file);    
            $db->query("DELETE FROM uploaded_photos WHERE id='{$id}'");
        }