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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站