PHP upload image

前端 未结 10 2448
不知归路
不知归路 2020-12-06 01:46

Alright I have way to much time invested in this. I am new to PHP programming and trying to grasp the basics, but I am a little lost as of last night I was able to get a PHP

相关标签:
10条回答
  • 2020-12-06 01:59

    Change function file_get_content() in your code to file_get_contents() . You are missing 's' at the end of function name. That is why it is giving undefined function error.

    file_get_contents()

    Remove last unnecessary comma after $image filed in line

    "INSERT INTO content VALUES         ('','','','','','','','','','$image_name','$image',)
    
    0 讨论(0)
  • 2020-12-06 02:01

    Here is a basic example of how an image file with certain restrictions (listed below) can be uploaded to the server.

    • Existence of the image.
    • Image extension validation
    • Checks for image size.

      <?php
         $newfilename = "newfilename";
      
         if(isset($_FILES['image'])){
            $errors= array();
            $file_name = $_FILES['image']['name'];
            $file_size =$_FILES['image']['size'];
            $file_tmp =$_FILES['image']['tmp_name'];
            $file_type=$_FILES['image']['type'];
            $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
      
            $expensions= array("jpeg","jpg","png");
            if(file_exists($file_name)) {
              echo "Sorry, file already exists.";
              }
            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($file_tmp,"images/".$newfilename.".".$file_ext);
              echo "Success";
              echo "<script>window.close();</script>";
      
            }
      
            else{
               print_r($errors);
            }
         }
      ?>
      <html>
         <body>
      
            <form action="" method="POST" enctype="multipart/form-data">
               <input type="file" name="image" />
               <input type="submit"/>
            </form>
      
         </body>
      </html>
      

      Credit to this page.

    0 讨论(0)
  • 2020-12-06 02:04
    <?php
    include("config.php");
    
    if(isset($_POST['but_upload'])){
     
      $name = $_FILES['file']['name'];
      $target_dir = "upload/";
      $target_file = $target_dir . basename($_FILES["file"]["name"]);
    
      // Select file type
      $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
    
      // Valid file extensions
      $extensions_arr = array("jpg","jpeg","png","gif");
    
      // Check extension
      if( in_array($imageFileType,$extensions_arr) ){
     
         // Insert record
         $query = "insert into images(name) values('".$name."')";
         mysqli_query($con,$query);
      
         // Upload file
         move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$name);
    
      }
     
    }
    ?>
    <form method="post" action="" enctype='multipart/form-data'>
      <input type='file' name='file' />
      <input type='submit' value='Save name' name='but_upload'>
    </form>
    

    Select the name or path of the image which you have stored in the database table and use it in the image source. Read More

     <?php
        $sql = "select name from images where id=1";
        $result = mysqli_query($con,$sql);
        while($row = mysqli_fetch_array($result)){
              $image = $row['name'];
              $image_src = "upload/".$image; 
              <img src='<?php echo $image_src;  ?>' > echo '<br>';
        }
        ?>
    

    source code: https://www.phpcodingstuff.com/blog/how-to-insert-image-in-php.html

    0 讨论(0)
  • 2020-12-06 02:07

    Simple PHP file/image upload code on same page.

    <form action="" method="post" enctype="multipart/form-data">
      <table border="1px">
        <tr><td><input type="file" name="image" ></td></tr>
        <tr><td> <input type="submit" value="upload" name="btn"></td></tr>
      </table>
    </form>
    
     <?php
       if(isset($_POST['btn'])){
         $image=$_FILES['image']['name']; 
         $imageArr=explode('.',$image); //first index is file name and second index file type
         $rand=rand(10000,99999);
         $newImageName=$imageArr[0].$rand.'.'.$imageArr[1];
         $uploadPath="uploads/".$newImageName;
         $isUploaded=move_uploaded_file($_FILES["image"]["tmp_name"],$uploadPath);
         if($isUploaded)
           echo 'successfully file uploaded';
         else
           echo 'something went wrong'; 
       }
    
     ?>
    
    0 讨论(0)
  • 2020-12-06 02:11

    This code is very easy to upload file by php. In this code I am performing uploading task in same page that mean our html and php both code resides in the same file. This code generates new name of image name.

    first of all see the html code

    <form action="index.php" method="post" enctype="multipart/form-data">
     <input type="file" name="banner_image" >
     <input type="submit" value="submit">
     </form>
    

    now see the php code

    <?php
    $image_name=$_FILES['banner_image']['name'];
           $temp = explode(".", $image_name);
            $newfilename = round(microtime(true)) . '.' . end($temp);
           $imagepath="uploads/".$newfilename;
           move_uploaded_file($_FILES["banner_image"]["tmp_name"],$imagepath);
    ?>
    
    0 讨论(0)
  • 2020-12-06 02:12
      <?php 
     $target_dir = "images/";
        echo $target_file = $target_dir . basename($_FILES["image"]["name"]);
        $post_tmp_img = $_FILES["image"]["tmp_name"];
        $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
        $post_imag = $_FILES["image"]["name"];
            move_uploaded_file($post_tmp_img,"../images/$post_imag");
     ?>
    
    0 讨论(0)
提交回复
热议问题