【问题标题】:form value processing to SQL表单值处理到 SQL
【发布时间】:2014-09-11 14:57:40
【问题描述】:

我使用此代码上传多个图像上传现在我需要将图像路径存储在 SQL 数据库中以进行检索我只想将路径存储在数据库中我使用多个图像上传并存储在文件夹中

class Upload_Rename {
const ALLOWED_TYPES = "jpg,gif,png";
public static function generate_new_name($extension, $uppercase = true, $prefix = '', $sufix = '') {
    $new_name = $prefix . uniqid() . '_' . time() . $sufix;
    return ($uppercase ? strtoupper($new_name) : $new_name) . '.' . $extension;
}

public static function check_and_get_extension($file) {
    $file_part = pathinfo($file);
    $allowed_types = explode(",", Upload_Rename::ALLOWED_TYPES);
    if (!in_array($file_part['extension'], $allowed_types)) {
        throw new Exception('Not ok.. bad bad file type.');
    }
    return $file_part['extension'];
}

public function upload($file, $target_destination) {
    if (!isset($file['tmp_name'])) {
        throw new Exception('Whaaaat?');
    }
    $_name = $file['name'];
    $_tmp = $file['tmp_name'];
    $_type = $file['type'];
    $_size = $file['size'];
    $file_extension = '';
    try {
        $file_extension = Upload_Rename::check_and_get_extension($_name);
    } catch (Exception $e) {
        throw new Exception('Ops.. file extension? what? ' . $e->getMessage());
    }
    $new_name = Upload_Rename::generate_new_name($file_extension, true, 'whaat_', '_okey');
    $destination = $target_destination . DIRECTORY_SEPARATOR . $new_name;
    return move_uploaded_file($_tmp, $destination);
}

public function multiple_files($files, $destination) {
    $number_of_files = isset($files['tmp_name']) ? sizeof($files['tmp_name']) : 0;
    $errors = array();
    for ($i = 0; $i < $number_of_files; $i++) {
        if (isset($files['tmp_name'][$i]) && !empty($files['tmp_name'][$i])) {
            try {
                $this->upload(array(
                    'name' => $files['name'][$i],
                    'tmp_name' => $files['tmp_name'][$i],
                    'size' => $files['size'][$i],
                    'type' => $files['type'][$i]
                        ), $destination);
            } catch (Exception $e) {
                array_push($errors, array('file' => $files['name'][$i], 'error' => $e->getMessage()));
            }
        }
    }
    print_r($errors);
}

}

if ($_FILES) {
$upload = new Upload_Rename();
$destination = 'upload/';
$upload->multiple_files($_FILES['myfile'], $destination);
$path = $upload->multiple_files($_FILES['myfile'], $destination);
}
$mysql_hostname = "";
$mysql_user = "";
$mysql_password = "";
$mysql_database = "";
$tbl_name="";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not     connect database");
mysql_select_db($mysql_database, $bd) or die("Could not select database");
// get the value
$name=$path;
// Insert data into mysql 
$sql="INSERT INTO $tbl_name(path,)VALUES('$name',)";
$result=mysql_query($sql);

?>

<form  method="post" enctype="multipart/form-data">
<?php for ($i = 0; $i < 10; $i++): ?>
    file: <input type="file" name="myfile[]"><br>
<?php endfor; ?>
<input type="submit">
</form>

我如何在 SQL 中存储文件路径并使用该路径显示在另一个 php 页面中

