2015-10-08 68 views
-1

我已阅读另一个话题,但我不能申请对方的回答给我的代码:SPHP得到变量的函数的另一个作用

我有这个功能

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; 
} 

    } 

然后如果功能是succesfull我想这样做:

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

     echo $cfile; 
} 

有人可以帮助我知道我在做什么错?我已经阅读过类似的主题,但在我的情况下我无法使用它们。

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

+0

这些在同一个文件中? –

+0

你是说'if(add_image($ cFile))'中的'$ cFile'不存在吗?那么,我们不能告诉你为什么,因为我们在那条线之前没有看到任何东西。 – deceze

+0

是同一个文件!说undefined ...我想将函数中的变量传递给其他操作 –

回答

0

你的函数看起来像它会终止整个脚本如果失败,所以唯一的办法就可以到达底部代码是功能是否成功。

您可能不需要if声明。

的一种方法,可能会更好,是return false;如果函数失败,然后检查if (add_image($cFile) !== false)

+0

问题是我无法在查询中获得变量cFile –

0

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

+0

该函数是succesfull,我无法将变量cFile传递给查询 –

+0

if (add_image($ cFile))用以下代码替换此代码: $ cFile = add_image($ cFile); (isset($ cFile)||!empty($ cFile)){ // } } –

相关问题