【问题标题】:Is this uploading script correct?这个上传脚本正确吗?
【发布时间】:2012-04-12 18:06:35
【问题描述】:

我有一个名为“Mobile_App”的文件夹,该文件夹中有一个 html 页面,其中包含一个文件输入、一个上传文件的 php 脚本和一个名为“ImageFiles”的文件夹。

问题是文件没有被上传到“ImageFiles”。我想知道的是,在将文件上传到“Imagefiles”文件夹时,下面的代码是正确的,还是不正确,我怎样才能让它正常工作?

以下是上传文件的php脚本(文件输入称为“fileImage”):

<?php

   $destination_path = $_SERVER['DOCUMENT_ROOT']."/ImageFiles";

   $result = 0;

   $target_path = $destination_path . basename( $_FILES['fileImage']['name']);

   if(move_uploaded_file($_FILES['fileImage']['tmp_name'], $target_path)) {
      $result = 1;
   }

   sleep(1);

?>

【问题讨论】:

  • 那么当你运行脚本时会发生什么?
  • 当我运行脚本时它只是带有一个空白页
  • 你试过error_reporting()吗?
  • 我会尝试error_reporting,我不知道为什么上传后文件会进入ImageFiles文件夹,因为我尝试了blake305的答案,但没有文件被上传到ImageFiles文件夹中。已经设置了必要的权限,因为我要求组织服务器的人确保脚本和所有文件夹都设置正确以允许上传和删除文件

标签: php


【解决方案1】:

首先,文件夹名和文件名之间没有/。其次,您不需要拥有文档根目录。更正代码:

<?php

   $destination_path = "/ImageFiles";

   $result = 0;

   $target_path = $destination_path . "/" . basename( $_FILES['fileImage']['name']);

   if(move_uploaded_file($_FILES['fileImage']['tmp_name'], $target_path)) {
      $result = 1;
   }

   sleep(1);

?>

【讨论】:

    【解决方案2】:

    没有。您根本没有检查上传是成功还是失败。您也不会检查文件名冲突,您的脚本会很高兴地用新文件覆盖任何现有文件。

    <?php
    
    if ($_FILES['fileImage']['error'] !== UPLOAD_ERR_OK) {
       die("Upload failed with error code " . $_FILES['fileImage']['error']);
    }
    
    ...
    $target_path = etc...
    ...
    if (file_exists($target_path)) {
       die("$target_path exists already");
    }
    // got here, must be ok to proceed
    if (move_uploaded_file(....)) {
    etc...
    

    上传错误码为defined here

    【讨论】:

      猜你喜欢
      • 2011-10-21
      • 2017-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 2012-08-07
      相关资源
      最近更新 更多