上传困难&使用PHP

时间:2015-05-08 17:09:44

标签: php mysql directory

我可以将图像作为序列化数组上传没问题,但我需要的是将原始文件名字符串存储在我的数据库中,并且我不确定从哪里开始编辑我的预先存在的代码让它工作。这应该更容易,但作为一个PHP新手,我无法让它工作。

基本上,我希望能够上传图片,然后将其显示在我网站的前端,执行以下操作:

<img src="img/<php echo $config->photo_a ?>"/>

我现有的代码是:

<?php
//connect to db //
session_start();
include('../config.php');
// check for login to use //
if (!$user->authenticated)
{
    header('Location: login.php');
    die();
}
//post form as array using class photo_loader//
if (isset($post->form_action))
{
$a = new photo_loader(false, $db);
$a->name = $post->name;
$image_files = array();
for ($i=1; $i<10; $i++)
{
    if (isset($_FILES['file'.$i]['name']) && $_FILES['file'.$i]['name'] != "")
    {
        $img = new upload($_FILES['file'.$i], M_ENV_SITE_URL, M_ENV_SITE_ROOT);
        $img->set_upload_target("/img/");
        $n = $img->do_upload();

        if (!$n)
        {
            $err = "Image file ".$i." too big or wrong file type.";
        }
        else
        {
            $image_files[] = $n;
            $img->batchResize("/img/", "/img/", $n, array("320x240", "800x600"));
        }
    }
}
if (empty($image_files)) $err = "You must include at least one image.";
$a->value = $image_files;
if (!$err)
{
    $a->create();
    $succ = "Success!";
}
}
?>

使用这样的简单表格:

                <form action="" method="post" enctype="multipart/form-data">
                <div class="control-group"><label for="file" class="control-label">Attach Slideshow Images:</label><div class="controls">
                        <?php
                        for ($i=1;$i<10;$i++)
                        {
                            echo "<input name=\"file".$i."\" type=\"file\" value=\"\" id=\"file".$i."\" />";
                        } ?>
                    </div></div>
                     <input type="hidden" name="name" value="photo_a"> 

                <div class="form-actions">
                    <input type="submit" name="form_action" class="btn btn-large btn-primary" value="Save" />
                </div>
            </form>

和photo_loader.class.php看起来像这样:

<?php 

class photo_loader
{
private $properties;
var $db;

function __construct($id, $dbase)
{
    $this->db = $dbase;

    if (is_numeric($id))
    {
        $sql = sprintf(
            "SELECT * FROM minty_config 
            WHERE ID=%d",
            $this->db->clean($id)
        );

        $result = $this->db->query($sql);
        $fields = $this->db->fetch_array($result);

        foreach ($fields as $k => $v)
        {
            $this->properties[$k] = $v; 
        }

        $this->value = unserialize($this->value);
    }
}

function __get($k)
{
    return $this->properties[$k];
}

function __set($k, $v)
{
    $this->properties[$k] = $v;
}

function update()
{
    $sql = sprintf(
        "UPDATE minty_config SET
        name='%s', 
        value='%s'
        WHERE ID=%d",
        $this->db->clean($this->name),
        serialize($this->value),
        $this->ID
    );

    $this->db->query($sql);
}

function create()
{
    $sql = sprintf(
        "INSERT INTO minty_config 
        (name, value) 
        VALUES('%s', '%s')",
        $this->db->clean($this->name),
        unserialize($this->value)       
    );

    $this->db->query($sql);
}

function delete()
{
    $sql = sprintf(
        "DELETE FROM minty_config  
        WHERE ID=%d",
        $this->ID
    );

    $this->db->query($sql);
}
}

?>

我认为我需要删除$ image_files = array();部分,但我不知道要替换什么!看似不断犯错并返回有错误的空白页或不上传图像。我无法看到它过于分散,但我认为我对此采取了错误的方式。非常感谢提前!!

0 个答案:

没有答案