上传的图像移动到文件夹 - PHP

时间:2017-05-27 11:43:28

标签: php image

我尝试了一些代码将上传的图像保存到文件夹中。文件夹的路径为C:\xampp\htdocs\gripOffers\Store_Brand\store_admin\images。我无法将其保存在文件夹中。这该怎么做?我在哪里做错了?

代码

if(isset($_POST['submit'])) {
    $image = $_FILES['file']['name'];

    $temp_name = $_FILES["file"]["tmp_name"];

    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $type = pathinfo($image, PATHINFO_EXTENSION);
    echo "<br>";
    echo $type;
    echo "<br>";

    if ((($type == "image/gif")
            || ($type == "jpeg")
            || ($type == "jpg")
            || ($type == "pjpeg")
            || ($type == "x-png")
            || ($type == "png"))
        && ($_FILES["file"]["size"] < 50000)
    ) {
        $m = move_uploaded_file($_FILES["file"]["tmp_name"], "/Store_Brand/store_admin/images/" . $image);
        if ($m) {
            echo "uploaded!";
        } else {
            echo "no";
        }
    } else {
        echo "file type is not image";
    }
}

2 个答案:

答案 0 :(得分:1)

请勿忘记在表单标记中指定enctype="multipart/form-data"

$_SERVER['DOCUMENT_ROOT']返回由&#39; DocumentRoot&#39;定义的项目的根目录。指令

<form method="post" enctype="multipart/form-data">
     <input type="file" name="file" value="">
     <input type="submit" name="submit" value="submit">
 </form>

PHP代码

<?php 
if(isset($_POST['submit'])) {
    $image = $_FILES['file']['name'];

    $temp_name = $_FILES["file"]["tmp_name"];

    $file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));
    $file_size =$_FILES['file']['size'];
      $expensions= array("jpeg","jpg","png","gif");

      if(in_array($file_ext,$expensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }

      if($file_size > 2097152){
         $errors[]='File size must be excately 2 MB';
      }

      if(empty($errors)==true){
         move_uploaded_file($temp_name,$_SERVER['DOCUMENT_ROOT']."/"."gripOffers/Store_Brand/store_admin/images/".$image);
         echo "Your file upload successfully.";
      }else{
         print_r($errors);
      }
}
 ?>

也许它会帮助你

答案 1 :(得分:0)

  

更改<Form action="">并尝试此操作...

<!DOCTYPE html>
<html>
<body>

<form action="example.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="file" id="file">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>


<?php

error_reporting(0);

if(isset($_POST['submit'])) {

    $image_name = $_FILES['file']['name'];

    $temp_name = $_FILES["file"]["tmp_name"];

     $file_extension = strtolower(end(explode('.',$_FILES['file']['name'])));

     $file_size =$_FILES['file']['size'];

      $expensions = array("jpeg","jpg","png","gif");

      if(in_array($file_extension,$expensions)=== false){

         $message = "File Type Not allowed, Please choose a JPEG or PNG file.";
      }

      if($file_size > 500000){

         $message = '<h1> File size Too Large !! </h1>';
      }

      if(empty($message)==true){

         move_uploaded_file($temp_name,$_SERVER['DOCUMENT_ROOT']."/gripOffers/Store_Brand/store_admin/images/".$image_name);

         $message = "<h1> Your file upload successfully. </h1>";
      }
      else{

         $message = "<h1> Something is Wrong  </h1>";
      }
}


if(isset($message))
{
echo '<div style="color:#FF0000;text-align:center;font-size:12px;">'.$message.'</div>';
}

 ?>