How to display or preview a uploaded image after submitted in PHP without save to database?

前端 未结 4 1311
北恋
北恋 2021-01-19 07:07

There are actually 2 page involve in my project.The 1st page is a form that to submit the uploaded image into 2nd page based on action=\"2page.php\" and will di

相关标签:
4条回答
  • 2021-01-19 07:28

    send your file to a php script with jQuery and return the temp name and path to jQuery and put the response on a <img> and then if the user clicks some button insert that temporal image to your database with a new jQuery $.post() to another php script so one php to get the file and another to insert it on your db

    This link will help you sending files with AJAX or jQuery

    0 讨论(0)
  • 2021-01-19 07:30

    You would only save the file to your database when you choose to. When uploaded, the file will be saved in your temp directory. From there you will have to tell php to move it to a real directory using move_uploaded_file().

    0 讨论(0)
  • 2021-01-19 07:38

    you can try this..here first u can upload a file and then take two divs in html one is for the upload button and another one is for the picture which you wana show... you can give background color for better clarity and understanding..

        <?php
    
    $submit=$_POST['sub'];
    if(isset($submit))
    {
    $name=$_FILES['img']['name'];
    $type=$_FILES['img']['type'];
    
    $size=($_FILES['img']['size'])/1024;
    
    $ext=end(explode('.',$name));
    if (($ext == "gif")
    || ($ext == "jpeg")
    || ($ext == "jpg")
    || ($ext =="png")
    && ($size > 30))
    {
    
    
    ##############################File Renaming ###################################################
    
    $newname=uniqid();
    //$ext=end(explode('.',$name));
    $fullname=$newname.".".$ext;
    $target="pics/";
    $fulltarget=$target.$fullname;
    if(move_uploaded_file($_FILES['img']['tmp_name'],$fulltarget))
    {
        echo "Success";
    
    
    
    }
    else
    {
        echo "Failed";
    }
    ##############################File Renaming end ###################################################
    }
    
    else{
        echo "not successful";
        }
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <link rel="stylesheet" type="text/css" href="abhi.css" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <div id="a1">
    <form name="frm" method="post" enctype="multipart/form-data">
    <input type="file" name="img" /><br />
    
    <input type="submit" name="sub" value="Store" />
    </form>
    </div>
    <div id="a2">
    <?php echo "<img src='$fulltarget'>";?>
    </div>
    </body>
    </html>
    
    0 讨论(0)
  • 2021-01-19 07:42

    if your goal is to upload an image and store it in a directory on the server then this code sample might help get you started (though I won't swear that it's bug free or secure). It's a single page form to upload, save, and display an image.

    <?php
    // prevent timezone warnings
    date_default_timezone_set('America/New_York');
    
    // set the upload location
    $UPLOADDIR = "images/";
    
    // if the form has been submitted then save and display the image(s)
    if(isset($_POST['Submit'])){
        // loop through the uploaded files
        foreach ($_FILES as $key => $value){
            $image_tmp = $value['tmp_name'];
            $image = $value['name'];
            $image_file = "{$UPLOADDIR}{$image}";
    
            // move the file to the permanent location
            if(move_uploaded_file($image_tmp,$image_file)){
                echo <<<HEREDOC
    <div style="float:left;margin-right:10px">
        <img src="{$image_file}" alt="file not found" /></br>
    </div>
    HEREDOC;
            }
            else{
                echo "<h1>image file upload failed, image too big after compression</h1>";
            }
        }
    }
    else{
        ?>
    <form name='newad' method='post' enctype='multipart/form-data' action=''>
        <table>
        <tr>
            <td><input type='file' name='image'></td>
        </tr>
        <tr>
            <td><input name='Submit' type='submit' value='Upload image'></td>
        </tr>
    </table>
    </form>
    <?php
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题