【问题讨论】:

    标签: php sql image forms upload


    【解决方案1】:

    使用它来将路径插入到您的数据库中。

    <?php
    class Upload_Rename {
    const ALLOWED_TYPES = "jpg,gif,png";
    public static function generate_new_name($extension, $uppercase = true, $prefix = '', $sufix = '') {
        $new_name = $prefix . uniqid() . '_' . time() . $sufix;
        return ($uppercase ? strtoupper($new_name) : $new_name) . '.' . $extension;
    }
    
    public static function check_and_get_extension($file) {
        $file_part = pathinfo($file);
        $allowed_types = explode(",", Upload_Rename::ALLOWED_TYPES);
        if (!in_array($file_part['extension'], $allowed_types)) {
            throw new Exception('Not ok.. bad bad file type.');
        }
        return $file_part['extension'];
    }
    
    public function upload($file, $target_destination) {
        if (!isset($file['tmp_name'])) {
            throw new Exception('Whaaaat?');
        }
        $_name = $file['name'];
        $_tmp = $file['tmp_name'];
        $_type = $file['type'];
        $_size = $file['size'];
        $file_extension = '';
        try {
            $file_extension = Upload_Rename::check_and_get_extension($_name);
        } catch (Exception $e) {
            throw new Exception('Ops.. file extension? what? ' . $e->getMessage());
        }
        $new_name = Upload_Rename::generate_new_name($file_extension, true, 'whaat_', '_okey');
        $destination = $target_destination . DIRECTORY_SEPARATOR . $new_name;
        move_uploaded_file($_tmp, $destination);
        return $destination;
    }
    
    public function multiple_files($files, $destination) {
        $number_of_files = isset($files['tmp_name']) ? sizeof($files['tmp_name']) : 0;
        $errors = array();
        $paths=array();
        for ($i = 0; $i < $number_of_files; $i++) {
            if (isset($files['tmp_name'][$i]) && !empty($files['tmp_name'][$i])) {
                try {
                    $path=$this->upload(array(
                        'name' => $files['name'][$i],
                        'tmp_name' => $files['tmp_name'][$i],
                        'size' => $files['size'][$i],
                        'type' => $files['type'][$i]
                            ), $destination);
                     $paths[]=$path;
                } catch (Exception $e) {
                    array_push($errors, array('file' => $files['name'][$i], 'error' => $e->getMessage()));
                }
            }
        }
        return $paths;
    }
    
    }
    
    if ($_FILES) {
    $upload = new Upload_Rename();
    $destination = 'upload';
    $paths=$upload->multiple_files($_FILES['myfile'], $destination);
    
    //Fill this with correct information
    $mysql_hostname = "localhost";
    $mysql_user = "root";
    $mysql_password = "";
    $mysql_database = "testdb";
    $tbl_name="test";
    $pathfield_name='path';
    //
    
    $mysql= new mysqli($mysql_hostname,$mysql_user,$mysql_password,$mysql_database);
    
    foreach($paths as $path)
    {
        $query='INSERT INTO `'.$tbl_name.'` (`'.$pathfield_name.'`) VALUES ("'.$mysql->escape_string($path).'");';
        $mysql->query($query);
    }
    $mysql->close();
    }
    
    ?>
    
    <form  method="post" enctype="multipart/form-data">
    <?php for ($i = 0; $i < 10; $i++): ?>
        file: <input type="file" name="myfile[]"><br>
    <?php endfor; ?>
    <input type="submit">
    </form>
    

    现在要检索图像,创建一个新文件并将此代码复制到那里:(此文件应该在上传文件夹之外)

    <?php
    
    //Fill this with correct information
    $mysql_hostname = "localhost";
    $mysql_user = "root";
    $mysql_password = "";
    $mysql_database = "testdb";
    $tbl_name="test";
    $pathfield_name='path';
    //
    
    $mysql= new mysqli($mysql_hostname,$mysql_user,$mysql_password,$mysql_database);
    $query='SELECT `'.$pathfield_name.'` FROM `'.$tbl_name.'`';
    $results=$mysql->query($query);
    while($result=$results->fetch_assoc())
    {
        echo '<img src="'.$result[$pathfield_name].'">';
    }
    $results->free();
    $mysql->close();
    ?>
    

    创建上表的 SQL 查询(表名=test,字段名=path):

    CREATE TABLE IF NOT EXISTS `test` (
      `path` varchar(50) NOT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    

    【讨论】:

    • 创建表的 SQL 注释是什么
    • 如何用同一种方法存储多张图片
    • 我在上面发布的代码存储了多个图像并保存了路径。运行它。
    • 一次我上传照片并且路径是存储的,但是为单次上传创建了两行我如何通过使用 id 检索来在单行中存储 8 张图像的路径
    • 抱歉没听清楚,请详细说明。
    猜你喜欢
    • 2018-11-20
    • 2019-06-24
    • 2012-02-19
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    • 2017-08-30
    • 1970-01-01
    相关资源
    最近更新 更多