Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未定义的偏移量:cakephp中的1_Php_Html_Cakephp - Fatal编程技术网

未定义的偏移量:cakephp中的1

未定义的偏移量:cakephp中的1,php,html,cakephp,Php,Html,Cakephp,您好,我一直在探索如何在实现站点中使用框架cakephp。在我当前的PHP脚本中出现了这个错误:未定义的偏移量:1,它已经让我头疼了。你们能帮我找出我做错了什么吗 我的代码在这里: function file_upload($file_array, $id){ if(count($file_array)) { $location = "files/library-files/"; foreach ($file_array as $file) {

您好,我一直在探索如何在实现站点中使用框架cakephp。在我当前的PHP脚本中出现了这个错误:未定义的偏移量:1,它已经让我头疼了。你们能帮我找出我做错了什么吗

我的代码在这里:

function file_upload($file_array, $id){
    if(count($file_array))
    {
        $location = "files/library-files/";

        foreach ($file_array as $file) {
            $ext = explode(".", $file['name']);
            $new_name = md5(uniqid());
            $target_path = $location . $new_name .".".$ext[1];
            if(move_uploaded_file($file['tmp_name'], $target_path))
            {
                $this->LibraryFiles->create();
                $this->LibraryFiles->set('id', $new_name);
                $this->LibraryFiles->set('library_id', $id);
                $this->LibraryFiles->set('name', $file['name']);
                $this->LibraryFiles->set('ext', $ext[1]);
                $this->LibraryFiles->set('type', $file['type']);
                $this->LibraryFiles->set('size', $file['size']);
                if($this->LibraryFiles->save())
                {

                }
                else
                {

                }
            }
        }
    }
}

您可以添加以下检查:

if(isset($ext[1]))
{
    $target_path = $location . $new_name .".".$ext[1];
}
else if($file['name'] !== '.' && $file['name'] !== '..')
{
    $target_path = $location . $new_name;
}
else
{
    continue;
}

我刚刚替换了$target_path=$location$新名称“..”$ext[1]


现在我很高兴:D

您的文件数组中是否有一个内部没有点的文件,或者指向上一个目录的指针?这是$target_path=$location这一行$新名称“..”$ext[1];这给了我错误。经过数小时的研究,我将其替换为if(isset($target_path))$target_path=$location$新名称“..”$ext[1];else$target_path='';现在不再有错误了:D
if (isset($target_path)) 
$target_path = $location . $new_name .".".$ext[1]; 
else 
$target_path = '';