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 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面