分割数组
我正在尝试从数组中分割图像名称。
$this->_filenames[]= array($name);
$files = explode(",", $name);
当我对 $files
执行 var_dump
时,它会返回存储在数组中的两个图像,如下所示:
array
0 => string 'felton300.jpg' (length=13)
1
array
0 => string 'felton400.jpg' (length=13)
1
如果我尝试将每个元素(使用下面的代码)存储在数组中使用 list()
我收到错误。
list($img1,$img2) = $files;
错误是:未定义的偏移量:1
我正在从表单上传图像,并希望使用变量(即 $img1)作为插入到 mysql 数据库的引用。这是我上传图像的代码。
public function move($overwrite = false) {
$field = current($this->_uploaded);
if(is_array($field['name'])){
foreach($field['name'] as $number => $filename){
//处理多重上传
$this->_renamed = false;
$this->processFile($filename,$field['error'][$number],
$field['size'][$number],
$field['type'][$number],$field['tmp_name'][$number],
$overwrite);
}
} else {
$this->processFile($field['name'],$field['error'],
$field['size'],$field['type'],$field['tmp_name'],$overwrite);
}
}
protected function processFile($filename,$error,$size, $type,$tmp_name,$overwrite){
$OK = $this->checkError($filename,$error);
if ($OK) {
$sizeOK = $this->checkSize($filename, $size);
$typeOK = $this->checkType($filename, $type);
if ($sizeOK && $typeOK) {
$name = $this->checkName($filename, $overwrite);
$success = move_uploaded_file($tmp_name, $this->_destination . $name);
if ($success) {
//将修改后的文件名添加到文件名数组中
$this->_filenames[]= array($name);
//拆分数组***这是我想要拆分数组的位置,以便可以将图像存储在 MYSQL 表中。该表将有 3 列 id、image1、image2 ******
$files = explode(",", $name);
var_dump($files);
I'm trying to split the image names from an array.
$this->_filenames[]= array($name);
$files = explode(",", $name);
When I do a var_dump
on $files
it returns both images stored in the array as shown below:
array
0 => string 'felton300.jpg' (length=13)
1
array
0 => string 'felton400.jpg' (length=13)
1
if I try to store each element (using the code below) in the array using list()
I get an error.
list($img1,$img2) = $files;
The error is: Undefined offset: 1
I'm uploading the images from a form and want to use the variable (ie $img1) as a reference to insert into a mysql database. Here's my code for uploading the image.
public function move($overwrite = false) {
$field = current($this->_uploaded);
if(is_array($field['name'])){
foreach($field['name'] as $number => $filename){
//process the multiple upload
$this->_renamed = false;
$this->processFile($filename,$field['error'][$number],
$field['size'][$number],
$field['type'][$number],$field['tmp_name'][$number],
$overwrite);
}
} else {
$this->processFile($field['name'],$field['error'],
$field['size'],$field['type'],$field['tmp_name'],$overwrite);
}
}
protected function processFile($filename,$error,$size, $type,$tmp_name,$overwrite){
$OK = $this->checkError($filename,$error);
if ($OK) {
$sizeOK = $this->checkSize($filename, $size);
$typeOK = $this->checkType($filename, $type);
if ($sizeOK && $typeOK) {
$name = $this->checkName($filename, $overwrite);
$success = move_uploaded_file($tmp_name, $this->_destination . $name);
if ($success) {
//add the amended filename to the array of filenames
$this->_filenames[]= array($name);
//split the array***THIS IS WHERE I WANT TO SPLIT THE ARRAY SO I CAN STORE THE IMAGES IN A MYSQL TABLE. THE TABLE WILL HAVE 3 COLUMNS id, image1, image2 ******
$files = explode(",", $name);
var_dump($files);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果数组中有两个图像,为什么不
麻烦地拆分它们呢?
从这个看起来
你有一个多维数组(注意每个都有一个 0 的键)。很难判断您提供的内容,但
也可能是您的解决方案。
If you have two images in an array, why not
Why bother splitting them?
And from the looks of this
You have a multidimensional array here (notice each has a key of 0). It's hard to tell with what you've provided, but
May also be a solution for you.