如果文件名存在,如何避免上传

时间:2015-11-05 20:31:55

标签: php file-upload filepath

首先,我想使用自动重命名文件,但我找不到重命名后获取文件路径名的解决方案。那么,是否可以在我的代码中添加一些东西以检查文件是否存在然后它不允许上传文件?

这是我的代码:

:forward(input)

我的问题是如果文件存在,它将被替换为现有文件,我可以找到一种方法来使用自动重命名文件,如下所示:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    //sets up cell

    let cell : EventCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! EventCell

    //adds attend action

    cell.attendButton.addTarget(self, action: "buttonTapped:", forControlEvents: UIControlEvents.TouchUpInside)


    //queries parse for events 

    let event = events?[indexPath.row]

    event?.eventImage.getDataInBackgroundWithBlock({ (data, error) -> Void in

        if let data = data, image = UIImage(data: data) {

            cell.eventBackgroundImage.image = image
            cell.eventTitleLabel.text = event?.eventTitle

            //gets profile picture of events creator 

            if let eventCreator = event?.objectForKey("user") as? PFUser {
                if let creatorImage = eventCreator.objectForKey("profilePicture") as? PFFile {
                        creatorImage.getDataInBackgroundWithBlock({ (data, error) -> Void in

                        cell.creatorImageView.image = UIImage(data: data!)

                    })
                }
            }



            //gets profile pictures for image view array on back of cell

            if let attendeeArray = event?.objectForKey("attendees") as? [PFUser] {

                for var index = 0; index < attendeeArray.count; ++index {
                    let profileImageView = cell.imageViewArray[index]
                    let usr : PFUser = (attendeeArray[index] as PFUser?)!

                    //                        if let picture = usr.objectForKey("profilePicture") as? PFFile {

                    usr.fetchIfNeededInBackgroundWithBlock({ (object: PFObject?, error: NSError?) -> Void in
                        if let picture = object!.objectForKey("profilePicture") as? PFFile {
                            picture.getDataInBackgroundWithBlock({ (data, error) -> Void in
                                profileImageView.image = UIImage(data: data!)
                            })
                        }
                    })


                }

            }


            //sets correct category for cell image

            if event?.category == "" {
                cell.categoryImageView.image = nil
            }

            if event?.category == "The Arts" {
                cell.categoryImageView.image = UIImage(named: "Comedy")
            }

            if event?.category == "The Outdoors" {
                cell.categoryImageView.image = UIImage(named: "Landscape")
            }

            if event?.category == "Other" {
                cell.categoryImageView.image = UIImage(named: "Dice")
            }

            if event?.category == "Sports" {
                cell.categoryImageView.image = UIImage(named: "Exercise")
            }

            if event?.category == "Academics" {
                cell.categoryImageView.image = UIImage(named: "University")
            }

            if event?.category == "Science" {
                cell.categoryImageView.image = UIImage(named: "Physics")
            }

            if event?.category == "Entertainment" {
                cell.categoryImageView.image = UIImage(named: "Bowling")
            }

            if event?.category == "Food & Drinks" {
                cell.categoryImageView.image = UIImage(named: "Food")
            }

            if let date = event?.eventDate {
                let dateFormatter = NSDateFormatter()
                dateFormatter.dateFormat = "yyyy-MM-dd"

                cell.eventDescriptionLabel.text = event?.eventDescription
                cell.eventDateLabel.text = dateFormatter.stringFromDate(date)
            }
        }
    })

    cell.layer.cornerRadius = 20

    //not sure if necessary

//        let subviews : NSArray = cell.contentView.subviews
//        
//        for view in subviews {
//            view.removeFromSuperview()
//        }

    return cell

}

但重命名为sql表后,我找不到发布文件名的解决方案。它将以实名发布而不是重命名文件。

所以我最后的选择是在我的代码中添加一些内容以检查文件是否存在然后避免上传文件(如果存在)。

你能帮我解决这个问题吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

使用您的代码建议,可能是这样的:

在此行下:$ destinationFile = $ destinationDir。 &#39; /&#39; 。 $ destinationName;

把这个:

theme