【问题标题】:HTML PHP Form Upload .doc and .docx only [closed]HTML PHP 表单仅上传 .doc 和 .docx [关闭]
【发布时间】:2011-10-18 03:13:51
【问题描述】:

我需要一些语法方面的帮助。我需要上传 .doc 或 .docx 文件的可能性。但我不知道该怎么做。请看我的代码:

//Сheck that we have a file
if((!empty($_FILES["datafile"])) && ($_FILES['datafile']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 2mb
  $filename = basename($_FILES['datafile']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "docx,doc") && ($_FILES["datafile"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document") && 
    ($_FILES["datafile"]["size"] < 2100000)) {
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/upload/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['datafile']['tmp_name'],$newname))) {
           $sasquach = "The file has been saved as: ".$newname;
           $link= 'http://orchardsystems2012.co.za/'.$_SERVER['PATH_INFO'].'/upload/'.$filename;
        } else {
           //echo "Error: A problem occurred during file upload!";
        }
      } else {
         //echo "Error: File ".$_FILES["datafile"]["name"]." already exists";
      }
  } else {
     //echo "Error: Only .jpg images under 2mb are accepted for upload";
  }
} else {
 //echo "Error: No file uploaded";
}

请帮忙:)

【问题讨论】:

    标签: html forms file file-upload


    【解决方案1】:
    .....
        if (in_array($ext, array("docx", "doc")
          && (in_array($_FILES["datafile"]["type"], array("application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
    ....
    

    查看其他类型here

    【讨论】:

    • 非常好:) 非常感谢!
    • 但是为什么如果我替换这个位: if (($ext == "docx,doc") until ($_FILES["datafile"]["size"]
    • @brett 不起作用是什么?有什么错误吗?
    • 抱歉,伙计,是的,它现在可以工作了-谢谢只是以不同的方式完成了...我不是PHP专家,所以我现在问了很多问题,因为有些人对我投了反对票,所以我不能问问题。 ..啊,好吧
    • +1 : 这真的很有帮助 :)
    猜你喜欢
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多