dsq30861 2013-11-26 20:34
浏览 37

通过PHP上传多个文件并将信息发布到MYSQL数据库

I am trying to upload multiple files to the server, while inputting upload information into a database. For some reason only the first or last file is being put into the DB but I can not for the life of me figure out why!

Any help appreciated - thanks!

public function processNewUploads()
{
    $language = OW::getLanguage();
    $osuploadBOL = OSUPLOAD_BOL_Service::getInstance();

    //Loop through each file that was uploaded  
    for($i=0; $i < count($_FILES['uploads']['tmp_name']); $i++){


        /* check if there is a file in the array
for($i=0; $i < count($_FILES['uploads']['tmp_name']); $i++){            
        if(!is_uploaded_file($_FILES['uploads']['tmp_name'][$i]))
        {
            OW::getFeedback()->error($language->text('osupload','no_files_selected'));
            OW::getApplication()->redirect(OW::getRouter()->urlForRoute('base_index'));
        } */


        //Check to see if there was an error
        if($_FILES['uploads']['error'][$i] > 0){
            $error = $_FILES['uploads']['error'][$i];
        }else{
            $error = 0;
        }

        //Prepare information to enter into database//

        //If the user is logged in then get the userId
        if(OW::getUser()->isAuthenticated()) {
            $fileOwner = OW::getUser()->getId();
        } else {
            $fileOwner = 0;
        }

        //Get the IP of the uploader
        $fileOwnerIp = $_SERVER['REMOTE_ADDR'];

        //Get the raw file name
        $rawFileName = $_FILES['uploads']['name'][$i];

        //Get the unique file name
        $uniqueFileName = uniqid('',true);

        //Get the upload time
        $uploadTimeStamp = time();

        //Get the file extension
        $fileExtension = explode(".", $_FILES['uploads']['name'][$i]);
        $n = count($fileExtension) - 1;
        $fileExtension = '.'.strtolower($fileExtension[$n]);

        //Get the size of the file
        $fileSize = $_FILES['uploads']['size'][$i];

        //Get the display name of the file
        $fileDisplayName = $rawFileName;

        //Insert the file information into the database
        $sql = 'INSERT INTO ' . OSUPLOAD_BOL_OsuploadDao::getInstance()->getTableName() . " (fileOwner,fileOwnerIp,rawFileName,uniqueFileName,uploadTimeStamp,fileExtension,fileSize,fileDisplayName,error)
        VALUES ('$fileOwner','$fileOwnerIp','$rawFileName','$uniqueFileName.$fileExtension','$uploadTimeStamp','$fileExtension','$fileSize','$fileDisplayName','$error')";

        OW::getDbo()->Insert($sql);

        if(!$error > 0){
            //Move the new upload as long as there was not an error with it
            $fileToMove = $_FILES['uploads']['tmp_name'][$i];
            $osuploadBOL->moveNewUpload($fileToMove,$uniqueFileName,$fileExtension);
            continue;
        }else{
            //If there was an error just go to the next file
            continue;
        }
    }
}

}

HTML:

<input name="uploads[]" id="uploads" type="file" multiple="">
  • 写回答

1条回答 默认 最新

  • dszm02606009 2013-11-26 21:39
    关注

    This is just a quick knock-up of yours, there may be errors.

    Use a foreach instead I think:

    public function processNewUploads()
    {
        $language = OW::getLanguage();
        $osuploadBOL = OSUPLOAD_BOL_Service::getInstance();
    
        //Loop through each file that was uploaded
        foreach($_FILES as $key => $file){
    
            $error = $file['error'] ? 
            /*
            if($file['error']){
                $error = $file['error'];
            }else{
                $error = 0;
            }
            */
    
            //If the user is logged in then get the userId
            if(OW::getUser()->isAuthenticated()) {
                $fileOwner = OW::getUser()->getId();
            } else {
                $fileOwner = 0;
            }
    
            //Get the IP of the uploader
            $fileOwnerIp = $_SERVER['REMOTE_ADDR'];
    
            //Get the raw file name
            $rawFileName = $file['name'];
    
            //Get the unique file name
            $uniqueFileName = uniqid('',true);
    
            //Get the upload time
            $uploadTimeStamp = time();
    
            //Get the file extension
            $fileExtension = explode(".", $file['name']);
            $n = count($fileExtension) - 1;
            $fileExtension = '.'.strtolower($fileExtension[$n]);
    
            //Get the size of the file
            $fileSize = $file['size'];
    
            //Get the display name of the file
            $fileDisplayName = $rawFileName;
    
            //Insert the file information into the database
            $sql = 'INSERT INTO ' . OSUPLOAD_BOL_OsuploadDao::getInstance()->getTableName() . " (fileOwner,fileOwnerIp,rawFileName,uniqueFileName,uploadTimeStamp,fileExtension,fileSize,fileDisplayName,error)
            VALUES ('$fileOwner','$fileOwnerIp','$rawFileName','$uniqueFileName.$fileExtension','$uploadTimeStamp','$fileExtension','$fileSize','$fileDisplayName','$error')";
    
            OW::getDbo()->Insert($sql);
    
            if(!$error > 0){
                //Move the new upload as long as there was not an error with it
                $fileToMove = $file['tmp_name'];
                $osuploadBOL->moveNewUpload($fileToMove,$uniqueFileName,$fileExtension);
                continue;
            }else{
                //If there was an error just go to the next file
                continue;
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)