dongqin8652 2014-10-22 13:17
浏览 44
已采纳

用PHP上传文件不起作用

I am new to PHP and I'm trying to make an upload script. But it doesn't work completely.

The thing that doesn't work is that when I have uploaded the photo it doesn't store the photo in the folder "uploads". (The folder location is: Applications > MAMP > htdocs > Marjolein)

Also I want to show the photo that has been uploaded in the browser, but this also doesn't work.

I work with a Mac and use MAMP to run my php code. Can you please help me so I can show the picture in the browser and that it will be stored in the folder "uploads"?

The code I have is:

uploader.php

<style>
.sucess{
    color:#088A08;
}
.error{
    color:red;
}
</style>

<?php
    $file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
    $upload_exts = end(explode(".", $_FILES["file"]["name"]));
    if ((($_FILES["file"]["type"] == "image/gif")
            || ($_FILES["file"]["type"] == "image/jpeg")
            || ($_FILES["file"]["type"] == "image/png")
            || ($_FILES["file"]["type"] == "image/pjpeg"))
            && ($_FILES["file"]["size"] < 2000000)
            && in_array($upload_exts, $file_exts))
    {
        if ($_FILES["file"]["error"] > 0)
        {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
        }
        else
        {
            echo "Upload: " . $_FILES["file"]["name"] . "<br>";
            echo "Type: " . $_FILES["file"]["type"] . "<br>";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
            // Enter your path to upload file here
            if (file_exists("://Applications/MAMP/htdocs/Marjolein/uploads/" .
            $_FILES["file"]["name"]))
            {
                echo "<div class='error'>"."(".$_FILES["file"]["name"].")".
                " already exists. "."</div>";
            }
            else
            {
                move_uploaded_file($_FILES["file"]["tmp_name"],
                "://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]);
                echo "<div class='sucess'>"."Stored in: " .
                "://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]."</div>";
            }
        }
    }
    else
    {
    echo "<div class='error'>Invalid file</div>";
    }

?>

<?php 
    if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!='')
    {
?>
        <p><img src="uploads/<?php echo $_REQUEST['show_image'];?>" /></p>
<?php 
    } 
?>

uploadform.html

<html>
<body>

<form action="uploader.php" method="post"
enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

The webbrowser shows after I click on the submit button:

Upload: 0_8caab_996cc75d_orig.jpg
Type: image/jpeg
Size: 538.166992188 kB
Temp file: /Applications/MAMP/tmp/php/phptpCA8B
Stored in: ://Applications/MAMP/htdocs/Marjolein/uploads/0_8caab_996cc75d_orig.jpg
  • 写回答

1条回答 默认 最新

  • dsdsds12222 2014-10-22 13:30
    关注

    Try to echo the image after uploading: (Don´t know about the storage of the image)

    echo '<img src="://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]" border=0>';
    

    Place it here:

    else
    {
    move_uploaded_file($_FILES["file"]["tmp_name"],
    "://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]);
    echo "<div class='sucess'>"."Stored in: " .
    "://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]."</div>";
     echo '<img src="://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]" border=0>';
    }
    

    Maybe the path to the file is not correct. i think you can try this instead of your previous path:

    move_uploaded_file($_FILES["file"]["tmp_name"],
    "../uploads/" . $_FILES["file"]["name"]);
    

    or if the folder "uploads" is located at in the same as the uploader.php you can use this:

    move_uploaded_file($_FILES["file"]["tmp_name"],
    "uploads/" . $_FILES["file"]["name"]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题