Ubuntu 的新手无法通过 xampp 将图像上传到网页目录,在 windows 上工作正常

New to Ubuntu cant upload images to directory for webpage through xampp worked fine on windows

嗨,我是 Linux Ubuntu 18.04 的新手。 我已经安装了 xampp 网络服务器。 我有 php 代码,可以在 windows 环境中使用 xampp 正常工作。但是,我现在已切换到 Linux,当我完成页面以将图像上传到目录时,看起来页面已完成处理,但目录本身是空的。任何人都可以帮助我尝试了其他论坛和视频提到的权限,但这没有任何区别。 我是 Linux 的新手所以请耐心等待我

感谢您的宝贵时间

库纳尔

按照所说编辑我的代码,我正在学习它可能有点麻烦 php 所有数据库调用工作正常。

<?php
     require_once $_SERVER['DOCUMENT_ROOT'].'/ECommerce/core/init.php';
include 'includes/head.php';
include 'includes/navigation.php';
$dbpath='';
if(isset($_GET['add'])||isset($_GET['edit'])){
  $brandQuery = $db->query("SELECT * FROM brand ORDER BY brand");
  $parentQuery = $db->query("SELECT * FROM catergories WHERE parent =0 ORDER BY category");
  $title =((isset($_POST['title'])&& $_POST['title'] !='')?sanitize($_POST['title']):'');
  $brand =((isset($_POST['brand']) && !empty($_POST['brand']))?sanitize($_POST['brand']):'');
  $parent =((isset($_POST['parent']) && !empty($_POST['parent']))?sanitize($_POST['parent']):'');
  $category =((isset($_POST['child'])) && !empty($_POST['child'])?sanitize($_POST['child']):'');
  $price =((isset($_POST['price'])&& $_POST['price'] !='')?sanitize($_POST['price']):'');
  $list_price =((isset($_POST['list_price'])&& $_POST['list_price'] !='')?sanitize($_POST['list_price']):'');
  $description =((isset($_POST['description'])&& $_POST['description'] !='')?sanitize($_POST['description']):'');
  $available =((isset($_POST['available'])&& $_POST['available'] !='')?sanitize($_POST['available']):'');
  $size =((isset($_POST['size'])&& $_POST['size'] !='')?sanitize($_POST['size']):'');
  $saved_image='';

if(isset($_GET['edit'])){
  $edit_id = (int)$_GET['edit'];
  $productResults= $db->query("SELECT * FROM products WHERE id ='$edit_id'");
  $product = mysqli_fetch_assoc($productResults);
  if (isset($_GET['delete_image'])){
    $image_url = $_SERVER['DOCUMENT_ROOT'].$product['image'];echo $image_url;
    unlink($image_url);
    $db->query("UPDATE products SET image=''WHERE id ='$edit_id'");
    header('Location: products.php?edit='.$edit_id);
  }
  $category = ((isset($_POST['child']) && $_POST['child']!= '')?sanitize($_POST['child']):$product['categories']);
  $title = ((isset($_POST['title']) && $_POST['title']!='')?sanitize($_POST['title']):$product['title']);
  $brand = ((isset($_POST['brand']) && $_POST['brand']!='')?sanitize($_POST['brand']):$product['brand']);
  $parentQ = $db->query("SELECT * FROM catergories WHERE id ='$category'");
  $parentResult= mysqli_fetch_assoc($parentQ);
  $parent = ((isset($_POST['parent']) && $_POST['parent'] !='')?sanitize($_POST['parent']):$parentResult['parent']);
  $price = ((isset($_POST['price']) && $_POST['price']!='')?sanitize($_POST['price']):$product['price']);
  $list_price = ((isset($_POST['list_price']) && $_POST['list_price']!='')?sanitize($_POST['list_price']):$product['list_price']);
  $description = ((isset($_POST['description']) && $_POST['description']!='')?sanitize($_POST['description']):$product['description']);
  $available = ((isset($_POST['available']) && $_POST['available']!='')?sanitize($_POST['available']):$product['Available']);
  $size = ((isset($_POST['size']) && $_POST['size']!='')?sanitize($_POST['size']):$product['size']);
  $saved_image=(($product['image'] !='')?$product['image']:'');
  $dbpath=$saved_image;
}
if($_POST){

  $categories =sanitize($_POST['child']);
  $price =sanitize($_POST['price']);
  $list_price =sanitize($_POST['list_price']);
  $size =sanitize($_POST['size']);
  $description =sanitize($_POST['description']);
  $errors = array();
  $size= sanitize($_POST['size']);
  $dbPath='';
  $required = array('title','price','parent','child');
  $available = sanitize($_POST['available']);

foreach ($required as $field) {
  if($_POST[$field]== ''){
    $errors[] ='All fields With an Asterisk are required.';
    break;
  }
}
if(!empty($_FILES)) {
  var_dump ($_FILES);
  $photo=$_FILES['photo'];
  $name=$photo['name'];
  $nameArray = explode('.',$name);
  $fileExt = $nameArray[1];
  $mime = explode ('/',$photo['type']);
  $mimeType=$mime[0];
  $mimeExt =$mime[1];
  $tmpLoc=$photo['tmp_name'];
  $fileSize=$photo['size'];
  $allowed= array('png','jpg','JPEG','GIF');
  $uploadName = md5(microtime()).'.'.$fileExt;
  $uploadPath= '/ECommerce/stock/'.$uploadName;
  $dbpath ='/ECommerce/stock/'.$uploadName;
  if($mimeType !='image'){
    $errors[]='The File must be an image';
  }
  if(!in_array($fileExt,$allowed)){
    $errors[]='The file extenstion must be a PNG, JPG,JPEG or GIF.';
  }
  if($fileSize > 15000000){
    $errors[]='The file size must be under 15MB.';
  }
  if ($fileExt != $mimeExt && ($mimeExt ==='jpeg' && $fileExt !='jpg')){
    $errors[]='The File extension does not match the file';
  }
}
if(!empty($errors)){
  echo display_errors($errors);
}else{
  //upload file and insert into database
  move_uploaded_file($tmpLoc,$uploadPath);
  $insertSQL="INSERT INTO  products (`title`,`price`,`list_price`,`brand`,`categories`,`size`,`image`,`description`,`Available`)
  VALUES('$title','$price','$list_price','$brand','$category','$size','$dbpath','$description','$available')";
if(isset($_GET['edit'])){
  $insertSQL="UPDATE products SET title ='$title', price = '$price', list_price = '$list_price',
  brand='$brand', categories ='$category', size='$size' , Available='$available',image='$dbpath',description='$description' WHERE id='$edit_id'";
}

$db->query($insertSQL);
header('Location: products.php');
}
}

?>
<h2 class="text-center"><?=((isset($_GET['edit']))?'Edit A ':'Add A New');?>Product</h2><hr>
<form action="products.php?<?=((isset($_GET['edit']))?'edit='.$edit_id:'add=1');?>" method="POST" ENCTYPE="multipart/form-data">
  <div class="form-group col-md-3">
<label for="title">Title*:</label>
<input type="text" name="title"class="form-control" id="title" value="<?=$title;?>">
  </div>
  <div class="form-group col-md-3">
    <label for="brand">Brand:</label>
    <select class="form-control" id="brand" name="brand">
      <option value=""<?=(($brand =='')?' selected':'');?>></option>
      <?php while($b=mysqli_fetch_assoc($brandQuery)): ?>
        <option value="<?=$b['id'];?>"<?=(($brand == $b['id'])?' selected':'');?>><?=$b['brand'];?></option>
      <?php endwhile;?>
    </select>
    </div>
    <div class="form-group col-md-3">
      <label for="parent">Parent Category*:</label>
      <select class="form-control" id="parent" name="parent">
        <option value=""<?=(($parent =='')?' selected':'');?>></option>
        <?php while($p= mysqli_fetch_assoc($parentQuery)): ?>
          <option value="<?=$p['id'];?>"<?=(($parent == $p['id'])?' selected':'');?>><?=$p['category'];?></option>
        <?php endwhile; ?>
      </select>
    </div>
    <div class="form-group col-md-3">
      <label for="child">Child Category*:</label>
      <select id="child" name="child" class="form-control">
      </select>
    </div>
    <div class="form-group col-md-3">
      <label for="price">Price*:</label>
      <input type="text" id="price" name="price" class="form-control" value="<?=$price;?>">
    </div>
    <div class="form-group col-md-3">
      <label for="price">List Price*:</label>
      <input type="text" id="list_price" name="list_price" class="form-control" value="<?=$list_price;?>">
    </div>
  <div class="form-group col-md-3">
    <label>Size*:</label>
    <input type="text" id="size" name="size" class="form-control" value="<?=$size;?>">
  </div>
  <div class="form-group col-md-3">
    <label>Available:</label>
    <input type="text" id="size" name="available" class="form-control" value="<?=$available;?>">
  </div>

<br>
  <div class="form-group col-md-6">
<?php if($saved_image !=''): ?>
  <div class="saved-image"><img src="<?=$saved_image;?>" alt="saved image"/><br>
    <a href = "products.php?delete_image=1&edit=<?=$edit_id;?>" class="text-danger"> Delete Image</a>
  </div>
<?php else: ?>
  <label for="photo">Product Photo:</label>
  <input type="file" name="photo" id="photo" class="form-control" accept="image/*" >
<?php endif;?>
</div>
<div class="form-group col-md-6">
  <label for="description">Description:</label>
  <textarea id="description" name="description" class="form-control" rows="6"><?=$description;?></textarea>
</div>
<div class="form-group pull-right">
<a href="products.php" class="btn btn-default">Cancel</a>
<input type="submit" value="<?=((isset($_GET['edit']))?'Edit  ':'Add ');?> Product" class="btn btn-success pull-left">

</div><div class ="clearfix"></div>
</form>

    </div>
      </div>

      </div>
    </div>
  </div>
</div>
<?php }else{
$sql = "SELECT * FROM products WHERE deleted = 0";
$presults =$db->query($sql);
if (isset($_GET['featured'])){
  $id = (int)$_GET['id'];
  $featured = (int)$_GET['featured'];
  $featuredsql = "UPDATE products SET featured ='$featured' WHERE id='$id'";
  $db->query($featuredsql);
  header('Location: products.php');
}
 ?>
<h2 class="text-center">Products </h2>
<a href="products.php?add=1" class="btn btn-success pull-right" id="add-product-btn">Add Product</a><div class="clearfix"></div>
<hr>
<table class="table table-bordered table-condensed table-striped">
  <thead><th></th><th>Product</th><th>Price</th><th>Category</th><th>Featured</th><th>Sold</th></thead>
  <tbody>
    <?php while($product = mysqli_fetch_assoc($presults)):
        $childID= $product['categories'];
        $catsql="SELECT* FROM catergories WHERE id = '$childID'";
        $result=$db->query($catsql);
        $child= mysqli_fetch_assoc($result);
        $parentID = $child['parent'];
        $psql="SELECT * FROM catergories WHERE id ='$parentID'";
        $presult=$db->query($psql);
        $parent= mysqli_fetch_assoc($presult);
        $category = $parent['category'].'-'.$child['category'];
      ?>
      <tr>
        <td>
          <a href="products.php?edit=<?=$product['id'];?>" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-pencil"></span> </a>
          <a href="products.php?delete=<?=$product['id'];?>" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-remove"></span> </a>
        </td>
        <td><?=$product['title'];?></td>
        <td><?=money($product['price']);?></td>
        <td><?=$category;?></td>
        <td><a href="products.php?featured=<?=(($product['featured']==0)?'1':'0')?>&id=<?=$product['id'];?>" class="btn btn-xs btn-default" >
          <span class="glyphicon glyphicon-<?=(($product['featured']==1)?'minus':'plus');?>"></span>
        </a>&nbsp <?=(($product['featured']== 1)?'Featured Product':'');?></td>

        <td>0</td>
      </tr>
    <?php endwhile; ?>
  </tbody>
</table>
 <?php
} include 'includes/footer.php';?>
<script>
jQuery('document').ready(function(){
get_child_options('<?=$category;?>');
});
</script>

 ?>
  • Linux 区分大小写,您是否正确指定了路径以匹配文件系统上文件夹的大小写?
  • 该文件夹是否在您的 webroot 中 - 如果不是,您可能必须编辑 apache 配置以设置 apache 访问该文件夹的权限(不仅仅是文件夹权限)
  • 您查看 error.log 以了解 PHP 的错误输出是什么吗? Windows 通常会显示错误,默认情况下 Linux 服务器可能不会显示错误,因此您会丢失错误输出。

简要查看您的代码,虽然不知道文件名、文件夹名称等其他任何信息 - 您检查文件扩展名但没有将所有文件(输入扩展名和要比较的数组)小写以确保区分大小写不是问题这里。例如 file.JPG 和 file.jpeg 将不匹配您的数组。

其次,您不检查 move_uploaded_file 的结果,这可能有助于确定此时是否成功。如前所述,请检查错误日志。

补充一下,此代码充满安全漏洞 - 适合开始学习,但我不会在任何地方将其投入生产。

钱恩

$uploadPath= '/ECommerce/stock/'.$uploadName;

$uploadPath=$_SERVER['DOCUMENT_ROOT'] . "/ECommerce/stock/".$uploadName;