du12197 2019-06-28 01:08
浏览 87

Php使用if else语句从另一个文件调用变量

I used this upload.php file to validate/store the upload image. Then the image will convert into base64 to store into database. In my save.php file, I include "upload.php"; and call the $hex_string and pass to a global variable $NewHexString. But seem in my save.php it return a null value.

p/s : from console upload.php I can see $hex_string return a value.

upload.php

$file = $_FILES['fileUpload'];  
$fileName = $_FILES['fileUpload']['name'];
$fileTmpName = $_FILES['fileUpload']['tmp_name'];  //directory location 
$fileSize = $_FILES['fileUpload']['size'];
$fileError = $_FILES['fileUpload']['error'];       //default 0 | 1 got error

print_r ($file);

$fileExt = explode('.', $fileName);           //split file name to get ext.
$fileActualExt = strtolower(end($fileExt));     //change to lowercase for the extension file
$allowed = array('jpg','jpeg','png');   

//$hex_string = 90707;   
// if I hardcode the value here it able pass this value to database. 
// But when I put inside if..else statement, it will return null.

if(in_array($fileActualExt, $allowed)){
  if($fileError === 0){
    if($fileSize < 500000){

      $fileDestination = './uploads/'.$fileName;                    
      move_uploaded_file($fileTmpName , $fileDestination); 

      $data = file_get_contents($fileTmpName);
      $hex_string = base64_encode($data);

      echo $hex_string;
      // print_r ($hex_string); 

    }else{
      echo "You files is too big!";
    }   
  }else{
    echo "Error occur when upload file!"; 
  }
}else{
  echo "You cannot upload files of this type!";
}

save.php that I use to call $hex_string and pass to global variable.

<?
$propertyID = "1";
include($_SERVER['DOCUMENT_ROOT'] . '/PDOfile.php'); 
$ehorsObj = new TM();
$ehorsObj->TM_CONNECT($propertyID);

include "upload.php";
$NewHexString = $hex_string;   //<-- return a null value??

$method = $_POST['method'];
$method();

/** EDIT **/
function editPropertyMasterData() {
    global $ehorsObj;   
    global $NewHexString;
    
    $propertyID     = (isset($_POST['propertyID']) ? $_POST['propertyID'] : '');
    $propertyName   = (isset($_POST['propertyName']) ? $_POST['propertyName'] : '');
    $propertyColor   = (isset($_POST['propertyColor']) ? $_POST['propertyColor'] : '');
    $businessRegistrationNo   = (isset($_POST['businessRegistrationNo']) ? $_POST['businessRegistrationNo'] : '');
    $noOfRooms   = (isset($_POST['noOfRooms']) ? $_POST['noOfRooms'] : '');
    $active         = (isset($_POST['active']) ? $_POST['active'] : '');

    $sqlUpdate = " UPDATE tblProperty
                      SET propertyName = '" . $propertyName . "',
                      propertyLogo = '" . $NewHexString . "',
                      propertyColor = '" . $propertyColor . "',
                      businessRegistrationNo = '" . $businessRegistrationNo . "',
                      noOfRooms = '" . $noOfRooms . "',
                      active = '" . $active . "'
                   WHERE propertyID = '" . $propertyID . "' ";
    $ehorsObj->ExecuteData($sqlUpdate, $ehorsObj->DEFAULT_PDO_CONNECTIONS);
}

?>

</div>
  • 写回答

1条回答 默认 最新

  • dplbf4340 2019-06-28 06:43
    关注

    you could return an array of data from the upload file:

    if (!in_array($fileActualExt, $allowed)) {
        return ['error' => 'You cannot upload files of this type!'];
    }
    
    if ($fileError !== 0) {
        return ['error' => 'Error occur when upload file!'];
    }
    
    if ($fileSize > 500000) {
        return ['error' => 'Your file size is too big!'];
    }
    
    $fileDestination = './uploads/' . $fileName;                    
    move_uploaded_file($fileTmpName, $fileDestination); 
    
    $data = file_get_contents($fileTmpName);
    
    return ['hexString' => base64_encode($data)];
    

    Now, in your save.php file you check the payload like:

    $propertyID = "1";
    
    include($_SERVER['DOCUMENT_ROOT'] . '/PDOfile.php'); 
    
    $ehorsObj = new TM();
    $ehorsObj->TM_CONNECT($propertyID);
    
    $uploadPayload = require "upload.php";
    
    if (isset($uploadPayload['error'])) {
        echo $uploadPayload['error']);
        // do something in case of error
    }
    
    $method = $_POST['method'];
    $method($ehorsObj, $uploadPayload['hexString']);
    
    /** EDIT **/
    function editPropertyMasterData($ehorsObj, $NewHexString)
    {
        $propertyID     = (isset($_POST['propertyID']) ? $_POST['propertyID'] : '');
        $propertyName   = (isset($_POST['propertyName']) ? $_POST['propertyName'] : '');
        $propertyColor   = (isset($_POST['propertyColor']) ? $_POST['propertyColor'] : '');
        $businessRegistrationNo   = (isset($_POST['businessRegistrationNo']) ? $_POST['businessRegistrationNo'] : '');
        $noOfRooms   = (isset($_POST['noOfRooms']) ? $_POST['noOfRooms'] : '');
        $active         = (isset($_POST['active']) ? $_POST['active'] : '');
    
        $sqlUpdate = " UPDATE tblProperty
                          SET propertyName = '" . $propertyName . "',
                          propertyLogo = '" . $NewHexString . "',
                          propertyColor = '" . $propertyColor . "',
                          businessRegistrationNo = '" . $businessRegistrationNo . "',
                          noOfRooms = '" . $noOfRooms . "',
                          active = '" . $active . "'
                       WHERE propertyID = '" . $propertyID . "' ";
        $ehorsObj->ExecuteData($sqlUpdate, $ehorsObj->DEFAULT_PDO_CONNECTIONS);
    }
    

    Things you should consider:

    1. Avoid using global variables
    2. Use prepared statements to avoid SQL injections
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向