【问题标题】:Image upload takes previous temp file to upload in PHP图像上传需要以前的临时文件在 PHP 中上传
【发布时间】:2012-01-13 16:23:44
【问题描述】:

我在 PHP 中上传图片时遇到问题。

问题是当我第一次上传图像文件时它工作正常。 但是当我第二次尝试上传文件而不刷新页面时,它需要第一个图像名称并上传它。

问题是什么,如何解决?

$name = $_FILES['ImageFile']['name'];
$size = $_FILES['ImageFile']['size'];
$tmp = $_FILES['ImageFile']['tmp_name'];

$path = "public/www/uploads/";
        $valid_formats = array("jpg", "png", "gif", "bmp");
        $response = '';
        if(strlen($name)) {

            list($txt, $ext) = explode(".", $name);
            if(in_array($ext,$valid_formats)) {

                if($size<(1024*1024)) {

                    $actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
                    if(move_uploaded_file($tmp, $path.$actual_image_name)) {

                                                $response = "<img src='public/www/uploads/".$actual_image_name."?parm=".time()."' class='preview'>";

                    } else { 
                        $response = "failed";
                    }
                } else { 
                    $response = "Image file size max 1 MB";                 
                }
            } else {
                $response = "Invalid file format..";    
            }
        } else {
            $response = "Please select image..!";
        }

这里,$response 是一个用于获取状态的变量。

【问题讨论】:

  • 在上面的代码中看不到问题。也许更相关的代码会有所帮助。

标签: php image file file-upload upload


【解决方案1】:

听起来您正在使用某种 AJAX 来调用此函数。

您可能需要找到一种方法来重置此函数末尾的 $_FILES 数组...也许这样的方法会有所帮助:

$_FILES = array();

否则,因为文件上传后没有(明显的)页面刷新(正如你提到的),我认为 $_FILES 变量没有机会自然重置(如果你没有在这里使用 AJAX。)

【讨论】:

  • 我试过了。但是找到了解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-08
  • 1970-01-01
  • 2017-06-06
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多