0

我对php很陌生。但是当我学会编程时,它是在 TI-83 计算器上的。对于 TI83,它们是可以使用的 if-then 语句。我正在编写一些代码来检查一个目录是否已经创建,如果没有创建它。无论哪种方式,我都需要它来运行代码的后半部分。这是代码。

<?php
$dir = "/Applications/XAMPP/xamppfiles/htdocs/upload/$_POST[Itemid]/" ;
if (!file_exists($dir) and !is_dir($dir)) {
    $upload_dir = mkdir($dir, 0777);
}else{
foreach ($_FILES["file"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
    $filename = $_FILES["file"]["name"][$key];
    $filetemp = $_FILES["file"]["tmp_name"][$key];
$filetype = $_FILES["file"]["type"][$key];
$filesize = $_FILES["file"]["size"][$key];
   if (file_exists($dir . $filename))
  {
  echo $filename . " already exists. <br><br>";
  }
else
  {
  echo "Upload: " . $filename . "<br>";
  echo "Type: " . $filetype . "<br>";
  echo "Size: " . ($filesize / 1024) . " kB<br>";
  echo "Temporarily Stored in: " . $filetemp . "<br>";
  move_uploaded_file($filetemp,"$dir/$filename");
  echo "uploaded the file \"" . $filename. "\" to the \"/Applications/XAMPP/xamppfiles/htdocs/upload/\" Directory<br>" ;
  $con=mysqli_connect("localhost","root","","Inventory");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="INSERT INTO ListPics (`1`, `Item_ItemID`)
VALUES
('$dir$filename', '$_POST[Itemid]')";


if (!mysqli_query($con,$sql))
 {
die('Error: ' . mysqli_error($con));
}
echo ($filename. " added to Database<br><br>");

mysqli_close($con);
  }
  }
  }
  }
?>

是的,我知道这段代码不安全。它永远不会看到互联网(恭敬地低头并说“谢谢你,戈尔”)它会紧贴在 VPN 后面,仅供个人使用。我只需要知道如何执行 PHP 等效的 If Then 语句

4

1 回答 1

0

这很简单:

if(condition) {
    /* do something if condition is true */
}
else {
    /* do something else if condition is false */
}
/* do other stuff either way */

当条件为假时,您才需要else是否有您想做的事情。如果您想以任何一种方式进行操作,则根本不要使用。else

于 2013-12-16T02:08:17.010 回答