PHP image upload function, save in a dir and then return save image url

前端 未结 5 402
被撕碎了的回忆
被撕碎了的回忆 2021-01-03 12:55

I am trying to upload an image to server using PHP and the save inside a dir, and then returning the image url.

HTML:



        
相关标签:
5条回答
  • 2021-01-03 13:38

    for file upload try this

    <?php if(isset($_POST['submit']))
    {
    $ImageName = $_FILES['photo']['name'];
    $fileElementName = 'photo';
    $path = 'images/'; 
    $location = $path . $_FILES['photo']['name']; 
    move_uploaded_file($_FILES['photo']['tmp_name'], $location); 
    } ?>
    <form name="form1" id="form1" method="post" action="" enctype="multipart/form-data">
    <input type="file" name="photo">
    <input type="submit" name="submit">
    </form>
    
    0 讨论(0)
  • 2021-01-03 13:43

    First you need a multipart/form-data form for uploading. This is a must :)

    <form action="upload_file.php" method="post"
    enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="submit" value="Submit">
    </form>
    

    The PHP part is fairly simple: This would result your file stored in "upload/{filename}" The main part you want to consider is how to get the filename and back to your write_string_to_database procedure, you could do a simple script after the upload page like

    save_string_to_database("upload/" . $_FILES["file"]["name"]);
    

    would do the trick.

    <?php
    if ($_FILES["file"]["error"] > 0)
      {
      echo "Error: " . $_FILES["file"]["error"] . "<br>";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
          "upload/" . $_FILES["file"]["name"]);
      }
    }
    
    0 讨论(0)
  • 2021-01-03 13:53
    <form method='post' action='' enctype='multipart/form-data'> 
      Name  : <input type="text" name="name"  required=""/><br><br>
      Code  : <input type="text" name="code"  required=""/><br><br>
      Price : <input type="text" name="price" required=""/><br><br>
      Image : <input type="file" name="image" required=""/><br><br>
      <button type='submit' class='buy' name="submit">Add Now</button>
    </form>
    <!--insert data  -->
    <?php
    session_start();
    include('db.php');
    
    if(isset($_POST["submit"]));
    {
      /*echo "<pre>";
      print_r($_POST);
      print_r($_FILES);*/
      $name  = $_POST["name"];
      $code  = $_POST["code"];
      $price = $_POST["price"];
      $image = $_FILES["image"]["name"];
    
      /* folder image save */
    
      // $target_dir  = "/var/www/html/shivam/new/upload/";
      // $target_file = $target_dir.basename($_FILES["image"]["name"]);
      // /*echo "1121".$target_file;*/
    
      // $name = basename($_FILES["image"]["name"]);
    
      // mysqli_query($con,$qry);
    
      // /* move file */                                   
      // move_uploaded_file($_FILES['image']['tmp_name'],$target_dir.$name);
      /* move_uploaded_file($tmp_name, "$target_dir/$name");*/
      /* end */
    
      $uploaddir = '/var/www/html/uploads/';
    $uploadfile = $uploaddir . basename($_FILES['image']['name']);
    echo '44'.$uploadfile;
    echo "<p>";
    
    if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
      echo "File is valid, and was successfully uploaded.\n";
    } else {
       echo "Upload failed";
    }
    
    echo "</p>";
    echo '<pre>';
    echo 'Here is some more debugging info:';enter code here
    print_r($_FILES);
    print "</pre>";
    }
    ?>
    
    0 讨论(0)
  • 2021-01-03 13:53

    This my function, variable $ten_anh is name of file image in html.

    function upload_anh($ten_anh){ //$ten_anh la ten tren html vi du "avatar"
    if(isset($_FILES[$ten_anh])){
         $errors= array();
         $file_name = $_FILES[$ten_anh]['name'];
         $file_size =$_FILES[$ten_anh]['size'];
         $file_tmp =$_FILES[$ten_anh]['tmp_name'];
         $file_type=$_FILES[$ten_anh]['type'];
         $file_ext=strtolower(end(explode('.',$_FILES[$ten_anh]['name'])));
    
         $expensions= array("jpeg","jpg","png");
    
         if(in_array($file_ext,$expensions)=== false){
                $errors[]="Không chấp nhận định dạng ảnh có đuôi này, mời bạn chọn JPEG hoặc PNG.";
         }
    
         if($file_size > 2097152){
                $errors[]='Kích cỡ file nên là 2 MB';
         }
    
         if(empty($errors)==true){
                move_uploaded_file($file_tmp,"../images/".$file_name);
                echo "Thành công!!!";
         }
         else{
                print_r($errors);
         }
    }
    

    }

    Example: - html code:

    <input type="file" id="avatar" name="avatar"accept="image/png, image/jpeg"      required/>
    
    • call function php: upload_anh('avatar');
    0 讨论(0)
  • 2021-01-03 13:59

    Here's a simple one.

    HTML form to upload image

    <form enctype="multipart/form-data" action="upload.php" method="POST">
        <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
        Send this file: <input name="userfile" type="file" />
        <input type="submit" value="Send File" />
    </form>
    

    Your PHP file that does the Upload

    <?php
    
    $uploaddir = '/var/www/uploads/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
    
    echo "<p>";
    
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
      echo "File is valid, and was successfully uploaded.\n";
    } else {
       echo "Upload failed";
    }
    
    echo "</p>";
    echo '<pre>';
    echo 'Here is some more debugging info:';
    print_r($_FILES);
    print "</pre>";
    
    ?> 
    

    Source

    0 讨论(0)
提交回复
热议问题