关于php中的move_uploaded_file()函数的问题

Tür*_*der 1 php filesystems upload file-upload

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

这是代码:

如果(isset($ _ POST [ '上传'])){

    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>";

}
Run Code Online (Sandbox Code Playgroud)

sym*_*ean 5

当您声称只有一行不起作用时,您已发布了大约50行代码.删除50行代码并将其替换为:

move_uploaded_file($_FILES['profile_foto']['temp_name'],'tempo/test.jpg');
Run Code Online (Sandbox Code Playgroud)

....并找出当您尝试上传文件时会发生什么.

C.

  • 这种方法的+1.始终从简单的步骤开始,使其工作,然后一次添加一个新功能,测试以确保它工作,重复.从一个复杂的代码开始会让你在发现bug时丢失.您还可以将复杂功能分成单独的功能,例如检查大小,检查文件类型,进行后期处理等. (2认同)