在CakePHP中,如何从图像数组中获取客户机文件名?
我正在尝试将图像上载到阵列中。我希望数组存储图像的名称。但是,每次使用在CakePHP中,如何从图像数组中获取客户机文件名?,php,cakephp,cakephp-4.x,Php,Cakephp,Cakephp 4.x,我正在尝试将图像上载到阵列中。我希望数组存储图像的名称。但是,每次使用getClientFilename()方法获取图像的名称时,都会出现一个错误。我得到的错误是: 对数组上的成员函数getClientFilename()的调用 这个错误是因为$otherName变量使用了getClientFilename()方法。如何从图像数组中获取客户端文件名?如果您查看图像示例,我想获取vashion.jpg的clientfilename值 SubmissionsController.php if(!is
getClientFilename()
方法获取图像的名称时,都会出现一个错误。我得到的错误是:
对数组上的成员函数getClientFilename()的调用
这个错误是因为$otherName
变量使用了getClientFilename()
方法。如何从图像数组中获取客户端文件名?如果您查看图像示例,我想获取vashion.jpg
的clientfilename值
SubmissionsController.php
if(!is_dir(WWW_ROOT.'otherImg'.DS.$folder)) {
mkdir(WWW_ROOT.'otherImg'.DS.$folder, 0775);
}
foreach($this->request->getData('data') as $otherImage) {
debug($otherImage);
$otherName = $otherImage->getClientFilename();
//Add to data to save
$imgData = array(
"original_pathname" => $otherImage,
"submission_id" => $submission->id
);
$this->Submission->Image->create();
$this->Submission->Image->save($imgData);
if(!$this->Submission->Image->save($imgData)) {
$this->Flash->error(__('The other images could not be uploaded.'));
}
}
<table id="uploads_table">
<thead>
<tr>
<th colspan="2">Upload Other Images (max of 25)<input type="hidden" name="data[Image][num_images]" id="num_images" value="1" /></th>
</tr>
</thead>
<tfoot></tfoot>
<tbody>
<tr>
<td><input type="file" name="data[Image][file][]" id="ImageFile1" /></td>
<td><input type="button" id="add_image" name="add_image" value="Add Another Image" /></td>
</tr>
</tbody>
</table>
add.php
if(!is_dir(WWW_ROOT.'otherImg'.DS.$folder)) {
mkdir(WWW_ROOT.'otherImg'.DS.$folder, 0775);
}
foreach($this->request->getData('data') as $otherImage) {
debug($otherImage);
$otherName = $otherImage->getClientFilename();
//Add to data to save
$imgData = array(
"original_pathname" => $otherImage,
"submission_id" => $submission->id
);
$this->Submission->Image->create();
$this->Submission->Image->save($imgData);
if(!$this->Submission->Image->save($imgData)) {
$this->Flash->error(__('The other images could not be uploaded.'));
}
}
<table id="uploads_table">
<thead>
<tr>
<th colspan="2">Upload Other Images (max of 25)<input type="hidden" name="data[Image][num_images]" id="num_images" value="1" /></th>
</tr>
</thead>
<tfoot></tfoot>
<tbody>
<tr>
<td><input type="file" name="data[Image][file][]" id="ImageFile1" /></td>
<td><input type="button" id="add_image" name="add_image" value="Add Another Image" /></td>
</tr>
</tbody>
</table>
上载其他图像(最多25个)
此图显示foreach循环的调试输出。我正在尝试返回clientfilename
我能够找到解决我的代码问题的方法:
foreach($this->request->getData('data') as $otherImage) {
for($key = 0; $key < $otherImage['num_images']; $key++) {
$otherName = $otherImage['file'][$key]->getClientFilename(); //Get file original name
//Add to data to save
$imgData = array(
"original_pathname" => $folder.'/'.$otherName,
"submission_id" => $submission->id
);
if($otherName) {
$otherImage['file'][$key]->moveTo(WWW_ROOT.'otherImg'.DS.$folder.DS.$otherName); // move files to destination folder
}
}
}
foreach($this->request->getData('data')作为$otherImage){
对于($key=0;$key<$otherImage['num_images'];$key++){
$otherName=$otherImage['file'][$key]->getClientFilename();//获取文件原始名称
//添加到要保存的数据
$imgData=数组(
“原始路径名”=>$folder.'/'。$otherName,
“提交id”=>$submission->id
);
如果($otherName){
$otherImage['file'][$key]->moveTo(WWW_ROOT.otherImg.DS.$folder.DS.$otherName);//将文件移动到目标文件夹
}
}
}
这将返回
vashion.jpg
检查此文档。您也可以看到本教程。您的调试输出确认了$otherImage
中的内容确实是一个数组,正如错误消息所告诉您的那样。所讨论的对象位于$otherImage->file
@SatuSultana,我以前看过那个YouTube视频。当我需要上传一张图片时,它非常有用。然而,它并没有覆盖上传一系列图像。我也在看你链接的文档网站,但我还是被卡住了。我可以用一些例子或比文档上更好的解释。谢谢。@GreySchmidt,debug($otherImage->file[0]->getClientFilename());给我一个通知,上面写着“试图获取非对象的属性”文件。关于如何访问clientfilename有什么想法吗?谢谢