PHP在另一个动作的函数中获取变量

时间:2015-10-08 11:33:31

标签: php

我已阅读其他主题但我无法将其他答案应用于我的代码:S

我有这个功能

function add_image($cFile) {

    //SET SETTINGS FOR THE IMAGE
    $target_dir = "../images/cuisines/";
    $target_file = $target_dir . basename($_FILES["cImage"]["name"]);
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    $cFile = basename($_FILES["cImage"]["name"], '.'. $imageFileType).uniqid($prefix = '-', $more_entropy = null) .'.'. $imageFileType;

    //BUT FIRST CHECK IF THERE IS NOT FILE
    if ($_FILES['cImage']['size'] == 0) {       
        echo "<script>alert('You forgot the image, you must complete the form again');</script>" ;
        echo '<script>window.location="'. $_SERVER['HTTP_REFERER'] .'"</script>' ;
        exit();
    } 

    // CHECK IF THE FILE IS AN IMAGE
    $check = getimagesize($_FILES["cImage"]["tmp_name"]);
    if($check == false) {
        echo "<script>alert('This is not an Image, you must complete the form again');</script>" ;
        echo '<script>window.location="'. $_SERVER['HTTP_REFERER'] .'"</script>' ;
        exit();
    }

    //ALLOW CERTAIN KIND OF FILES
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
        echo "<script>alert('Sorry, only JPG, JPEG, PNG & GIF files are allowed, you must complete the form again');</script>" ;
        echo '<script>window.location="'. $_SERVER['HTTP_REFERER'] .'"</script>' ;
        exit();
    }

    if(move_uploaded_file($_FILES["cImage"]["tmp_name"], $target_dir.$cFile)) { 
        return $cFile;
        } else {
return false;
}

    }

然后如果功能成功,我想这样做:

if (add_image($cFile) !== false) {

        echo $cfile;
}

有人能帮助我知道我做错了什么吗?我已经阅读了类似的主题,但在我的案例中我无法使用它们。

我收到的错误是关于代码第二部分中的变量cFile不存在:S

1 个答案:

答案 0 :(得分:0)

可能是$ target_dir,可能没有写访问权限。如果没有使用chmod命令为target_dir添加写权限。