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

时间:2019-02-15 10:16:01

标签: php permissions upload xampp directory

嗨,我是Linux Ubuntu 18.04的新手。 我已经安装了xampp Web服务器。 我有php代码,可以在xampp的Windows环境下正常工作。但是,现在我已切换到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>

 ?>

2 个答案:

答案 0 :(得分:-1)

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

简短地看一下代码,尽管不知道文件名,文件夹名之类的其他内容-您检查文件扩展名,但未全部小写(输入扩展名和要比较的数组)以确保区分大小写在这里不是问题。例如file.JPG和file.jpeg将与您的数组不匹配。

第二,您不必检查move_uploaded_file的结果,这可能有助于确保它在这一点上是否成功。如前所述,请检查错误日志。

只需添加一下,此代码就充满了安全漏洞-很好地开始了学习,但我不会在任何地方将其投入生产。

答案 1 :(得分:-1)

Chane

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

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