dpxua26604 2019-03-29 11:09
浏览 95

如何让用户将文件上传到服务器

I have a page, and I want it to allow users to upload a file to my server so that I can see it later. I have already gotten started, but when you click submit, it takes you to 500 error. Here is what code I have so far:

HTML:

<form action="uploadfile.php" enctype="multipart/form-data" method="POST">
Choose File: 
<input name="userfile" type="file">
<p></p>
<p class="section-content"><input type="submit" value="Upload File"></p>
</form>

PHP: (named uploadfile.php)

<?php 
    $path = "files/"; $path = $path . basename( $_FILES['userfile']['name']);

    if(move_uploaded_file($_FILES['userfile']['tmp_name'], $path)) { 
        echo "Success uploading". basename($_FILES['userfile']['name']); 
    } else{ 
        echo "Error when uploading file."; 
    } 
?>

In HTML, there is a button to choose the file and one to submit. The submit button takes you to uploadfile.php, and that appears as 500 - internal server error. Note that it does not just say 'Error when uploading file' like it should when there is an error. I am new to PHP, so I don't know if I am doing something completely wrong, or maybe there is a way to do it in Javascript, which I am slightly more experienced in?

Thank you in advance.

Edit: I have tried 2 different browsers (Chrome and Edge)

  • 写回答

1条回答 默认 最新

  • dpj96988 2019-03-29 13:57
    关注

    corrected

    use $_SERVER['DOCUMENT_ROOT'] instead of basename()

    $path = $_SERVER['DOCUMENT_ROOT'].'/your root file name/files';
           $name = $_FILES['userfile']['name'];
           $tmp_name = $_FILES['userfile']['tmp_name'];
    
           move_uploaded_file($tmp_name, $path.'/'.$name);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助