【问题标题】:Error in moving file in php在php中移动文件时出错
【发布时间】:2011-12-23 05:27:56
【问题描述】:

我有以下脚本在move_uploaded_file 上显示警告。

警告:move_uploaded_file(/home/oxfordmo/public_html/ticket/attachement/1320610829.docx) [function.move-uploaded-file]:无法打开流:/home/oxfordmo/public_html/ 中没有这样的文件或目录第53行的ticket/open.php

警告:move_uploaded_file() [function.move-uploaded-file]:无法将 '/tmp/phpLHjtMJ' 移动到 /home/oxfordmo/ 中的 '/home/oxfordmo/public_html/ticket/attachement/1320610829.docx'第 53 行的 public_html/ticket/open.php

 if ( $file != "" ) {
     $type = $_FILES["file"]["type"];
     $ext  = pathinfo( $_FILES["file"]['name'], PATHINFO_EXTENSION );
     if ( $ext != "doc" || $ext != "pdf" || $ext != "docx" ) {
         if ( $_FILES["file"]["size"] <= 1024 * 1024 ) {
             if ( file_exists( "$root/ticket/attachement/$time.$ext" ) ) {
                 unlink( "$root/ticket/attachement/$time.$ext" );
             } elseif ( !move_uploaded_file( $_FILES["file"]['tmp_name'], "$root/ticket/attachement/$time.$ext" ) ) {
                 $errorfile = "Unable to move file to destination folder.";
             }
             $file = $time . '.' . $ext;
             $query = mysql_query( "UPDATE tkt_container SET attachment='" . $file . "' WHERE ticket_id='" . $ticket_id . "'" ) or die( mysql_error() );
         } else {
             $errorfile = "File not saved. Size limit Exceed! ";
         }
     } else {
         $errorfile = "File not saved. Invalid Format!";
     }
 }

【问题讨论】:

    标签: php


    【解决方案1】:

    你可以改变这一行:

             } elseif ( !move_uploaded_file( $_FILES["file"]['tmp_name'], "$root/ticket/attachement/$time.$ext" ) ) {
    

             } elseif ( !file_exists($_FILES["file"]['tmp_name']) || !move_uploaded_file( $_FILES["file"]['tmp_name'], "$root/ticket/attachement/$time.$ext" ) ) {
    

    这样,您将在移动文件之前检查文件是否确实存在

    一个好的做法是在对上传的文件进行操作之前检查 FILES 中的“错误”值

    【讨论】:

    • 另外,在$errorfile = "Unable to move file to destination folder."; 语句之后,即使出现错误,以下$file=..$query=.. 仍会执行.. 应该将它们包装在else 语句中
    • 最好是检查错误变量,或者至少将file_exists($_FILES["file"]['tmp_name']) 添加到if($file!="")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 2013-02-21
    • 2011-12-28
    • 2014-04-09
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多