将文件路径上载到数据库

时间:2017-09-25 10:59:00

标签: php mysql

我正在尝试从一个输入上传两个文件,这两个文件都应该出现在uploads文件夹中,但只有一个文件路径进入数据库。

E.g。如果我上传test1.pdf和test2.pdf,它们都将进入uploads文件夹,但test1.pdf将插入数据库的两列而不是一列。

在我疯了之前,有人可以帮助我吗?

import UIKit
import UIKit
import Alamofire
import AlamofireNetworkActivityIndicator
import SwiftLocation
import CoreLocation
import AMScrollingNavbar

class NewHomeFeedControllerViewController: UIViewController {
    let detailView = EventDetailViewController()
    var allEvents = [Event]()
    let customCellIdentifier1 = "customCellIdentifier1"
    var grideLayout = GridLayout(numberOfColumns: 2)
    let refreshControl = UIRefreshControl()
    var newHomeFeed: NewHomeFeedControllerViewController?
      let paginationHelper = PaginationHelper<Event>(serviceMethod: PostService.showEvent)
    lazy var dropDownLauncer : DropDownLauncher = {
        let launcer = DropDownLauncher()
        launcer.newHomeFeed = self
        return launcer
    }()

    // 1 IGListKit uses IGListCollectionView, which is a subclass of UICollectionView, which patches some functionality and prevents others.
    let collectionView: UICollectionView = {
        // 2 This starts with a zero-sized rect since the view isn’t created yet. It uses the UICollectionViewFlowLayout just as the ClassicFeedViewController did.
        let view = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())
        // 3 The background color is set to white
        view.backgroundColor = UIColor.white
        return view
    }()
    func handleDropDownMenu(){
        dropDownLauncer.showDropDown()
    }
    func configureCollectionView() {
        // add pull to refresh
        refreshControl.addTarget(self, action: #selector(reloadHomeFeed), for: .valueChanged)
        collectionView.addSubview(refreshControl)
    }
    func reloadHomeFeed() {
        self.paginationHelper.reloadData(completion: { [unowned self] (events) in
            self.allEvents = events

            if self.refreshControl.isRefreshing {
                self.refreshControl.endRefreshing()
            }

            DispatchQueue.main.async {
                self.collectionView.reloadData()
            }
        })
    }

    func categoryFetch(dropDown: DropDown){
        navigationItem.title = dropDown.name
        paginationHelper.category = dropDown.name
        configureCollectionView()
        reloadHomeFeed()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(collectionView)
        collectionView.contentInset = UIEdgeInsetsMake(15, 0, 0, 0)
        navigationItem.title = "Home"
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.collectionViewLayout = grideLayout
        collectionView.reloadData()
        collectionView.register(CustomCell.self, forCellWithReuseIdentifier: customCellIdentifier1)
        //  self.navigationItem.hidesBackButton = true
        let backButton = UIBarButtonItem(image: UIImage(named: "menu"), style: .plain, target: self, action: #selector(handleDropDownMenu))
        self.navigationItem.leftBarButtonItem = backButton
        configureCollectionView()
        reloadHomeFeed()
        // Do any additional setup after loading the view.
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if let navigationController = self.navigationController as? ScrollingNavigationController {
            navigationController.followScrollView(self.collectionView, delay: 50.0)
        }
    }
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)

        if let navigationController = navigationController as? ScrollingNavigationController {
            navigationController.stopFollowingScrollView()
        }
    }

     func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
        if let navigationController = navigationController as? ScrollingNavigationController {
            navigationController.showNavbar(animated: true)
        }
        return true
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        grideLayout.invalidateLayout()
    }
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}


extension NewHomeFeedControllerViewController: UICollectionViewDataSource {
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return allEvents.count
    }
    // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let customCell = collectionView.dequeueReusableCell(withReuseIdentifier: customCellIdentifier1, for: indexPath) as! CustomCell
        let imageURL = URL(string: allEvents[indexPath.item].currentEventImage)
        print(imageURL ?? "")
        customCell.sampleImage.af_setImage(withURL: imageURL!)
        return customCell
    }
}


extension NewHomeFeedControllerViewController: UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.item == 0 || indexPath.item == 1 {
            return CGSize(width: view.frame.width, height: grideLayout.itemSize.height)
        }else{
            return grideLayout.itemSize
        }
    }
}

2 个答案:

答案 0 :(得分:0)

if (count($_FILES) >= 1) {


//Loop through each file
foreach ($_FILES as $id=>$file) {
    //Get the temp file path
    $tmpFilePath = $file['tmp_name'];

    //Make sure we have a filepath
    if ($tmpFilePath != "") {

        //save the filename
        $shortname = $file['name'];

        //save the url and the file
        $filePath = "uploads/" . date('d-m-Y-H-i-s') . '-' . $file['name'];

        //Upload the file into the temp dir
        if (move_uploaded_file($tmpFilePath, $filePath)) {


        $sql = "INSERT INTO " . $config_tbl_prefix . " subcontractor_qs (field1) VALUES ('$filePath')";

        mysql_query($sql);

         }
       }
     }
   }
 }

答案 1 :(得分:0)

如果您想使用相同的输入字段上传两个或更多文件,请更新您的代码。在此代码中保存文件名和路径。在一次你无法使用循环获得两个文件路径。

<强> 1。重要的是删除mysql并使用pdo或mysqli

 <if (count($_FILES['enviroupload']['name']) >= 1) {
//Loop through each file
for ($i = 0; $i < count($_FILES['enviroupload']['name']); $i++) {
    //Get the temp file path
    $tmpFilePath = $_FILES['enviroupload']['tmp_name'][$i];

    //Make sure we have a filepath
    if ($tmpFilePath != "") {

        //save the filename
        $shortname = $_FILES['enviroupload']['name'][$i];

        //save the url and the file
        $filePath = "uploads/" . date('d-m-Y-H-i-s') . '-' . $_FILES['enviroupload']['name'][$i];

        //Upload the file into the temp dir
        if (move_uploaded_file($tmpFilePath, $filePath)) {

            $sql = "INSERT INTO " . $config_tbl_prefix . " subcontractor_qs         (field1, field2)

 VALUES
 ('$shortname','$filePath')";

            //mysql_query($sql);
            echo $sql;
            echo "<br>";

        }
    }
}

}

在此代码文件中,路径和文件名将保存在db中,如果只想保存文件路径则需要更新代码