gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:10:03 25 4
gpt4 key购买 nike

我正在尝试将多个文件上传到服务器,同时将上传信息输入数据库。由于某种原因,只有第一个或最后一个文件被放入数据库中,但我终究无法弄清楚为什么!

感谢任何帮助 - 谢谢!

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="">

最佳答案

这只是您的快速拼凑,可能存在错误。

我认为使用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;
}
}
}

关于php - 通过PHP上传多个文件并将信息发布到MYSQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20227405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com