Php 存在内部服务器错误时上载图像

Php 存在内部服务器错误时上载图像,php,Php,我正在制作图像上传器,它工作正常,直到我尝试上传同名图像。我在课堂上做了递归方法,也许这就是问题所在。 我得到了这个错误: 500内部服务器错误 图像不是很大,它在不存在时工作 这是我的upload.php文件: $upload = new Upload("post", $_FILES['file']); $upload->upload(); 这是我的Uploader.php类文件: class Upload { protected $_type = null, $

我正在制作图像上传器,它工作正常,直到我尝试上传同名图像。我在课堂上做了递归方法,也许这就是问题所在。 我得到了这个错误:

500内部服务器错误

图像不是很大,它在不存在时工作

这是我的upload.php文件:

$upload = new Upload("post", $_FILES['file']);
$upload->upload();
这是我的Uploader.php类文件:

class Upload {

protected $_type = null,
          $_file = null,
          $_file_name = null,
          $_file_tmp = null,
          $_slug = null,
          $_path = null;

public function __construct($type, $file, $slug = null) {
    $this->_file = $file;
    $this->_file_tmp = $file['tmp_name'];
    $this->_file_name = $file['name'];
    $this->_slug = $slug;

    if($type == "post") {
        $this->_type = "post";
        $config1 = '/admin/posts/images'; // Relative to domain name
        $config = $_SERVER['DOCUMENT_ROOT'] . $config1; // Physical path. [Usually works fine like this]
        $this->_path = $config . "/";
    } else if($type == "gallery") {
        $this->_type = "gallery";
        $config1 = '/admin/galeries/images'; // Relative to domain name
        $config = $_SERVER['DOCUMENT_ROOT'] . $config1; // Physical path. [Usually works fine like this]
        $this->_path = $config . "/";
    }
}

public function upload() {
    $new_name = "";
    if($this->_slug == null) {
        $new_name = $this->_file_name;
    } else {
        $new_name = $this->_slug . "-" . $this->_file_name;
    }

    if (file_exists($this->_path.$new_name)) {
        if($this->_slug != null)
            $new_name = $this->_slug . "-" . rand(00,99) . "-" . $this->_file_name;
        else
            $new_name = rand(0000,9999) . "-" . $new_name;

        // MAYBE HERE IS PROBLEM...
        $this->upload();
    } else {
        move_uploaded_file($this->_file_tmp, $this->_path.$new_name);
        return true;
    }
}

正如您所看到的,我的方法upload()是递归调用的。我想知道这些受保护的变量在调用之后是否消失了。

我必须添加
$this->\u file\u name=$new\u name以上
$this->upload()

如何
上传
了解您对
$new\u name
所做的更改?或者将其用作类属性或将其作为参数传递,否则-它超出范围。我会尝试将其作为参数传递。当图像不存在时,所有工作都正常。如果图像不存在,则条件返回false,并且脚本运行
else
部分。问题出在你的
if
块中,它似乎创建了一个无限循环。这是一个完整的答案,我会像答案一样检查它