dongyong8098 2014-07-26 10:14
浏览 66
已采纳

在Php上传照片

I've been trying to do a photo upload using Php. What I am seeing when I do a submit is, the page keeps loading infinitely.

End result the photo is not being uploaded to the directory.

Here is the HTML Code :

<!DOCTYPE html>
<html>
    <head>
        <title>Photo Upload</title>
    </head>
    <body>
        <form action="upload.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>

Php Script :

    <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("uploads/" .
            $_FILES["file"]["name"]))
        {
            echo "<div class='error'>"."(".$_FILES["file"]["name"].")".
                " already exists. "."</div>";
        }
        else
        {
            move_uploaded_file($_FILES["file"]["tmp_name"],
                "uploads/" . $_FILES["file"]["name"]);
            echo "<div class='sucess'>"."Stored in: " .
                "uploads/" . $_FILES["file"]["name"]."</div>";
        }
    }
}
else
{
    echo "<div class='error'>Invalid file</div>";
}
?>

I've taken this code sample from Here.

I cannot understand what is going wrong here, but I think it has something to do with the path, but I am not sure.

Some help will be appreciated.

*I've edited the code and changed it to 'uploads/' . It now works!

  • 写回答

3条回答 默认 最新

  • drpph80800 2014-07-26 10:30
    关注

    I checked it out use Relative Path Instead of absolute path then there will be no problem which ever environment you are working in local or server

    eg:Instead of this C:\Users\Priyabrata\PhpstormProjects\FileUpload/uploads/

    Use uploads/

    It will work

    when you host it on server some hosting providers will not provide the access so you have to change the folder permissions to writable and it will work cheers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口