douju4594 2014-01-11 15:17 采纳率: 100%
浏览 54
已采纳

php文件上传失败

I followed every tutorial in the internet that I find to upload a file. But still, It failed. It gave me this error:

Warning: move_uploaded_file(/var/www/projects/upload/TASK.txt): failed to open stream: No such file or directory in /var/www/projects/test/upload.php on line 6 Warning: move_uploaded_file(): Unable to move '/tmp/phpjr2JJA' to '/var/www/projects/upload/TASK.txt' in /var/www/projects/test/upload.php on line 6 Something went wrong

index.html

 <head>
        <title></title>
    </head>
    <body>
        <form method="POST" action="upload.php" enctype="multipart/form-data">
            <input type="file" name="upload" ><br />
            <input type="hidden" name="MAX_FILE_SIZE" value="1024" />
            <input type="submit" name="submit" value="Submit" />
        </form>
    </body>
    </html>

upload.php

<?php

$target_path = $_SERVER['DOCUMENT_ROOT'] . "/upload/";
$target_path = $target_path . basename( $_FILES['upload']['name'] );

if ( move_uploaded_file($_FILES['upload']['tmp_name'], $target_path) ) {
    echo "has been uploaded";
} else {
    echo "Something went wrong";
}

Can you help me and point out where I went wrong? I'm using ubuntu 12.04 and also I tried to change the permission for the /upload folder to 755 and checked the file_upload in php.ini is ON

Any help would be much appreciated. Thanks!

  • 写回答

3条回答 默认 最新

  • dongwende1984 2014-01-11 16:26
    关注

    $_SERVER['DOCUMENT_ROOT'] gives /var/www/projects/upload.. as output / in the start is cause of error

    Hence

    try with relative path

    <?php
    
    $target_path = "upload/";
    $target_path = $target_path . basename( $_FILES['upload']['name'] );
    
    if ( move_uploaded_file($_FILES['upload']['tmp_name'], $target_path) ) {
    echo "has been uploaded";
    } else {
    echo "Something went wrong";
    }
    ?>
    

    worked for me

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

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题