我有此代码:
controller:
public function uploadFiles()
{
if (isset($_FILES['file']) && count($_FILES['file']['name']) > 0) {
$acceptFormat = array(
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpg',
'png' => 'image/png'
);
$folder = "system";
$destinationFilePath = "../" . $this->_config->upload_catalog_name . "" . $folder;
$nazwy = array();
for ($i = 0; $i <= count($_FILES['file']['name']) + 1; $i++) {
$newFileName = $this->uploadFile1($_FILES['file'][$i], $destinationFilePath, true, "FILE_SYSTEM_CONFIG", 0, $acceptFormat);
}
}
}
function:
private function uploadFile1(array $filesArray, string $destinationFilePath, bool $fileValidation = true, string $fileType, int $category = 0, array $acceptFormat):string
{
for ($i = 0; $i <= count($filesArray['name']) + 1; $i++) {
try {
if (
!isset($filesArray['error'][$i]) ||
is_array($filesArray['error'][$i])
) {
throw new RuntimeException('Invalid parameters.');
}
switch ($filesArray['error'][$i]) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
default:
throw new RuntimeException('Unknown errors.');
}
if ($filesArray['size'][$i] > 1000000000) {
throw new RuntimeException('Exceeded filesize limit.');
}
$ext = strtolower(pathinfo($filesArray['name'][$i], PATHINFO_EXTENSION));
if (!in_array($ext, array_keys($acceptFormat)) && $fileValidation == true) {
throw new RuntimeException('Invalid file format.');
}
$mime = mime_content_type($filesArray['tmp_name'][$i]);
if (!in_array($mime, array_values($acceptFormat)) && $fileValidation == true) {
throw new RuntimeException('Invalid mime format.');
}
require_once $this->_config->function_path . "RandomFileName.php";
$pathTmpName = pathinfo($filesArray['name'][$i]);
$fileExtension = strtolower($pathTmpName['extension']);
$randomName = randomFileName(60);
$newFileName = $randomName . "." . $fileExtension;
if (!file_exists($destinationFilePath)) {
mkdir($destinationFilePath, 0777);
}
if (!file_exists($destinationFilePath . "/thumbs")) {
mkdir($destinationFilePath . "/thumbs", 0777);
}
if (!move_uploaded_file($filesArray['tmp_name'][$i], $destinationFilePath . "/" . $newFileName)) {
throw new RuntimeException('Failed to move uploaded file.');
} else {
return $newFileName;
}
} catch (RuntimeException $e) {
echo $e->getMessage();
return "";
}
}
}
我需要一个通用功能来将文件发送到php中的服务器。可以是1个文件-一次可以是10个文件。
此刻,尽管发送了5个文件,但我还是向我发送了1个文件。
由于此功能,我想在服务器上获得一个新的文件名。 为此,我打了个电话:
for ($ i = 0; $ i <= count ($ _ FILES ['file'] ['name']) + 1; $ i ++) {
$ newFileName = $ this-> uploadFile1 ($ _ FILES ['file'] [$ i], $ destinationFilePath, true, "FILE_SYSTEM_CONFIG", 0, $ acceptFormat);
}
有人知道如何解决吗?
更新
我的传入数据:
Array
(
[name] => Array
(
[0] => X+T+rcffQvCG67MuqR49Vg.jpg
[1] => X1pHOaBFRtGyXrA0FoDBVg.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
)
[tmp_name] => Array
(
[0] => /Applications/XAMPP/xamppfiles/temp/php2fuLup
[1] => /Applications/XAMPP/xamppfiles/temp/phpZIoKbH
)
[error] => Array
(
[0] => 0
[1] => 0
)
[size] => Array
(
[0] => 1965954
[1] => 1557849
)
)
答案 0 :(得分:0)
使用php函数上传多个文件
resid = house['price'] - y_fit
shrink = -.2
arrow_dim = (20, 20)
for i in range(len(house)):
plt.arrow(house.loc[i, 'sqf'], y_fit[i], 0, resid[i] + resid[i]*shrink ,
head_width=arrow_dim[0], head_length=arrow_dim[1], fc='r', ec='r')
答案 1 :(得分:0)
它不起作用:(
现在我有了以下代码:
global.fetch =
我在DEBUG文件中添加了一条记录。
在文件newfile.txt中,我有:
jest.stub
在文件newfile1.txt中,我具有:
$acceptFormat = array(
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpg',
'png' => 'image/png'
);
$folder = "system";
$destinationFilePath = "../" . $this->_config->upload_catalog_name . "" . $folder;
$resultArray = $this->uploadFiles123($_FILES, $destinationFilePath, true, "FILE_SYSTEM_CONFIG", 0, $acceptFormat);
public function uploadFiles123(array $filesArray, string $destinationFilePath, bool $fileValidation = true, string $fileType, int $category = 0, array $acceptFormat)
{
$result = array();
foreach ($filesArray as $file)
{
$temp = array();
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
if (!in_array($ext, array_keys($acceptFormat)) && $fileValidation == true) {
$temp['newFileName'] = $file['name'];
$temp['error'] = 'Invalid file format.';
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "filename: ".$file['name']." \n";
fwrite($myfile, $txt);
$txt = file_put_contents('newfile1.txt', print_r($file, true));
fwrite($myfile, $txt);
fclose($myfile);
// return error if invalid format with original file name and continue upload next file
}else if ($file['size'] > 10000000000)
{
$temp['newFileName'] = $file['name'];
$temp['error'] = 'Exceeded filesize limit.';
// return error if Exceeded filesize limit with original file name and continue upload next file
}
else if(is_uploaded_file($file['tmp_name'])){
require_once $this->_config->function_path . "RandomFileName.php";
$randomName = randomFileName(60);
$newFileName = $randomName . "." . $ext;
if (!file_exists($destinationFilePath)) {
mkdir($destinationFilePath, 0777);
}
if(move_uploaded_file($newFileName,$destinationFilePath))
{
$temp['newFileName']=$newFileName;
$temp['error'] = '';
//upload file successfully and return new file name;
}else{
$temp['newFileName'] = $file['name'];
$temp['error'] = 'Failed to move uploaded file.';
}
}
$result[] = $temp;
}
return $result;
}