传递参数函数不起作用

时间:2015-05-30 15:03:01

标签: bash shell scripting

public function img_normal_upload($id,$folder,$operation){

            $path = realpath(APPPATH . '../public/resources/photos/' . $folder );
            //var_dump($_SERVER['DOCUMENT_ROOT'] . '\\ssmis\\public\\resources\\photos\\' . $folder . ' \\');
            echo $path;
            //checks if there is an image to be uploaded. If none, use default image.   
            if(!empty($_FILES) && $_FILES["userfile"]["name"] != "" && isset($_FILES["userfile"]["name"])){

                // Note we dont have to need $path be cause we do it already on $config
                $config = array (
                    'image_library' => 'gd',
                    'upload_path' =>  $_SERVER['DOCUMENT_ROOT'] . '\\ssmis\\public\\resources\\photos\\' . $folder . ' \\',
                    'file_name' =>  $id . '.jpg',
                    'allowed_types' => 'png|jpg|jpeg',
                    'overwrite' => TRUE,
                    'max_size' => '2000',   
                );
                $this->_CI->upload->initialize($config);
                //kapag failed
                if(!$this->_CI->upload->do_upload()){
                    echo 'Error creating image.';
                    echo $this->_CI->upload->display_errors();
                }else{
                    //now, the $path will become our resource path for copying and creating thumbnails.
                    return true;
                }
            } else {
                if($operation === 'add'){
                    // Ang value ng APPPATH is application/ , this is not the root folder.
                    // Pwede ko siguro palitan ng $_SERVER['DOCUMENT_ROOT'] or dirname with str_replace
                    $resource_path = realpath(APPPATH . '../public/resources/default_picture.jpg');
                    //we have to append '/' kasi hindi niya nilalagay sa realpath ito
                    copy($resource_path, $path . '/' . $id . '.jpg');
                }                   
            }

            $this->_CI->image_lib->clear();
        }

在上面的示例中, vaild()功能不起作用,为什么会这样?当传递有效参数时,它会检查文件名并打印,但如果没有传递,则会打印“exists”。

2 个答案:

答案 0 :(得分:1)

在将字符串作为参数时应检查null字符串,而不是将其与0进行比较。还需要将参数传递给有效函数。

valid()
{
 if [ -z "$1" ]; then
        echo "Pass a file name as argument"
        exit 1
 fi
}

valid $1

if [ -f "$1" ]; then
        echo "$1 exists"
else
        echo "$1 doesnt exist"
fi

答案 1 :(得分:1)

而不是if [[ "$1" = "0" ]]; then,您只需将其更改为:

if [[ "$1" = "" ]]; then

它将按预期工作。

您还需要在[ ]语法中双引号变量。 [[ ]]语法可以处理空格而不需要任何双引号,但[ ]不能。