douze2890241475 2018-04-21 16:45
浏览 24

使用PHP在使用表单上传的localhost上保存图像

i'm trying to save an image uploaded with a HTML form on my localhost server (working with Xampp), but although there are no errors, the file isn't saved anywhere. This is the form, really simple:

<form id="form1" action="result.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="imguploaded" id="imguploaded" accept=".png, .jpg, .jpeg"></br>
    <input class="btn btn-outline-danger btn-lg" id="inputbtn" type="submit" value="Upload Image" name="submit">
  </form>

and this is the PHP code (on result.php):

<?php
    $result=false;
    $error=false;
    if (isset($_FILES['imguploaded'])){
        $nomefile = strtolower($_FILES['imguploaded']['name']);
        $path = "caricamenti/$nomefile";
        move_uploaded_file($_FILES['imguploaded']['name'], $path);
        echo($path);
    }
    ?>

the path exists and it is in the same folder of the .PHP files.

  • 写回答

2条回答

  • dongqiongjiong4740 2018-04-21 17:00
    关注

    In move_uploaded_file($_FILES['imguploaded']['name'], $path); the first parameter isn't correct. It should be the temporary path where php stored it intermediatly, which you find in $_FILE['imguploaded']['tmp_name']. So change that line to

    move_uploaded_file($_FILES['imguploaded']['tmp_name'], $path);
    

    relevant docs

    Be sure to:

    • sanitize filename & extention first
    • check for allowed mimetypes, size, ..

    Right now I could easily upload a php script and execute that.

    评论

报告相同问题?