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

时间:2013-11-26 20:34:05

标签: php mysql

我正在尝试将多个文件上传到服务器,同时将上传信息输入到数据库中。出于某种原因,只有第一个或最后一个文件被放入数据库,但我不能为我的生活找出原因!

感谢任何帮助 - 谢谢!

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 个答案:

答案 0 :(得分:0)

这只是你的快速敲门,可能有错误。

我认为使用foreach代替:

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;
        }
    }
}