PHP仅上传文件图片

时间:2018-10-22 11:55:26

标签: php

我无法使用XAMMP phpmyadmin和apache服务将图像文件上传到服务器。我想知道哪里做错了。
我必须使用这种安全代码来防止任何可被黑客利用的扩展上传。
它只是显示文件“您的图片未上传”为错误代码。 不确定是否是因为我未指定网站的根目录?如果是,该如何指定?

<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>

2 个答案:

答案 0 :(得分:0)

我尝试了以下代码。它工作正常。由于$uploaded_size出现问题,您尚未声明size变量并使用它。

    <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>

答案 1 :(得分:0)

您正尝试按其原始文件名移动文件。但是,当它上传时,会有一个['tmp_name']move_uploaded_file()时,您需要引用tmp名称。

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

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