使用一个IF中的变量 - Statement到另一个IF - PHP中的语句

时间:2015-05-21 09:08:52

标签: php html ajax

我在login.php中的IF - Statement中有一个变量$ target。我根据这个变量创建了文件夹和子文件夹。现在我想将上传的文件移动到此位置。我怎么能这样做?

这是代码

$upload = "E:/demons";

if(isset($_POST['userid'], $_POST['pid']))
         {
         $userid = trim($_POST["userid"]);
         $pid = trim($_POST["pid"]);

         $sql = "SELECT * FROM template WHERE uname = '$userid' and pword = '$pid'";
         $result = mysqli_query($conn,$sql);
         $row = mysqli_fetch_array($result);  



        echo "公司".'<br/>';
        echo $row['client'].'<br/>'.'<br/>';
        echo "第".'<br/>';
        echo '<a href="upload.html"/>'.$row['day1'].'</a>'.'<br/>';


        $target = $upload.'/'.$row['week'].'/'.$row['day1'].'/'.$row['client'].'/'.$row['brand'].'/'.$row['sc'].'/';


        $imagename = $row['week'].'.'.$row['day1'].'.'.$row['client'].'.'.$row['brand'].'.'.$row['sc'].'.'.'jpg';


                if(!file_exists($target))
                    {
                        mkdir($target,null,true);
                    }
        }

else if(isset($_FILES['image']))
        {
        $image = basename($_FILES["image"]["name"]);
        echo $image;
        //$target4 = $upload.'/'.$row['week'].'/'.$row['day1'].'/'.$row['client'].'/'.$row['brand'].'/'.$row['sc'].'/';
        move_uploaded_file($_FILES['image']['tmp_name'], $target);
        }
        else
            {
                echo "asdfg";
         } 

Userid和Pid来自login.html,而Image值来自upload.html

2 个答案:

答案 0 :(得分:0)

将else-if作为if在第一个if中嵌套。否则就不会被执行。

$upload = "E:/demons";

if(isset($_POST['userid'], $_POST['pid']))
{
  $userid = trim($_POST["userid"]);
  $pid = trim($_POST["pid"]);

  $sql = "SELECT * FROM template WHERE uname = '$userid' and pword = '$pid'";
  $result = mysqli_query($conn,$sql);
  $row = mysqli_fetch_array($result);

  echo "公司".'<br/>';
  echo $row['client'].'<br/>'.'<br/>';
  echo "第".'<br/>';
  echo '<a href="upload.html"/>'.$row['day1'].'</a>'.'<br/>';

  $target = $upload.'/'.$row['week'].'/'.$row['day1'].'/'.$row['client'].'/'.$row['brand'].'/'.$row['sc'].'/';

  $imagename = $row['week'].'.'.$row['day1'].'.'.$row['client'].'.'.$row['brand'].'.'.$row['sc'].'.'.'jpg';

  if(!file_exists($target))
  {
    mkdir($target,null,true);
  }
  if(isset($_FILES['image']))
  {
    $image = basename($_FILES["image"]["name"]);
    echo $image;
    move_uploaded_file($_FILES['image']['tmp_name'], $target);
  }
}

答案 1 :(得分:0)

$upload = "Your desired location";


//comes from the login.html page
if(isset($_POST['userid'],$_POST['pid']))
{
  $userid = trim($_POST["userid"]);
  $pid = trim($_POST["pid"]);

  $sql = "SELECT * FROM template WHERE uname = '$userid' and pword = '$pid'";
  $result = mysqli_query($conn,$sql);
  $row = mysqli_fetch_array($result);

  echo "Whatever coloumn you wish to echo from database".'<br/>';
  echo $row['col1'].'<br/>'.'<br/>';
  echo "Second Coloumn".'<br/>';
  echo '<a href="upload.html"/>'.$row['col2'].'</a>'.'<br/>';
//create the folders and subfolders based on the data from the database
$target = $upload.'/'.$row['col1'].'/'.$row['col2'].'/'.$row['col3'].'/'.$row['col4'].'/'.$row['col5'].'/';

//for renaming the image.
$imagename = $row['col1'].'.'.$row['col2'].'.'.$row['col3'].'.'.$row['col4'].'.'.$row['col5'].'.'.'jpg';
//create the folders and subfolders
 if(!file_exists($target))
   {
     mkdir($target,null,0777);
   }
//start session and store the value of $target and $imagename in a variable 
session_start();
$_SESSION['str'] = $target;
$_SESSION['img'] = $imagename;

//This comes from other HTML page but to the same PHP.
if(isset($_FILES['image']))

// image upload from upload.html 
// Want the value of target here.
  {

      session_start();
      $_SESSION['str'];
      $_SESSION['img'];
      $image = basename($_FILES["image"]["name"]);

//Move the uploaded file to the desired location.
 move_uploaded_file($_FILES['image']['tmp_name'], $_SESSION['str'].$_SESSION['img']);

echo "Upload Successful";

希望这对所有人都有帮助。