我已阅读另一个话题,但我不能申请对方的回答给我的代码: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
这些在同一个文件中? –
你是说'if(add_image($ cFile))'中的'$ cFile'不存在吗?那么,我们不能告诉你为什么,因为我们在那条线之前没有看到任何东西。 – deceze
是同一个文件!说undefined ...我想将函数中的变量传递给其他操作 –