dsjj15012 2014-05-08 21:12
浏览 389

基本文件上传 - 500内部服务器错误

Just playing around with uploading files as it's actually something I've never done before. I copied some supposedly working code from here.

I'm using cPanel hosting from Namecheap, with absolutely nothing changed from the default config.

I think the most likely problem is something very basic that I haven't activated. My HTML looks like this

<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
    Your Photo: <input type="file" name="photo" size="25" />
    <input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

and my PHP looks like this

<?php
//if they DID upload a file...
if($_FILES['photo']['name'])
{
    //if no errors...
    if(!$_FILES['photo']['error'])
    {
        //now is the time to modify the future file name and validate the file
        $new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
        if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
        {
            $valid_file = false;
            $message = 'Oops!  Your file\'s size is to large.';
        }

        //if the file has passed the test
        if($valid_file)
        {
            //move it to where we want it to be
            move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);
            $message = 'Congratulations!  Your file was accepted.';
        }
    }
    //if there is an error...
    else
    {
        //set that to be the returned message
        $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
    }
}

//you get the following information for each file:
$_FILES['field_name']['name']
$_FILES['field_name']['size']
$_FILES['field_name']['type']
$_FILES['field_name']['tmp_name']
}

When I try to upload an image, I get a 500 Internal Server Error when I hit submit.

What am I missing?

Thanks

  • 写回答

1条回答 默认 最新

  • dongxun8189 2014-05-08 21:21
    关注

    Get rid of the stuff at the bottom:

    <?php
    //if they DID upload a file...
    if($_FILES['photo']['name'])
    {
        //if no errors...
        if(!$_FILES['photo']['error'])
        {
            //now is the time to modify the future file name and validate the file
            $new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
            if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
            {
                $valid_file = false;
                $message = 'Oops!  Your file\'s size is to large.';
            }
    
            //if the file has passed the test
            if($valid_file)
            {
                //move it to where we want it to be
                move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);
                $message = 'Congratulations!  Your file was accepted.';
            }
        }
        //if there is an error...
        else
        {
            //set that to be the returned message
            $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
        }
    
    }
    

    Not sure what that was for... Also, try checking the Namecheap php.ini in your CPanel to see what the max upload size is so your users get your error, not a PHP error or a 500.

    评论

报告相同问题?

悬赏问题

  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)