English 中文(简体)
PHP 传递 $FILES 和$POST 作为函数参数
原标题:PHP Passing $_FILES and $_POST as parameters to function

我在网上搜索了使用 ajax 进行 PHP 图像上传的代码 。 我找到了下面的代码 。 问题是我更改了几件东西( 最小 tweaks), 使服务器上的工作正常。 最初它只是处理从表格中发布的数据的php 页面( 不是一个分类或函数) 。 我将其转换成类功能 。 我现在正在跟踪 OOP 。 我认为将程序转换为 OOP 的最佳方法就是将 $_ FILES 和 $_ POST 转换为一种方法, 并在内部处理它们。 我认为这行不通 。 看看这个示例, 请就如何前进提出建议 。

function uploadImageChosen($_FILES, $_POST){
            $path = "../uploads/images/";
            $valid_formats = array("jpg", "png", "gif", "bmp");
            $connectionInstance = new ConnectionClass();
            $connectionInstance->connectToDatabase();
            $imgName;
            $imgURL;
            $imgSize;
            $imgDir = $_POST[ directory ];
            if(isset($_POST) and $_SERVER[ REQUEST_METHOD ] == "POST")
            {
                $name = $_FILES[ photoimg ][ name ];
                $imgSize = $_FILES[ photoimg ][ size ];
                if(strlen($name))
                {
                    list($txt, $ext) = explode(".", $name);
                    if(in_array($ext,$valid_formats))
                    {
                        if($size<(1024*1024))
                        {
                            $imgName = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
                            $tmp = $_FILES[ photoimg ][ tmp_name ];
                            if(move_uploaded_file($tmp, $path.$imgName))
                            {
                                $imgURL = $path.$imgName;
                                $connectionInstance->query("INSERT INTO imagesupload(id, title, url, size, directory) VALUES (null,  $imgName , $imgURL ,  $imgSize ,  $imgDir )");
                                //echo "<img src= uploads/".$imgName."   class= preview >";
                            }
                            else{
                                echo "failed";
                            }
                        }else{
                            echo "Image file size max 1 MB";                    
                        }
                    }else{
                        echo "Invalid file format..";   
                    }
            }else{
                echo "Please select image..!";
            }           

          }//end of if      

        }//end of function

至于排序函数的调用页面,这里是:

<?php
    require_once("../classes/UploadImages.php");
    $uploadInstance = new UploadImages();
    $uploadInstance->uploadImageChosen($_FILES, $_POST);
    //header("LOCATION:portfolio.php");
?>

非常感谢:)

最佳回答

$_POST $_FILES 是超全球阵列,它们总是可用,用函数或方法重新定义它们是一个坏主意。

你可以做这样的事情:

$uploadInstance->uploadImageChosen();

...

function uploadImageChosen(){
           $path = "../uploads/images/";
           $valid_formats = array("jpg", "png", "gif", "bmp");
...
$name = $_FILES[ photoimg ][ name ];
...

或者,如果你需要本地范围的副本 这样做:

$uploadInstance->uploadImageChosen($_FILES, $_POST);

...

function uploadImageChosen($files, $post){
               $path = "../uploads/images/";
               $valid_formats = array("jpg", "png", "gif", "bmp");
...
$name = $files[ photoimg ][ name ];
...
问题回答

尝试从 Inf- statement 中删除 $_Server[REQEST_METHOD] : "POST" , 看看它会做什么 。

上面的脚本有问题, 错误日志中未检测到。 问题首先出现于我对连接类进行即时处理时。 我本应该创建另一个变量, 以接收打开的连接, 以便查询到工作。 这个变量已经解决, 数据现在存在于 DB 中。 第二个问题是 JPG 格式不是可接受类型, 所以我加上了该格式和 jpg (变量) 。 这让文件实际上被传输到上传文件夹 。 感谢大家的支持和不便, 感谢大家 :





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签