【问题标题】:The problem about move_uploaded_file() function in phpphp中move_uploaded_file()函数的问题
【发布时间】:2010-02-27 20:45:25
【问题描述】:

我在 apache localhost 中运行了代码,并在我的主机中进行了尝试。在他们两个中,该方法移动了文件,但文件似乎为 0kb。

代码如下:

if(isset($_POST['upload'])){

    if($_FILES['profile_foto']['size']>0&&$_FILES['profile_foto']['size']<102400){

        $image_extension=explode("/",$_FILES['profile_foto']['type']);

        if($image_extension[1]!="jpeg")
            echo "<script type='text/javascript'>alert('The extension of the profile image must be jpeg!!!')</script>";

        else{

            if($_POST['image_id']==""){

                $image_id= md5(uniqid());

                $_FILES['profile_foto']['name']="tempo/".$image_id.".jpg";

                move_uploaded_file($_FILES['profile_foto']['temp_name'],$_FILES['profile_foto']['name']);

                list($width,$height)=getimagesize("tempo/".$image_id.".jpg");

                if($width<=0||$width>170||$height<=0||$height>200){

                    $myFile="tempo/".$image_id.".jpg";
                    $fh=fopen($myFile,'w') or die("The File could not be opened!!!");
                    fclose($fh);
                    unlink($myFile);

                    echo "<script type='text/javascript'>alert('The width of your profile image must be less than 170 px, the height must be less than 200 px!!!')</script>";

                }
                else
                    $_POST['image_id']=$fotograf_id;

            }
            else{

                $image_id= md5(uniqid());

                $_FILES['profile_foto']['name']="tempo/".$image_id.".jpg";

                move_uploaded_file($_FILES['profile_foto']['temp_name'],$_FILES['profile_foto']['name']);

                list($width,$height)=getimagesize("tempo/".$image_id.".jpg");

                if($width<=0||$width>170||$height<=0||$height>200){

                    $myFile="tempo/".$image_id.".jpg";
                    $fh=fopen($myFile,'w') or die("The File could not be opened!!!");
                    fclose($fh);
                    unlink($myFile);

                    echo "<script type='text/javascript'>alert('The width of your profile image must be less than 170 px, the height must be less than 200 px!!!')</script>";

                }
                else{
                    $image_will_be_deleted=$_POST['image_id'];

                    $myFile="tempo/".$image_will_be_deleted.".jpg";
                    $fh=fopen($myFile,'w') or die("The File cannot be opened!!!");
                    fclose($fh);
                    unlink($myFile);
                    $_POST['image_id']=$image_id;

                }
            }
        }
    }
    else
        echo "<script type='text/javascript'>alert('The size of the profile image must be less than 100 kb!!!')</script>";

}

【问题讨论】:

  • 请指定代码运行的环境、操作系统、托管或VPS等
  • @Türker 不要重新标记您的问题,而是显示一些代码!欢迎来到 SO。
  • 如果没有代码我可能会在夜间拍摄,但请确保您要移动的文件存在,if ($_FILES["file"]["error"] > 0) { echo "错误: ” 。 $_FILES["file"]["error"] 。 "
    "; }
  • 您在特定条件下创建空文件。我怀疑您看到的是其中一个,而不是实际上传的图像。那些空文件有什么用?
  • 我检查了 FILE 数组是否不为空并且文件上传没有错误。将文件上传到 FILE 数组没有问题,但是当我尝试使用 move_uploded_file() 时,它会将文件移动到我想要的位置并使用我想要的名称,但它的大小似乎为 0 kb。

标签: php upload file-upload filesystems


【解决方案1】:

您已经发布了大约 50 行代码,而您声称只有其中一个不起作用。去掉 50 行代码,替换为:

move_uploaded_file($_FILES['profile_foto']['temp_name'],'tempo/test.jpg');

....并了解当您尝试上传文件时会发生什么。

C.

【讨论】:

  • 这种方法+1。总是从简单的步骤开始,让它工作,然后一次添加一个新功能,测试以确保它工作,重复。从复杂的代码开始会让你在发现错误时迷失方向。您还可以将复杂的功能拆分为单独的功能,例如检查大小、检查文件类型、进行后期处理等。
【解决方案2】:

我认为

$_FILES['profile_foto']['temp_name']

应该是

$_FILES['profile_foto']['tmp_name']

在声明中

move_uploaded_file($_FILES['profile_foto']['temp_name'], $_FILES['profile_foto']['name']);

【讨论】:

  • 您解决了问题,谢谢。原因是我应该使用 tmp_name 而不是 temp_name。非常感谢您从所有这些复杂的算法中弄清楚这一点。
【解决方案3】:

如果您想要一个小功能来方便地将照片调整为如下所示的大小。如果仅提供 $size1,则图像将使用它作为其最大尺寸调整大小,否则如果提供 $size2,则图像将使用 $size1 作为其最大尺寸按比例调整大小,然后裁剪其余部分。可能比您的 ($width170||$height200) 更容易:

function resizeImg($name, $extension, $size1, $size2) {
    if (preg_match('/jpg|jpeg/',$extension)){
        $image = imagecreatefromjpeg($name);
    }
    if (preg_match('/gif/',$extension)){
        $image = imagecreatefromgif($name);
    }

    $old_width = imageSX($image);
    $old_height = imageSY($image);
    $old_aspect_ratio = $old_width/$old_height; 

    if($size2 == 0){
        $new_aspect_ratio = $old_aspect_ratio;
        if($old_width > $old_height){
            $new_width = $size1;
            $new_height = $new_width / $old_aspect_ratio;
        } else {
            $new_height = $size1;
            $new_width = $new_height * $old_aspect_ratio;
        }
    } elseif($size2 > 0){
        $new_aspect_ratio = $size1/$size2;
        //for landscape potographs
        if($old_aspect_ratio >= $new_aspect_ratio) {
            $new_width = $size1;
            $new_height = $size2;
            $x1 = round(($old_width - ($old_width * ($new_aspect_ratio/$old_aspect_ratio)))/2);
            $old_width = round($old_width * ($new_aspect_ratio/$old_aspect_ratio));
            $y1 = 0;
            //for portrait photographs
        } else{
            $new_width = $size1;
            $new_height = $size2;
            $x1 = 0;
            $y1 = round(($old_height/2) - ($new_height/2));
            $old_height = round($old_width/$new_aspect_ratio);
        }
    }

    $new_image = imagecreatetruecolor($new_width, $new_height);
    imagecopyresized($new_image, $image, 0, 0, $x1, $y1, $new_width, $new_height, $old_width, $old_height);

    return $new_image;
}

【讨论】:

    猜你喜欢
    • 2016-09-19
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 2016-12-14
    • 2020-11-04
    • 2011-07-20
    相关资源
    最近更新 更多