使用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.

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.

Try this:

 $file_name= $_FILES['imguploaded']['name'];
 $file_tmp_name= $_FILES['imguploaded']['tmp_name'];
 $div= explode('.',$file_name );
 $file_txt= strtolower(end($div));
 $unique_image= substr(md5(time()),0,10).".".$file_txt;
 $uploaded_image= "caricamenti/".$unique_image;
 move_uploaded_file($file_tmp_name,$uploaded_image);