简体   繁体   中英

move_uploaded_file error

I was writing a form for uploading photos but it doesn't work. Here is my code:

HTML:

<!DOCTYPE html>
<html>
<body>

<form action="akcija.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload">
    <input type="submit" value="Ucitaj" name="submit">
</form>

</body>
</html>

PHP:

?php
$fileName = $_FILES["fileToUpload"]["name"];
$path = "/htdocs/";
$newFilePlace = $path . $fileName;
$tmpname = $_FILES["fileToUpload"]["tmp_name"];
echo $fileName . "    nejm" . "<br/>" ;
echo $newFilePlace . "<br/>";
echo $tmpname . " hej <br/>";



if (!move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newFilePlace)) {
echo "ERROR";
}


?>

Output:

800px-Juan_Manuel_Santos_and_Lula_(cropped).jpg nejm

/htdocs/800px-Juan_Manuel_Santos_and_Lula_(cropped).jpg /tmp/phpAQPhC7

hej ERROR

Only add the root path of folder and create a folder name htdocs inside the folder where the file is.

define ('SITE_ROOT', realpath(dirname(__FILE__)));
$fileName = $_FILES["fileToUpload"]["name"];
$path = SITE_ROOT;  // for root folder 
$path1 = SITE_ROOT . "foldername"; // for specific folder
$newFilePlace = $path . $fileName;
$tmpname = $_FILES["fileToUpload"]["tmp_name"];
echo $fileName . "    nejm" . "<br/>" ;
echo $newFilePlace . "<br/>";
echo $tmpname . " hej <br/>";

if (!move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newFilePlace)) {
echo "ERROR";
}

This is tested and working fine for me

ok you sould use the path as empty string if you want to upload the file to htdocs keeping in mind that your file also exists on the htdocs directory. This code works fine for you.

<!DOCTYPE html>
<html>
<body>

    <form action="" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="fileToUpload">
        <input type="submit" value="Submit" name="submit">
    </form>

</body>
</html>

<?php
if(isset($_POST["submit"])){
    $fileName = $_FILES["fileToUpload"]["name"];
    $path = "";
    $newFilePlace = $path . $fileName;
    $tmpname = $_FILES["fileToUpload"]["tmp_name"];
    echo $fileName . "    nejm" . "<br/>" ;
    echo $newFilePlace . "<br/>";
    echo $tmpname . " hej <br/>";



    if (!move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newFilePlace)) {
        echo "ERROR";
    }
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM