dongqing220586 2014-01-11 17:28
浏览 52
已采纳

move_uploaded_file返回false,具有正确的路径和权限

So I'm making this file upload system in PHP. The $target_path is the absolute path and the permissions of the folder it will be uploaded into is set to 777. The tmp_name also returns something valid and the $_FILES['file_uploaded'] is an array.

However, when I run the following line of code. It returns false.

move_uploaded_file($_FILES['file_uploaded']['tmp_name'], $target_path)

Am I missing something here?

Update [Form HTML code]

<form action="photos.php" enctype="multipart/form-data" method="POST">

    <input type ="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
    <input type ="hidden" name ="upload" value="upload" />
    <input type="file" name="file_upload" />
    <input type="text" name="tags" value="" />

    <input type="radio" name="size" value="_small" checked> Small<br>
    <input type="radio" name="size" value="_medium"> Medium<br>
    <input type="radio" name="size" value="_large"> Large<br>

    <select name="imagelib_id">
    <option value="1">General</option>
    </select>
<input type="submit" name="submit" value="Upload" />

Solution I missed a filename for the uploaded file. The following line fixed my problem:

move_uploaded_file($_FILES['file_uploaded']['tmp_name'], $target_path . $_FILES['file_uploaded']['name']);
  • 写回答

1条回答 默认 最新

  • doudu9094 2014-01-11 17:35
    关注

    As it says in the PHP manual entry, the second parameter of the needs to be a filename, not a directory. You can follow the sample code and use the "name" value of the array:

    // FIXME: verify filename - see below
    $destination = $target_path . $_FILES['file_uploaded']['name'];    
    move_uploaded_file($_FILES['file_uploaded']['tmp_name'], $destination);
    

    I strongly recommend to follow the tips in this comment on the PHP manual and verify both the filename for dangerous characters and correct file extension:

    // snippet taken from the comment by Yousef Ismaeil Cliprz
    function check_file_uploaded_name ($filename) {
        (bool) ((preg_match("`^[-0-9A-Z_\.]+$`i",$filename)) ? true : false);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程