PHP uploading files image only

11

I am not able upload my image file to server, using XAMMP phpmyadmin and apache services. I'm wondering where I did wrong.
I have to use this kind of security code to prevent any extension upload that can exploit by hacker.
It just shows that the files is "Your image was not uploaded" as an error code. Not sure if it is because I didn't specify the root of the website? If yes, how can I specify?

<html>
<head> 
<?php
if(isset($_POST['submit']))
{
$target_dir = '/uploads/';
$target_dir1 = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadedname=$_FILES['fileToUpload']['name'];
$uploadedname_temp=$_FILES['fileToUpload']['name'];
$uploaded_ext=substr($uploadedname_temp,strrpos($uploadedname_temp,'.')+1);
$uploaded_size=$_FILES['fileToUpload']['size'];
if(isset($_POST['submit']))
{
if (($uploaded_ext == "jpg" || $uploaded_ext == "JPG" || $uploaded_ext == "jpeg" || $uploaded_ext == "JPEG") && ($uploaded_size < 1000000)){ 
                if(!move_uploaded_file($_FILES['fileToUpload']['name'], $target_dir1)) { 

                    echo '<pre>'; 
                    echo 'Your image was not uploaded.'; 
                    echo '</pre>'; 

                  } else { 

                    echo '<pre>'; 
                    echo $target_dir1. ' succesfully uploaded!'; 
                    echo '</pre>'; 

                    } 
            } 

            else{ 

                echo '<pre>'; 
                echo 'Your image was not uploaded.'; 
                echo '</pre>'; 

            }   
}

}

?>



</head>
<body>




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


</body>
</html>
898

Answer

Solution:

I tried below code. it is working properly. As there was issue with$uploaded_size you have not declared size variable and used that.

    <html>
    <head> 

    <?php
    if(isset($_POST['submit']))
    {
    $target_dir = realpath(__DIR__) . '/uploads/';
    $target_dir1 = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadedname=$_FILES['fileToUpload']['name'];
    $uploadedname_temp=$_FILES['fileToUpload']['name'];
    $uploaded_size = $_FILES['fileToUpload']['size'];
    $uploaded_ext=substr($uploadedname_temp,strrpos($uploadedname_temp,'.')+1);

    if(isset($_POST['submit']))
        {
         if (($uploaded_ext == "jpg" || $uploaded_ext == "JPG" || $uploaded_ext == "jpeg" || $uploaded_ext == "JPEG") && ($uploaded_size < 1000000)){ 
                    if(!move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_dir1)) { 

                        echo '<pre>'; 
                        echo 'Your image was not uploaded.'; 
                        echo '</pre>'; 

                      } else { 

                        echo '<pre>'; 
                        echo $target_dir1. ' succesfully uploaded!'; 
                        echo '</pre>'; 

                        } 
                } 

                else{ 

                    echo '<pre>'; 
                    echo 'Your image was not uploaded.'; 
                    echo '</pre>'; 

                }   
    }

    }

    ?>



    </head>
    <body>




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


    </body>
    </html>
325

Answer

Solution:

You are trying to move the file by it's original file name. However, when it gets uploaded, there is a['tmp_name']. When youmove_uploaded_file(), you need to refer to the tmp name.

if(!move_uploaded_file($_FILES['fileToUpload']['name'], $target_dir1)) { 

to

if(!move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_dir1)) { 

People are also looking for solutions to the problem: php - utf8 filenames and greek chars

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.