duanluanlang8501 2014-09-15 02:34
浏览 339
已采纳

如何确定PHP move_uploaded_file失败的原因

Given:

function upload($file_id, $destination) {
  $folder = "/uploads";
  $types = "jpg, jpeg, gif, tiff, png";
  if (! $_FILES [$file_id] ['name'])
      return false;

  $fileName = $_FILES [$file_id] ['name'];

  if (! testExtensions ( $fileName, $types )) {
      echo ("<p>FAILED TEST EXTENSIONS</p>");
      return false;
  }

  $uniqer = substr ( md5 ( uniqid ( rand (), 1 ) ), 0, 5 );
  $fileName = $uniqer . '_' . $fileName; // Get Unique Name
  $path = getPath ( $destination ) . "/";

  $uploadFile = $path . $fileName;

 if (! move_uploaded_file ( $_FILES [$file_id]['tmp_name'], $uploadfile )) {
    return false;
 } else {
    if (! $_FILES [$file_id] ['size']) { // Check if the file is made
        @unlink ( $uploadfile ); // Delete the Empty file
        $file_name = '';
        return false;
    } else {
        chmod ( $uploadfile, 0777 ); // Make it universally writable.
    }
  }

 return $file_name;
}

function getPath($destination) {
  $path = getcwd ();

  $pathParts = explode ( "/", $path );
  array_pop ( $pathParts );

  array_push ( $pathParts, $destination );

  $path = implode ( "/", $pathParts );

  return $path;
}

 function testExtensions($fileName, $types) {
  $extensions = split ( "\.", basename ( $fileName ) );
  $extension = strtolower ( $extensions [count ( $extensions ) - 1] ); // Get the last extension

  $all_types = explode ( ",", strtolower ( $types ) );
  if ($types) {
    if (! in_array ( $extension, $all_types ))
        return false;
  }
  return true;
}

is there any way to test why move_uploaded_file failed? I've generated some trace output to make sure that everything makes sense, but all of the output seems to suggest that move_uploaded_file should be working. However, the function consistently returns false.

  • 写回答

1条回答 默认 最新

  • dongyuelian9602 2014-09-15 02:57
    关注

    There is only probable reason I know that move_uploaded_file fail is the destination of your file. It can be either no permission to the dir or it doesn't exist (aside from syntax error).

    To address the issue you have to edit the permission of the destination directory. The principle of least privilege applies. Only give users the rights they need and no more.

    In this case, if Apache is only serving up pages, give the user acct no rights to edit. Possible risks include: changing file content or uploading new one; adding executable code to files, etc. These risks exists regardless of whether it is a single site up multiples. If the application has a need to edit a specific file, restrict permissions changes to that file.

    Here's how to change it:

    [me@linuxbox me]$ chmod 600 some_file
    

    Ideally, you need a permission code

    766 - The file's owner may read, write, and execute the file. But others can only read and write.

    but to be safe, you can use:

    755 - The file's owner may read, write, and execute the file. All others may read and execute the file. This setting is common for programs that are used by all users..

    UPDATE

    You can change the permission of the whole directory using this command:

    find /opt/lampp/htdocs -type d -exec chmod 755 {} \;
    

    assuming that the location is /opt/lampp/htdocs

    As Barmar quoted, you can check the size of the file if it is uploaded. The fix I mentioned is when you are moving the files itself.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退